ユニットテストのテンプレートです。
・testedpy.py はテストされる関数のあるpy
・unittest_py.py はユニットテストをする側
この2ファイルは同じディレクトリにある。
実行は
python3 unittest_py.py -v
-v で verbose
---
testedpy.py
# required to use in function
class OutOfRangeError(ValueError):
pass
# if needed, write variable used in tested_func
# input n, return result
# Test result is as expected.
# Test n is as expected.
def tested_func(n):
"""what this func does"""
if XXXX == XXXX: # condition with n
raise OutOfRangeError("about input n, why this func fail and what should be")
result = ""
"""some calculation"""
return result
unittest_py.py
"""
class arg MUST be (unittest.Testcase)
function name MUST start from test_...
self.assertRaises() for bad input
self.assertEqual() for expected results
unittest use .py name and funcname to get expected result
result = testedpy.tested_func(integer)
"""
import unittest
import testedpy # .py to be tested
# Test n for testedpy.tested_func is as expected.
class XxxxBadInput(unittest.TestCase):
def test_xxxx(self):
"""explanation why this test fail"""
self.assertRaises(testedpy.OutOfRangeError, testedpy.tested_func, xxxx)
# Test result from testedpy.tested_func(n) is as expected.
class KnownValues(unittest.TestCase):
known_values = (
"""(integer, result_known)"""
)
def test_tested_func_known_values(self):
"""tested_func should give known result with known input"""
for integer, result_known in self.known_values:
result_from_func = testedpy.tested_func(integer)
self.assertEqual(result_known, result_from_func)
if __name__ == "__main__":
unittest.main()
0 件のコメント:
コメントを投稿