question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve tests using pytest parametrize

See original GitHub issue

Improve tests using pytest parametrize

The idea is to improve our testing code using pytest.mark.parametrize.

Currently some tests are implemented as :

def test_something():

    def _test(a):
       # internal function doing some tests
       # For example, a dummy check below
       assert 0 < a < 5

    a = 1
    _test(a)

    a = 2
    _test(a, b, c)

We would like to implement that using pytest.mark.parametrize:


@pytest.mark.parametrize("a", [1, 2])
def test_something(a):
    # internal function doing some tests
    # For example, a dummy check below
    assert 0 < a < 5

Another example PR doing that for a test case: https://github.com/pytorch/ignite/pull/2521/files

What to do:

  • Read CONTRIBUTING to see how to develop and execute a specific test: https://github.com/pytorch/ignite/blob/master/CONTRIBUTING.md#run-tests
  • Explore test codebase and identify tests to improve. Please pay attention that some tests most probably can’t be reimplemented with pytest.mark.parametrize as they can generated random data. Let’s skip all _test_distrib_* tests as they can be particularly complicated to port.
  • Once a test to improve is identified, comment out here about it and propose a draft code snippet of how you think to reimplement it using pytest.mark.parametrize.
  • In parallel, you can try to reimplement it locally and test if the new test is still passing. Please make sure not to modify test assertions to make test pass as your PR wont be accepted.
  • Send a draft PR

Thanks!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
vfdev-5commented, Mar 24, 2022

Looks good to me @Ishan-Kumar2 , please send a PR 😃

1reaction
Ishan-Kumar2commented, Mar 24, 2022

Hi @vfdev-5 Great idea of using pytest.mark.parameterize

A simple fix in https://github.com/pytorch/ignite/blob/3a286b1d13a4a0476b3ee8e8046f16465818c9f6/tests/ignite/handlers/test_time_limit.py#L18-L39 would be to write it as


@pytest.mark.parametrize("n_iters, limit",[(20, 10),(5,10)])
def test_terminate_on_time_limit(n_iters, limit):
        started = time.time()
        trainer = Engine(_train_func)

        @trainer.on(Events.TERMINATE)
        def _():
            trainer.state.is_terminated = True

        trainer.add_event_handler(Events.ITERATION_COMPLETED, TimeLimit(limit))
        trainer.state.is_terminated = False

        trainer.run(range(n_iters))
        elapsed = round(time.time() - started)
        assert elapsed <= limit + 1
        assert trainer.state.is_terminated == (n_iters > limit)

and move the _train function outside. Let me know what you think. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parametrizing tests — pytest documentation
Use pytest.raises() with the pytest.mark.parametrize decorator to write parametrized tests in which some tests raise exceptions and others do not.
Read more >
How to Parameterize Python Tests Using Pytest
The @pytest.mark.parametrize() decorator lets you parameterize arguments of the testing function independent of fixtures you created. Now I can ...
Read more >
Deep dive into Pytest parametrization | by George Shuklin
In this article I will focus on how fixture parametrization translates into test parametrization in Pytest. Pytest has two nice features: parametrization ......
Read more >
Using Pytest Parametrize for testing your code - CodinGame
Lets create some generic math operations on different python data types. · Lets write the pytests code for testing above functions. · Finally,...
Read more >
How To Do Parameterization In Pytest With Selenium?
Parameterized tests not only help in avoiding code duplication but also help in improving coverage/test coverage.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found