Reputation: 6197
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?
Upvotes: 5
Views: 3992
Reputation: 89
from test_foo import TestFoo :- this sentence will make TestFoo class available to test_bar.py
and
class TestBar(TestFoo): this makes TestBar Class extends TestFoo....
class TestFoo(unittest.TestCase): def test_foo(self): print ("in test_foo")
class TestBar(TestFoo): #this is because u are extending TestFoo and hence its function are also available to TestBar def test_foo(self): print ("in test_foo")
def test_bar(self):
print ("in test_bar")
now its crystal clear what would be the output if u run this particular module..
Upvotes: -1
Reputation: 390
An easy way to fix this is to import the module instead of importing the test class.
import test_foo
class TestBar(test_foo.TestFoo):
def test_bar(self):
print "in test_bar"
This will allow you to access the TestFoo
class without having the tests run twice.
Upvotes: 8
Reputation: 59593
No. It doesn't have a good way to ignore imports. Test runners simply enumerate the names defined in a module and execute ones that look like a test. For example, if you import the first test_bar.py and dir
the module, it defines both TestFoo
and TestBar
. The test runner sees both tests and executes them.
Similarly, TestBar
has two methods - test_bar
and test_foo
. The test runner does not distinguish between names defined by the test class and those inherited from base classes.
Upvotes: 1