unittest
模块提供了一系列创建和运行测试的工具。这一段落演示了这些工具的一小部分,但也足以满足大部分用户的需求。
首先我们为了学习写一个简单的逻辑,代码如下:
def add(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
为了测试3个函数是否正确,我们利用unittest进行测试。
import unittest
import ddt
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
@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()
一个简单的测试脚本完成了,使用pycharm的话点击右上角的绿色运行按钮即可运行。直接运行看下结果:
1*1=1
a*1=a X
a*b=ab X
1+1=2
a+1=a1
a+b=ab
.FF...
======================================================================
FAIL: test_chengfa_2 (__main__.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:/PycharmProjects/seleniumtest/testmath.py", line 46, in test_chengfa
raise e
File "D:/PycharmProjects/seleniumtest/testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
AssertionError: 0 != 'a'
======================================================================
FAIL: test_chengfa_3 (__main__.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:/PycharmProjects/seleniumtest/testmath.py", line 46, in test_chengfa
raise e
File "D:/PycharmProjects/seleniumtest/testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
AssertionError: 0 != 'ab'
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=2)
我们发现第2个方法先运行,这是因为使用main或命令行运行时,加载器按照方法名进行排序。
unittest.main()
提供了一个测试脚本的命令行接口。unittest 模块可以通过命令行运行模块、类和独立测试方法的测试:
python -m unittest test_module1 test_module2 # python -m unittest test_module.TestClass python -m unittest test_module.TestClass.test_method python -m unittest tests/test_something.py
用于获取命令行选项列表:
python -m unittest -h
因为是对测试框架学习所以我们看下运行脚本时可以用到的参数。
运行参数
-k
只运行匹配模式或子串的测试方法和类。可以多次使用这个选项,以便包含匹配子串的所有测试用例。可以使用通配符(*)的模式对测试名称进行匹配。另外,该匹配是大小写敏感的。
python -m unittest newtest.py -k jiafa
当为true时,当出现第一个错误或者失败时,停止运行测试。
如果为True,在测试运行时,标准输出流与标准错误流会被放入缓冲区。成功的测试的运行时输出会被丢弃;测试不通过时,测试运行中的输出会正常显示,错误会被加入到测试失败信息。
加入参数-b运行
(venv) D:\PycharmProjects\seleniumtest>python -m unittest testmath.py -b
.F
Stdout:
a*1=a X
F
Stdout:
a*b=ab X
...
======================================================================
FAIL: test_chengfa_2 (testmath.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\seleniumtest\testmath.py", line 46, in test_chengfa
raise e
File "D:\PycharmProjects\seleniumtest\testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
AssertionError: 0 != 'a'
Stdout:
a*1=a X
======================================================================
FAIL: test_chengfa_3 (testmath.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\seleniumtest\testmath.py", line 46, in test_chengfa
raise e
File "D:\PycharmProjects\seleniumtest\testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
AssertionError: 0 != 'ab'
Stdout:
a*b=ab X
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=2)
只在错误时进行打印
'verbose'
0 不打印描述及任何成功失败标志,相当于命令行的-q
1 不打印描述只打印失败标志,默认
2 打印描述内容并打印成功及失败标志,相当于命令行的-v
加参数-v 相当于unittest.main(verbosity = 1)运行
(venv) D:\PycharmProjects\seleniumtest>python -m unittest testmath.py -v
test_chengfa_1 (testmath.TestMath)
测试乘法程序 ... 1*1=1
ok
test_chengfa_2 (testmath.TestMath)
测试乘法程序 ... a*1=a X
FAIL
test_chengfa_3 (testmath.TestMath)
测试乘法程序 ... a*b=ab X
FAIL
test_jiafa_1 (testmath.TestMath)
测试加法程序 ... 1+1=2
ok
test_jiafa_2 (testmath.TestMath)
测试加法程序 ... a+1=a1
ok
test_jiafa_3 (testmath.TestMath)
测试加法程序 ... a+b=ab
ok
======================================================================
FAIL: test_chengfa_2 (testmath.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\seleniumtest\testmath.py", line 46, in test_chengfa
raise e
File "D:\PycharmProjects\seleniumtest\testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
AssertionError: 0 != 'a'
======================================================================
FAIL: test_chengfa_3 (testmath.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\seleniumtest\testmath.py", line 46, in test_chengfa
raise e
File "D:\PycharmProjects\seleniumtest\testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
AssertionError: 0 != 'ab'
----------------------------------------------------------------------
Ran 6 tests in 0.005s
FAILED (failures=2)
加参数-q 相当于unittest.main(verbosity = 0)运行
(venv) D:\PycharmProjects\seleniumtest>python -m unittest testmath.py -q
1*1=1
a*1=a X
a*b=ab X
1+1=2
a+1=a1
a+b=ab
======================================================================
FAIL: test_chengfa_2 (testmath.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\seleniumtest\testmath.py", line 46, in test_chengfa
raise e
File "D:\PycharmProjects\seleniumtest\testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
AssertionError: 0 != 'a'
======================================================================
FAIL: test_chengfa_3 (testmath.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "D:\PycharmProjects\seleniumtest\testmath.py", line 46, in test_chengfa
raise e
File "D:\PycharmProjects\seleniumtest\testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
AssertionError: 0 != 'ab'
----------------------------------------------------------------------
Ran 6 tests in 0.003s
FAILED (failures=2)
-W --warnings
“error” | 将警告转换为异常
“ignore” | 不会打印匹配的警告
“always” | 总是打印匹配的警告
“default” | 打印发出警告的每个位置的首次出现的匹配警告
“module” | 将为发出警告的每个模块打印首次发生的匹配警告
“once” | 仅打印第一次匹配的警告,不管位置如何
默认为default
在回溯中显示局部变量。
python -m unittest newtest.py --locals
(venv) D:\PycharmProjects\seleniumtest>python -m unittest testmath.py --locals
1*1=1
.a*1=a X
Fa*b=ab X
F1+1=2
.a+1=a1
.a+b=ab
.
======================================================================
FAIL: test_chengfa_2 (testmath.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
args = ({'a': 'a', 'b': 1, 'jiafa': 'a1', 'chengfa': 'a'},)
func = <function TestMath.test_chengfa at 0x035C9A08>
kwargs = {}
self = <testmath.TestMath testMethod=test_chengfa_2>
File "D:\PycharmProjects\seleniumtest\testmath.py", line 46, in test_chengfa
raise e
data = {'a': 'a', 'b': 1, 'jiafa': 'a1', 'chengfa': 'a'}
self = <testmath.TestMath testMethod=test_chengfa_2>
File "D:\PycharmProjects\seleniumtest\testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
data = {'a': 'a', 'b': 1, 'jiafa': 'a1', 'chengfa': 'a'}
self = <testmath.TestMath testMethod=test_chengfa_2>
AssertionError: 0 != 'a'
======================================================================
FAIL: test_chengfa_3 (testmath.TestMath)
测试乘法程序
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\PycharmProjects\seleniumtest\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
args = ({'a': 'a', 'b': 'b', 'jiafa': 'ab', 'chengfa': 'ab'},)
func = <function TestMath.test_chengfa at 0x035C9A08>
kwargs = {}
self = <testmath.TestMath testMethod=test_chengfa_3>
File "D:\PycharmProjects\seleniumtest\testmath.py", line 46, in test_chengfa
raise e
data = {'a': 'a', 'b': 'b', 'jiafa': 'ab', 'chengfa': 'ab'}
self = <testmath.TestMath testMethod=test_chengfa_3>
File "D:\PycharmProjects\seleniumtest\testmath.py", line 42, in test_chengfa
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
data = {'a': 'a', 'b': 'b', 'jiafa': 'ab', 'chengfa': 'ab'}
self = <testmath.TestMath testMethod=test_chengfa_3>
AssertionError: 0 != 'ab'
----------------------------------------------------------------------
Ran 6 tests in 0.004s
FAILED (failures=2)