25.3. Unittest — Unittest Framework — Python 2.7.x Documentation
Some people have already created a template, so I think you should refer to the following. python unittest template-Qiita
The following is an example of a minimum configuration test.
Minimum configuration test example
import unittest
class UnitTest(unittest.TestCase):
    def test1(self):
        self.assertEqual(1, 1)
if __name__ == '__main__':
    unittest.main()
Execution example
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
※important point
The test to be executed must start with test in the method name.
| Method | Evaluation contents | 
|---|---|
| assertEqual(a, b) | a == b | 
| assertNotEqual(a, b) | a != b | 
| assertTrue(x) | bool(x) is True | 
| assertFalse(x) | bool(x) is False | 
| assertIs(a, b) | a is b | 
| assertIsNot(a, b) | a is not b | 
| assertIsNone(x) | x is None | 
| assertIsNotNone(x) | x is not None | 
| assertIn(a, b) | a in b | 
| assertNotIn(a, b) | a not in b | 
| assertIsInstance(a, b) | isinstance(a, b) | 
| assertNotIsInstance(a, b) | not isinstance(a, b) | 
Please refer to the following for details. There are many others. 25.3. Unittest — Unittest Framework — Python 2.7.x Documentation-
Executed when the entire Test is initialized
def setUpModule():
    pass
Run at the end of the entire Test
def tearDownModule():
    pass
Executed at test case initialization
class HogeTest(unittest.TestCase):
#...
    @classmethod
    def setUpClass(cls):
        pass
Execute at the end of TestCase
class HogeTest(unittest.TestCase):
#...
    @classmethod
    def tearDownClass(cls):
        pass
Executed at test case initialization
class HogeTest(unittest.TestCase):
#...
    def setUp(self):
        pass
Execute at the end of TestCase
class HogeTest(unittest.TestCase):
#...
    def tearDown(self):
        pass
Here is the log when the following test is executed.
Execution order confirmation sample of each function (excerpt)
def setUpModule():
    print '## setUpModule!'
def tearDownModule():
    print '## tearDownModule!'
    print unittest.TestResult
class HogeTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print '## setUpClass!'
        pass
    @classmethod
    def tearDownClass(cls):
        print '## tearDownClass!'
    def setUp(self):
        print '## setUp!'
    def tearDown(self):
        print '## tearDown!'
    def test1(self):
        expected = 1
        actual = 1
        self.assertEqual(expected, actual)
Execution result
.....
----------------------------------------------------------------------
Ran 1 tests in 0.000s
OK
## setUpModule!
## setUpClass!
## setUp!
## tearDown!
## tearDownClass!
## tearDownModule!
You can get it with TestResult class. In the frequently used ʻunittest.main () `, implement as follows.
unittest.main()When getting in
def main()
    #By default sys.exit()Will be called, so exit=Specify False
    test_program = unittest.main(exit=False)
    test_result = test_program.result
Please refer to the following for the contents that can be obtained by TestResult.
25.3. Unittest — Unit test framework — Python 2.7.x documentation
python - How to know time spent on each test when using unittest? - Stack Overflow
python unittest template-Qiita Library: unittest --Life with Python python - Unittest causing sys.exit() - Stack Overflow When writing a test with python unittest, use doCleanups for setUp that may fail --Qiita
Recommended Posts