Port more tests files to pytest
See original GitHub issueNow that we support pytest
, we want to remove the use of the unittest
module for different test files. We’ve already ported a lot of tests in previous issues (https://github.com/pytorch/vision/issues/3987, https://github.com/pytorch/vision/issues/3945, https://github.com/pytorch/vision/issues/3956, https://github.com/pytorch/vision/issues/3951), and we want to extend this to more files now.
Instructions
I listed the list of files that need to be ported below. If you’re interested in working on this issue, please comment below with “I’m working on file X
” so that others don’t pick the same file as you do. To keep things simple, please only submit one PR per file, and don’t pick more than 2 files at once. You can pick more files once your PRs are merged. Before picking a group, make sure it wasn’t picked by another contributor first.
Please don’t hesitate to ask for guidance and help! Thanks!!
How to port a test to pytest
Porting a test from unittest
to pytest is usually fairly straightforward. For a typical example, see https://github.com/pytorch/vision/pull/3907/files:
- take the test methods out of the
Tester(unittest.TestCase)
class and just declare it as a function. Note: for some files, it might make sense to keep the class structure, and just rename the class as e.gTestBlahBlah:
- Replace
@unittest.skipIf
withpytest.mark.skipif(cond, reason=...)
- remove any use of
self.assertXYZ
.- Typically
assertEqual(a, b)
can be replaced byassert a == b
when a and b are pure python objects (scalars, tuples, lists), and otherwise we can rely onassert_equal
ortorch.testing.assert_close
(look for other usage of these files in the codebase if needed) self.assertRaises
should be replaced with thepytest.raises(Exp, match=...):
context manager, as done in https://github.com/pytorch/vision/pull/3907/files. Same for warnings withpytest.warns
self.assertTrue
should be replaced with a plainassert
- Typically
- When a function uses for loops to tests multiple parameter values, one should use
pytest.mark.parametrize
instead, as done e.g. in https://github.com/pytorch/vision/pull/3907/files. - It may make sense to keep related tests within a single class. Not all groups need a dedicated class though, it’s on a case-by-case basis.
Files that need to be ported:
-
test_datasets_video_utils.py
#4035 This one will be quite easy, you’ll just need to rename the Tester(unittest.TestCase) class into e.g. TestVideo, and changewith self.assertWarns(UserWarning):
intowith pytest.warns(UserWarning):
-
test_hub.py
#4038 -
test_datasets_samplers.py
https://github.com/pytorch/vision/pull/4037 -
test_models_detection_anchor_utils.py
#4046 -
test_models_detection_negative_samples.py
#4045 -
test_models_detection_utils.py
https://github.com/pytorch/vision/pull/4036 -
test_onnx.py
#4047 -
test_transforms_video.py
#4040
cc @pmeier
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
All done! Thank you so much everyone for the blazing fast PRs!!
I’ll also pickup the only one left
test_models_detection_utils.py
. Should be quite easy too.