I explained how to use unittest in Run unittest in Python (for beginners). While using it, I wanted to test multiple tests at once and organized them. In other words, how to use TestSuite. Official documentation and "[[Python] Two ways to implement a test suite with the unittest module](http://www.yoheim. net / blog.php? q = 20160903) ”was used as a reference.
.
├─sample_a.py
└─tests
├─__init__.py
├─test_samplea.py
├─test_sampleb.py
├─samplec.py
└─sample_suite.py
sample_a.py
class SampleA():
def hello(self):
print('Hello, I am sample of a!')
test_samplea.py
import unittest
from sample_a import SampleA
class TestSampleA(unittest.TestCase):
def test_a1(self):
print('Test sample A1')
a = SampleA()
a.hello()
def test_a2(self):
print('Test sample A2')
def a3(self):
print('Test sample A3')
test_sampleb.py
import unittest
class TestSampleB(unittest.TestCase):
def test_b1(self):
print('Test sample B1')
def test_b2(self):
print('Test sample B2')
def b3(self):
print('Test sample B3')
samplec.py
import unittest
class SampleC(unittest.TestCase):
def test_c1(self):
print('Test sample C1')
def test_c2(self):
print('Test sample C2')
def c3(self):
print('Test sample C3')
sample_suite.py
import unittest
from . import test_samplea
from . import test_sampleb
from . import samplec
def suite():
suite = unittest.TestSuite()
suite.addTest(test_samplea.TestSampleA('test_a1'))
suite.addTest(test_samplea.TestSampleA('a3'))
suite.addTest(unittest.makeSuite(test_sampleb.TestSampleB))
suite.addTest(samplec.SampleC('test_c1'))
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
test_suite = suite()
runner.run(test_suite)
python -m unittest tests.test_samplea The execution result is as follows.
Test sample A1
Hello, I am sample of a!
.Test sample A2
.
----------------------------------------------------------------------
Ran 2 tests in 0.003s
OK
From the output, you can see that the test methods test_a1 and test_a2 are being executed. The method of a3 has not been tested.
python -m unittest The execution result is as follows.
Test sample A1
Hello, I am sample of a!
.Test sample A2
.Test sample B1
.Test sample B2
.
----------------------------------------------------------------------
Ran 4 tests in 0.006s
OK
From the test results, you can see that the tests for test_samplea.py and test_sampleb.py are running. The test for samplec.py has not been run. A file that starts with test runs all the tests for the methods that start with test.
__init __.py
to recognize the tests in the tests directory.
In Python3, even if __ init__.py
does not exist, it will be recognized as a package, so you may not create it. Please note that you will need it when using test discovery.python -m tests.sample_suite
Test sample A1
Hello, I am sample of a!
.Test sample A3
.Test sample B1
.Test sample B2
.Test sample C1
.
----------------------------------------------------------------------
Ran 5 tests in 0.006s
OK
The test added by addTest is running. Test methods (a3) that do not start with test and test files (samplec.py) that do not start with test are also being executed. How to add a test is to add for each method suite.addTest(test_samplea.TestSampleA('test_a1')) Or Addition by class suite.addTest(unittest.makeSuite(test_sampleb.TestSampleB)) there is.
The test discovery method is very convenient because you can run the test without setting anything. However, when the number of tests increases or tests that take a long time to process are added, I want to exclude the tests, so I investigated how to execute only the tests that I decided. I couldn't make a trial and error and thought that it could be implemented with a little simpler implementation and executed with python -m unit test. It's not good because the interface to run is inconsistent. .. .. Please let me know if there is a simpler way to implement it.
Recommended Posts