GithubHelp home page GithubHelp logo

apowis / pytest-sample-argvalues Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 6 KB

A pip package for helping to choose a random sample from your argvalues in pytest

Home Page: https://pypi.org/project/pytest-sample-argvalues/

License: MIT License

Python 100.00%

pytest-sample-argvalues's Introduction

pytest-sample-argvalues

A utility function to help choose a random sample from your argvalues in pytest

Install

pip install pytest-sample-argvalues

Why would I need this?

One of the cool features of pytest is that you can stack multiple instances of pytest.mark.parametrize, to get all combinations of multiple parametrized arguments. See the docs for full details. For instance:

import pytest


@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
    pass

# This will run the test with the arguments set to x=0/y=2, x=1/y=2, x=0/y=3, and x=1/y=3 exhausting parameters in the order of the decorators.

However, it quickly becomes apparent that this product of parameters can grow vey quickly, leading to too many tests...

Example Usage

This package present an easy method of taking a random sample of your multiple parametrized arguments. This is useful if you don't have time to run all of your tests (or there are simply too many!), and want to check a random sample of them, to find any potential bugs. For instance,

import pytest
from pytest_argvalues_sample import pytest_argvalues_sample

MAX_TESTS = 1_000
SAMPLE_FRACTION = 0.000001
PARAMS = {
    "a": list(range(1000)),
    "b": list(range(1000)),
    "c": list(range(1000)),
}

@pytest.mark.parametrize(
    "a,b,c",
    argvalues=pytest_argvalues_sample(PARAMS, SAMPLE_FRACTION, MAX_TESTS)
)
def test_param_sample(a: int, b: int, c: int):
    assert a + b + c <= 2997

Instead of attempting to run 1000**3 pytests, this will run (1000**3)*0.000001=1000 pytests.

pytest-sample-argvalues's People

Contributors

apowis avatar

Stargazers

Daniel Jary avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.