Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port test/test_utils.py to pytest #3909

Closed
NicolasHug opened this issue May 25, 2021 · 3 comments · Fixed by #3917
Closed

Port test/test_utils.py to pytest #3909

NicolasHug opened this issue May 25, 2021 · 3 comments · Fixed by #3917

Comments

@NicolasHug
Copy link
Member

NicolasHug commented May 25, 2021

Currently, most tests in test/test_utils.py rely on unittest.TestCase. Now that we support pytest, we want to remove the use of the unittest module.

Similar issues: #3915, #3914

Instructions

If you're interested in working on this issue, please comment below with "I'm working on <test_method_a>, <test_method_b>, etc..." so that others don't pick the same tests as you do. The tests in this file are fairly easy to port, so it's possible to port all of them at once in a single PR, in which case please say "I'm working on porting all the tests"

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 method out of the Tester(unittest.TestCase) class and just declare it as a function
  • Replace @unittest.skipIf with pytest.mark.skipif(cond, reason=...)
  • remove any use of self.assertXYZ.
    • Typically assertEqual(a, b) can be replaced by assert a == b when a and b are pure python objects (scalars, tuples, lists), and otherwise we can rely on assert_equal which is already used in the file.
    • self.assertRaises should be replaced with the pytest.raises(Exp, match=...): context manager, as done in https://github.com/pytorch/vision/pull/3907/files. Same for warnings with pytest.warns
    • self.assertTrue should be replaced with a plain assert
  • When a function uses for loops to tests multiple parameter values, one should usepytest.mark.parametrize instead, as done e.g. in https://github.com/pytorch/vision/pull/3907/files. From a quick look, I don't think this will be needed for the tests in test_utils.py
@zhiqwang
Copy link
Contributor

Hi @NicolasHug

I'm working on porting all the tests in test/test_utils.py.

@fmassa
Copy link
Member

fmassa commented Jun 4, 2021

@zhiqwang thanks for your help!

@zhiqwang
Copy link
Contributor

zhiqwang commented Jun 5, 2021

Hi @fmassa , This is my honor and I also have learned a lot from your guidance here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants