lundi 5 octobre 2015

import runs tests twice in pytest

why does py.test run the TestFoo.test_foo() test there? I understand it runs TestBar.test_foo().

Contents of test_foo.py:

import unittest

class TestFoo(unittest.TestCase):
    def test_foo(self):
        print "in test_foo"

Contents of test_bar.py:

from test_foo import TestFoo

class TestBar(TestFoo):
    def test_bar(self):
        print "in test_bar"

Output:

[999]anarcat@marcos:t$ pytest -v
no test dir found testing here: /tmp/t
===========================  test_bar.py  ============================
test_bar (test_bar.TestBar) ... in test_bar
ok
test_foo (test_bar.TestBar) ... in test_foo
ok
test_foo (test_foo.TestFoo) ... in test_foo
ok

===========================  test_foo.py  ============================
test_foo (test_foo.TestFoo) ... in test_foo
ok

*******************************************************************************
Ran 4 test cases in 0.00s (0.00s CPU)
All 2 modules OK

If TestBar is put in the same file as TestFoo, the TestFoo.test_foo() test get ran only once:

import unittest

class TestFoo(unittest.TestCase):
    def test_foo(self):
        print "in test_foo"

class TestBar(TestFoo):
    def test_bar(self):
        print "in test_bar"

Output:

[1001]anarcat@marcos:t$ pytest -v
no test dir found testing here: /tmp/t
===========================  test_foo.py  ============================
test_bar (test_foo.TestBar) ... in test_bar
ok
test_foo (test_foo.TestBar) ... in test_foo
ok
test_foo (test_foo.TestFoo) ... in test_foo
ok

*******************************************************************************
Ran 3 test cases in 0.00s (0.00s CPU)
All 1 modules OK

Shouldn't py.test ignore the tests that are found behind imports?

Aucun commentaire:

Enregistrer un commentaire