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_models.py to pytest

See original GitHub issue

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

For a similar issue: see https://github.com/pytorch/vision/issues/3945 and #3956

Instructions

There are many tests in this file, but it should be possible to port all of them in one PR as they’re all pretty straightforward. If you’re interested in this issue, please comment below to indicate that you started working on a PR. Look below for some porting tips, and please don’t hesitate to ask for help. Thanks!

In this file there are already some test functions like:

def test_classification_model(model_name, dev):
    ModelTester()._test_classification_model(model_name, dev)

For those, we should just copy the body of _test_classification_model into test_classification_model so that we can get rid of the ModelTester class altogether.

@pytest.mark.parametrize('dev', _devs) should be changed into @pytest.mark.parametrize('dev', cpu_and_gpu()) where cpu_and_gpu() is in common_utils.

The tests that need cuda (e.g. test_fasterrcnn_switch_devices) should use the @needs_cuda decorator, also from common_utils. The test that don’t need cuda should use the @cpu_only decorator.

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.

cc @pmeier

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
AnirudhDagarcommented, Jun 3, 2021

Hi @NicolasHug, I’ll start working on a PR if no one has claimed it yet.

0reactions
NicolasHugcommented, Jun 7, 2021

done in #3978, thanks again @AnirudhDagar

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to invoke pytest — pytest documentation
Pytest supports several ways to run and select tests from the command-line. Run tests in a module. pytest test_mod.py. Run tests in a...
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 >
Porting to pytest: a practical example • Dimitri Merejkowsky
Each link, when clicked displays the choices and has a form to let the user vote. The code is pretty straightforward: # polls/models.py...
Read more >
pytest-asyncio/test_simple.py at master - GitHub
@pytest.mark.asyncio. async def test_unused_port_fixture(unused_tcp_port, event_loop):. """Test the unused TCP port fixture.""" async def closer(_, writer):.
Read more >
Five Advanced Pytest Fixture Patterns - Inspired Python
A close look at five pytest fixture patterns that will teach you just how much you ... connection with the host and port...
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