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 more tests files to pytest

See original GitHub issue

Now 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.g TestBlahBlah:
  • 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 or torch.testing.assert_close (look for other usage of these files in the codebase if needed)
    • 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. Not all groups need a dedicated class though, it’s on a case-by-case basis.

Files that need to be ported:

cc @pmeier

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
NicolasHugcommented, Jun 11, 2021

All done! Thank you so much everyone for the blazing fast PRs!!

1reaction
AnirudhDagarcommented, Jun 10, 2021

I’ll also pickup the only one left test_models_detection_utils.py. Should be quite easy too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parametrizing tests — pytest documentation
Here is a quick port to run tests configured with testscenarios, an add-on from ... file are “session-scoped” because we don't need to...
Read more >
python pytest - receiving test parameters from outside
So I'd suggest passing in a single parameter, the path to a file specifying the rest of your info. Use whatever parsing mechanism...
Read more >
pytest Documentation - Read the Docs
pytest will run all files of the form test_*.py or *_test.py in the current directory and its subdirectories. More generally,.
Read more >
A comprehensive guide to pytest. - Level Up Coding
Unit tests should only test the logic contained within each file (in other words: mock any imports), with a notable exception to this...
Read more >
Two Methods for Testing HTTPS API Calls with Python and ...
API endpoints and web URLs are, thankfully, more secure than ever, usually requiring encrypted HTTPS.... Tagged with python, pytest, webdev, ...
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