python的unittest好处是通过python脚本编写用例,每个用例可以单独调试初始化和清理动作,因为都是用例都是代码所以调试起来也很方便;它的缺点是得先学会python,难易程度见仁见智吧,对于我来说那些用excel来写测试用例的框架,我看到里面的用例头都大,还不如看代码来的简单呢。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
os,sys
import
time
import
datetime
import
unittest
class
NginxTest(unittest.TestCase):
def
setUp(
self
):
#如果不需要每个case都预置和清理环境,而是每个class用一次,只需要用setUpClass、tearDownClass代替即可,如果是整个文件只需要用一次,则用要用 setUpModule() 和 tearDownModule() 这两个函数了,注意是函数,与 TestCase 类同级
#预置环境
print
'--------------NginxTestSetUp--------------\n'
def
tearDown(
self
):
#清理环境
print
'--------------NginxTestClear--------------\n'
def
test_nginx(
self
):
print
'test_nginx'
def
test_nginxlog(
self
):
print
'test_nginxlog'
@unittest
.skip(
"must skipping"
)
#必须跳过下面用例,相当少用
def
test_mustskip(
self
):
print
'test_mustskip'
# def test_1(self):
# a=1
# return 1
@unittest
.skipUnless(sys.platform.startswith(
"win"
),
"requires Windows"
)
#根据条件跳过下面这个用例
def
test_maybeskip(
self
):
print
'test_maybeskip'
def
suite_1(
self
):
#非test开头的用例在NginxTest中不会被跑到
print
'suite_1'
def
suite_2(
self
):
print
'suite_2'
class
PhpTest(unittest.TestCase):
#因为每个接口的预置环境可能不一样,所以每个接口的用例应该都用单独class来包含,不过每个class的用例都还是要用test开头
def
setUp(
self
):
#预置环境
print
'--------------PhpTestSetUp--------------\n'
def
tearDown(
self
):
#清理环境
print
'--------------PhpTestClear--------------\n'
def
test_php(
self
):
print
'test_php'
def
test_phplog(
self
):
print
'test_phplog'
def
suite():
#这个表示测试集,不要放在class内,否则会提示"没有这样的测试方法在
suite
=
unittest.TestSuite()
suite.addTest(NginxTest(
"suite_1"
))
suite.addTest(NginxTest(
"suite_2"
))
suite.addTest(PhpTest(
"test_php"
))
suite.addTest(PhpTest(
"test_phplog"
))
unittest.TextTestRunner().run(suite)
if
__name__
=
=
'__main__'
:
# unittest.main(exit = False,verbosity=2)#它是全局方法,把它屏蔽后,不在suite的用例就不会跑,exit = False表示中间有用例失败也继续执行;还有比较常用的verbosity=2,表示显示def名字
suite()
#执行suite
|
如果接口相当多,为了方便维护,建议每个case用独立的py来写,然后用一个“总入口”去import所有py,然后再调用就行了,这是suite就派上用场了
举个例子,luatestcase.py如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import time import datetime import unittest class LuaTest(unittest.TestCase): def runTest(self): print 'anything' def setUp(self): #预置环境 print '--------------LuaTestsetUp--------------\n' def tearDown(self): #清理环境 print '--------------LuaTestclear--------------\n' def test_lua(self): print 'test_lua' def test_lualog(self): print 'test_lualog' def casesuite(): suite = unittest.TestSuite() suite.addTest(LuaTest("test_lua")) suite.addTest(LuaTest("test_lualog")) unittest.TextTestRunner().run(suite)
调用luatestcase.py的“总入口py”就得这样写:
#!/usr/bin/env python # -*- coding: utf-8 -*- import os,sys import time import datetime import unittest import luatestcase if __name__ == '__main__': luatestcase.casesuite()
如果luatestcase.py没有用casesuite“收集”它的用例的话,总入口调不了里面的用例的,不知道是不是我的用法有问题,如果有朋友知道欢迎指点。有的文章说用discover可以实现同样的需求,不过我不会。。。
如果要保存unitest的测试输出日志,则需要用到TextTestRunner,例子如下
1
2
3
4
5
|
log_file
=
"log_file.txt"
f
=
open
(log_file,
"w"
)
runner
=
unittest.TextTestRunner(stream
=
f,verbosity
=
2
)
unittest.main(exit
=
False
,testRunner
=
runner)
f.close()
|
补充一个方法:每个接口的测试用例按照普通的unittest格式来写,放到统一的目录中,然后用一个总的py去遍历这些unittest文件,然后用os.popen打开,把这些用例的测试结果read下来保存到一个总的log文件中就行了。
最后说一下,我最常用的方法是assertEqual、assertNotEqual、assertTrue。