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.

Port test/test_functional_tensor.py to pytest

See original GitHub issue

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

Instructions

There are many tests in this file, so I bundled them in multiple related groups below. If you’re interested in working on this issue, please comment below with “I’m working on <group X>, <group Y>, etc…” so that others don’t pick the same tests as you do. Feel free to pick as many groups as you wish, but please don’t submit more than 2 groups per PR in order to keep the reviews manageable. Before picking a group, make sure it wasn’t picked by another contributor first. 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 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.
  • It may make sense to keep related tests within a single class. For example here, the tests in group A could be grouped into a TestToPILImage class, the tests in group N could be in TestPad, etc. Not all groups need a dedicated class though, it’s on a case-by-case basis.
  • Important: a lot of these tests rely on self.device because they need to be run on both CPU and GPU. For these, use the cpu_and_gpu() from common_utils instead, e.g.:

https://github.com/pytorch/vision/blob/f7b4cb0438702f67cf71cdd7dd8057fc377fb816/test/test_functional_tensor.py#L845-L846

and you can just replace self.device by device in the test

CC @saswatpp as promised!


(EDIT: oops, I initially named both groups below Group B. Renamed into B1 and B2)

  • Group B1

    • test_hsv2rgb
    • test_rgb2hsv
    • test_rgb_to_grayscale – parametrize over num_output_channels
    • test_center_crop
    • test_five_crop
    • test_ten_crop
  • Group B2 https://github.com/pytorch/vision/pull/3974

    • test_pad – parametrize over dt, pad configs
    • test_resized_crop
  • Group C https://github.com/pytorch/vision/pull/3974 This one might be a bit tricky. It will require some parametrization, and we should probably split each sub-method like _test_affine_all_ops, _test_affine_rect_rotations etc. into a single test function (or method, if you decide to bundle them into a TestAffine class, which would probably make sense).

    • test_affine
  • Group D https://github.com/pytorch/vision/pull/3983 Parametrize over data, dt, a, e, f, (and maybe c if there’s no issue) We should split this test into multiple one, for example the self.assertWarnsRegex should be in a separate test

    • test_rotate
  • Group E https://github.com/pytorch/vision/pull/3977 parametrize over tensor, dt, ksize, sigma

    • test_gaussian_blur

cc @pmeier @vfdev-5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

3reactions
NicolasHugcommented, Jun 7, 2021

Looks like we’re done with this file, thank you so much everyone for your help!! I’ll close this issue, for those who are interested I opened a similar issue in https://github.com/pytorch/vision/issues/3987

2reactions
NicolasHugcommented, Jun 4, 2021

So How to parameterize tensor because I have to parameterize device also? please guide!

We can probably parametrize over a new image_size parameter, like so:

@parametrize('image_size', ('small', 'large'))
def test_gaussian_blur(image_size, ...):
    if image_size == 'small':
        tensor = torch.from_numpy(
            np.arange(3 * 10 * 12, dtype="uint8").reshape((10, 12, 3))
        ).permute(2, 0, 1).to(device)
    else:
        tensor = torch.from_numpy(
            np.arange(26 * 28, dtype="uint8").reshape((1, 26, 28))
        ).to(device)

which is the better way to parameterize

I prefer the second way, unless test_configs can/should be re-used somewhere else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to invoke pytest — pytest documentation
This will execute all tests in all files whose names follow the form test_*.py or \*_test.py in the current directory and its subdirectories....
Read more >
How can I check the port py.test is using - Stack Overflow
I'm running py.test on PyCharm COMMUNITY 2017.3. How can I check the port py.test is using. Or let me know the default port...
Read more >
py-pytest - Ports | MacPorts
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
Read more >
Two Methods for Testing HTTPS API Calls with Python and ...
Otherwise, pytest-httpserver will assign a random port. The heart of the test server setup is the line. httpserver.expect_request(endpoint) ...
Read more >
pytest-httpserver — pytest_httpserver 1.0.6 documentation
pytest -httpserver is a python package which allows you to start a real HTTP server for ... thread and listening on a TCP...
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