当我们有多个测试脚本时,使用python -m unittest会搜索当前目录运行所有脚本,我们建造多个文件,使用不同的测试方法。
'''jisuanqi.py'''
def jiafa(a,b):
if type(a) is str or type(b) is str:
return str(a) + str(b)
return a+b
def chengfa(a,b):
if type(a) is str or type(b) is str:
return 0
return a*b
'''testjiafa.py'''
import unittest
import ddt
from jisuanqi import *
testdata = [{'a':1,'b':1,'jiafa':2,'chengfa':1},
{'a':'a','b':1,'jiafa':'a1','chengfa':'a'},
{'a':'a','b':'b','jiafa':'ab','chengfa':'ab'}]
@ddt.ddt
class TestMath(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
@ddt.data(*testdata)
def test_jiafa(self,data):
'''测试加法程序'''
try:
self.assertEqual(jiafa(data['a'],data['b']),data['jiafa'])
print(str(data['a']) + '+' + str(data['b']) + '=' + str(data['jiafa']))
except Exception as e:
print(str(data['a']) + '+' + str(data['b']) + '=' + str(data['jiafa'] + ' X'))
raise e
if __name__ == '__main__':
unittest.main()
'''testchengfa.py'''
import unittest
import ddt
from jisuanqi import *
testdata = [{'a':1,'b':1,'jiafa':2,'chengfa':1},
{'a':'a','b':1,'jiafa':'a1','chengfa':'a'},
{'a':'a','b':'b','jiafa':'ab','chengfa':'ab'}]
@ddt.ddt
class TestMath(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
@ddt.data(*testdata)
def test_chengfa(self,data):
'''测试乘法程序'''
try:
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
print(str(data['a']) + '*' + str(data['b']) + '=' + str(data['chengfa']))
except Exception as e:
print(str(data['a']) + '*' + str(data['b']) + '=' + str(data['chengfa'] + ' X'))
raise e
if __name__ == '__main__':
unittest.main()
在同一目录创建runner.py
import unittest
testsuite = unittest.TestLoader().discover('./')
runner = unittest.TextTestRunner()
runner.run(testsuite)
相当于python -m unittest discover
通过从指定的起始目录递归到子目录中,找到所有测试模块,然后返回包含它们的TestSuite对象。
运行查看结果
.FF...
======================================================================
FAIL: test_chengfa_2 (testchengfa.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\unitest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\unitest\testchengfa.py", line 27, in test_chengfa
raise e
File "D:\PycharmProjects\unitest\testchengfa.py", line 23, in test_chengfa
self.assertEqual(chengfa(data['a'], data['b']), data['chengfa'])
AssertionError: 0 != 'a'
======================================================================
FAIL: test_chengfa_3 (testchengfa.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\unitest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\unitest\testchengfa.py", line 27, in test_chengfa
raise e
File "D:\PycharmProjects\unitest\testchengfa.py", line 23, in test_chengfa
self.assertEqual(chengfa(data['a'], data['b']), data['chengfa'])
AssertionError: 0 != 'ab'
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=2)
1*1=1
a*1=a X
a*b=ab X
1+1=2
a+1=a1
a+b=ab
Process finished with exit code 0
python -m unittest
与 python -m unittest discover
等价。如果你需要向探索性测试传入参数,必须显式地使用 discover
子命令。
discover参数:
-v
,
--verbose
更详细地输出结果。
(venv) D:\PycharmProjects\unitest>python -m unittest discover -v
test_chengfa_1 (testchengfa.TestChengfa)
测试乘法程序 ... 1*1=1
ok
test_chengfa_2 (testchengfa.TestChengfa)
测试乘法程序 ... a*1=a X
FAIL
test_chengfa_3 (testchengfa.TestChengfa)
测试乘法程序 ... a*b=ab X
FAIL
test_jiafa_1 (testjiafa.TestJiafa)
测试加法程序 ... 1+1=2
ok
test_jiafa_2 (testjiafa.TestJiafa)
测试加法程序 ... a+1=a1
ok
test_jiafa_3 (testjiafa.TestJiafa)
测试加法程序 ... a+b=ab
ok
======================================================================
FAIL: test_chengfa_2 (testchengfa.TestChengfa)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\unitest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\unitest\testchengfa.py", line 27, in test_chengfa
raise e
File "D:\PycharmProjects\unitest\testchengfa.py", line 23, in test_chengfa
self.assertEqual(chengfa(data['a'], data['b']), data['chengfa'])
AssertionError: 0 != 'a'
======================================================================
FAIL: test_chengfa_3 (testchengfa.TestChengfa)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\unitest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\unitest\testchengfa.py", line 27, in test_chengfa
raise e
File "D:\PycharmProjects\unitest\testchengfa.py", line 23, in test_chengfa
self.assertEqual(chengfa(data['a'], data['b']), data['chengfa'])
AssertionError: 0 != 'ab'
----------------------------------------------------------------------
Ran 6 tests in 0.006s
FAILED (failures=2)
-s
,
--start-directory
开始进行搜索的目录(默认值为当前目录 .
)
-p
,
--pattern
用于匹配测试文件的模式(默认为 test*.py
)