我们在您的 wrkDir 中,您的 pythonFunction.py 所在的位置,您的 foo(input), d dict 所在的位置。在那个 wrkDir 中创建文件 tests.py:
import unittest
class TestPythonFunction(unittest.TestCase):
# define here all required cases
ALL_DICT_REQUIREMENTS_ =(
('a', 1),
('b', 2),
('c', 3)
)
def test_dictFoo(self):
# pythonFunction reflects to your pythonFunction.py
from pythonFunction import foo
# test all required patterns
for pattern in TestPythonFunction.ALL_DICT_REQUIREMENTS_:
key = pattern[0]
value = pattern[1]
self.assertIs(foo(key), value)
# test function is raising error if key doesn't exists
with self.assertRaises((KeyError)):
foo('nonsenseKey')
if __name__ == '__main__':
unittest.main()
在 wrkDir 中运行
python tests.py
预期输出:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
tests.py 退出代码为 0(所有测试成功),如果某些测试失败,则为无 0,因此您可以相应地控制 CI 任务,
python tests.py && git push || echo "Bad, check tests for error"