python includes unittest in the standard module, but I didn't know how to use it right away, so I'll keep the template as a memo so that I can copy and paste it in other projects.
*Notes
From python2.7 and above, unittest can use setUpClass and tearDownClass, which are executed when the test class is initialized. For python2.6 and below, you can use these methods by importing a module called unittest2.
point
# -*- coding: utf-8 -*-
import os, sys, unittest
class Sample():
"""Class to be tested"""
#Method to be tested
def return_hoge(self):
return 'hoge'
#Method to be tested
def return_poyo(self):
return 'poyo'
class SampleTest(unittest.TestCase):
"""A test class that tests a class"""
CLS_VAL = 'none'
#Called only once when the test class is initialized(python2.7 or more)
@classmethod
def setUpClass(cls):
if sys.flags.debug: print('> setUpClass method is called.')
#Execute heavy processing methods to prepare for testing
cls.CLS_VAL = '> setUpClass : initialized!'
if sys.flags.debug: print(cls.CLS_VAL)
#Called only once when the test class is released(python2.7 or more)
@classmethod
def tearDownClass(cls):
if sys.flags.debug: print('> tearDownClass method is called.')
#Release the object prepared by setUpClass
cls.CLS_VAL = '> tearDownClass : released!'
if sys.flags.debug: print(cls.CLS_VAL)
#Called every time the test method is executed
def setUp(self):
if sys.flags.debug: print(os.linesep + '> setUp method is called.')
#Perform a light process to prepare for the test
self.smpl = Sample()
#Called after each test method execution
def tearDown(self):
if sys.flags.debug: print(os.linesep + '> tearDown method is called.')
#Release the object prepared by setUp
def test_hoge(self):
expected = 'hoge'
actual = self.smpl.return_hoge()
self.assertEqual(expected, actual)
def test_poyo(self):
expected = 'poyo'
actual = self.smpl.return_hoge() #Common mistake
self.assertEqual(expected, actual)
if __name__ == '__main__':
#Run unittest
unittest.main()
Put the -d flag on when running a python script to enter debug mode. (sys.flags.debug will be True.)
python -d /Users/you/Desktop/sample.py
> setUpClass method is called.
> setUpClass : heavy method
> setUp method is called.
> tearDown method is called.
.
> setUp method is called.
F
> tearDown method is called.
> tearDownClass method is called.
> tearDownClass : released
======================================================================
FAIL: test_poyo (__main__.SampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/you/Desktop/sample.py", line 54, in test_poyo
self.assertEqual(expected, actual)
AssertionError: 'poyo' != 'hoge'
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
Output when not in debug mode
python /Users/you/Desktop/sample.py
.F
======================================================================
FAIL: test_poyo (__main__.SampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/you/Desktop/sample.py", line 54, in test_poyo
self.assertEqual(expected, actual)
AssertionError: 'poyo' != 'hoge'
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=1)
Appendix
I don't think it's used much in small scripts, but if you need it, there is also an initialization method for each Module such as setUpModule.
Recommended Posts