1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Unittest's subTest doesn't work with Pytest, or am I crazy?

Discussão em 'Python' iniciado por Stack, Setembro 9, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página