Reputation: 553
As I understand, pytest should provide parameter information withunittest.TestCase.subtest()
if a test fails. So here is something that resembles my code:
class TestStuff(unittest.TestCase):
def test_foo(self):
for i in range(0, 100):
with self.subTest(msg = "seed", i = i):
np.random.seed(i)
n = np.random.randint(0, 30)
self.assertGreaterEqual(28, n)
This ofcourse fails, and prints out the following:
================================================================================================== test session starts ===================================================================================================
platform darwin -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /Users/foopackage
plugins: openfiles-0.3.2, arraydiff-0.3, doctestplus-0.3.0, remotedata-0.3.1
collected 7 items
foo.py ......F [100%]
======================================================================================================== FAILURES ========================================================================================================
________________________________________________________________________________________________ TestStuff.test_foo _________________________________________________________________________________________________
self = <foo.test_foo.TestStuff testMethod=test_foo>
def test_foo(self):
for i in range(0, 100):
with self.subTest(msg = "seed", i = i):
np.random.seed(i)
n = np.random.randint(0, 30)
> self.assertGreaterEqual(28, n)
E AssertionError: 28 not greater than or equal to 29
foo.py:135: AssertionError
=========================================================================================== 1 failed, 6 passed in 1.91 seconds ===========================================================================================
As you can see, there are no message about which seed (value of i) failed the test. I have read everywhere that Pytest is compatible with unittest, so I cant seem to see the problem here. Can someone explain this? Thx
Upvotes: 4
Views: 2059
Reputation: 11
Simply installing pytest-subtest worked for me:
pip install pytest-subtest
Upvotes: 1
Reputation: 330
For people coming in like me, check if you are using Nosetests instead. I've added it to our project at some point to get test coverage out and several months later we noticed that subTest
don't execute separately (when stuff broke, as usual), and were trying to figure out why all of the sudden subTests are executing inline with main tests.
Other things to look that are not directly related but might help:
settings.py
of your Django app you can also specify a test runner. Mine was, again, nosetest, which doesn't properly work as far as PyCharm & Django goes (for me).django.test.TestCase
is unittest.TestCase
and supports subTest since v3.4 (of unittest)Upvotes: 0