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?
Repeating a test Each test collected by pytest will be run count times. If you want to override default tests executions order, you can use --repeat-scope command line option with one of the next values: session , module , class or function (default). It behaves like a scope of the pytest fixture.
Specifying which tests to run Pytest supports several ways to run and select tests from the command-line. This will run tests which contain names that match the given string expression (case-insensitive), which can include Python operators that use filenames, class names and function names as variables.
pytest-ordering is a pytest plugin to run your tests in any order that you specify. It provides custom markers that say when your tests should run in relation to each other. They can be absolute (i.e. first, or second-to-last) or relative (i.e. run this test before this other test).
pytest looks for a conftest.py module in each directory. If you add your general-purpose fixtures to the conftest.py module, then you'll be able to use that fixture throughout the module's parent directory and in any subdirectories without having to import it.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With