Skip to content

Port test/test_image.py to pytest #3914

@NicolasHug

Description

@NicolasHug

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

Similar issues: #3915, #3909

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. Typically here in test_image.py, these will be things like for pil_mode, mode in conversion:, etc.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions