Improve tests using pytest parametrize
See original GitHub issueImprove 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:
- Created a year ago
- Comments:12 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Looks good to me @Ishan-Kumar2 , please send a PR 😃
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
and move the
_train
function outside. Let me know what you think. Thanks!