Situation 1
Suppose I have a test class that I'd like to re-use, test_doubled.py
:
import unittest
class BaseTestCase(unittest.TestCase):
def test_something(self):
self.assertTrue(True)
It contains a singled class, BaseTestCase
, that inherits from unittest.TestCase
and has a single test method.
Running the test from the command line produces the following output as expected:
$ python -m unittest discover -p test_doubled.py -v
test_something (test_doubled.BaseTestCase) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Situation 2
Now suppose I add a second module, test_doubled_2.py
, like so:
from test_doubled import BaseTestCase
class DerivedTestCase(BaseTestCase):
pass
It inherits from BaseTestCase
, so I expect that it will have the same test method, and that the test method will get executed. However, when I run it, I get the following output:
$ python -m unittest discover -p test_doubled_2.py -v
test_something (test_doubled.BaseTestCase) ... ok
test_something (test_doubled_2.DerivedTestCase) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
It seems to run the method twice, once for the derived class, and once for the base class. This is not what I expect at all.
Situation 3
My suspicion is that, by importing BaseTestCase
from test_doubled_2.py
, the whole BaseTestCase
class is getting executed. This seems to be confirmed by running both test modules:
$ python -m unittest discover -p test_doubled\*.py -v
test_something (test_doubled.BaseTestCase) ... ok
test_something (test_doubled.BaseTestCase) ... ok
test_something (test_doubled_2.DerivedTestCase) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
Running both modules executes the same test method three times, when I would expect it to be executed only twice.
How can I get the test method to only execute once in Situation 2 and twice in Situation 3?
UPDATE
To clarify, the BaseTestCase
should be a usable test on its own, so making it a mixin won't work. I only want to prevent it from getting executed extra times.
Here is the desired outcome:
Running just the
BaseTestCase
test intest_doubled.py
:$ python -m unittest discover -p test_doubled.py -v test_something (test_doubled.BaseTestCase) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
This already works as desired.
Running just the
DerivedTestCase
intest_doubled_2.py
:$ python -m unittest discover -p test_doubled_2.py -v test_something (test_doubled_2.DerivedTestCase) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK
It should only execute the
DerivedTestCase
, and not theBaseTestCase
.Running both:
$ python -m unittest discover -p test_doubled\*.py -v test_something (test_doubled.BaseTestCase) ... ok test_something (test_doubled_2.DerivedTestCase) ... ok ---------------------------------------------------------------------- Ran 3 tests in 0.000s OK
The test method is executed exactly twice, once for
BaseTestcase
and once forDerivedTestCase
.