在使用python写自动化的时候,选择使用了unittest运行测试用例,在此记录使用unittest的常规用法
参考1:http://www.cnblogs.com/idbeta/archive/2015/12/11/5040187.html
参考2:http://www.2cto.com/kf/201409/330721.html
初始化环境
dome1:
每次执行用例时都会: setUp->test_case->tearDown ;也就是说可以使用setUp和tearDown做每个用例的环境初始化
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
class MyTestCase(unittest.TestCase):
# 预置环境
def setUp(self):
print '开始执行'
# 清理环境
def tearDown(self):
print '结束执行'
# 测试用例
def test_case1(self):
print '用例1'
# 测试用例
def test_case2(self):
print '用例2'
if __name__ == '__main__':
unittest.main()
每个class用一次,setUpClass->test_case1->test_case2->tearDownClass;也就是说使用setUpClass、tearDownClass可以做所有用例的前置初始化和结束
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
class MyTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
print '开始执行'
@classmethod
def tearDownClass(cls):
print '结束执行'
# 测试用例
def test_case1(self):
print '用例1'
# 测试用例
def test_case2(self):
print '用例2'
if __name__ == '__main__':
unittest.main()
dome3
setUp,tearDown,setUpClass,tearDownClass可以组合使用也可单独使用
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
class MyTestCase(unittest.TestCase):
# @classmethod
# def setUpClass(cls):
# print '首先开始执行'
@classmethod
def tearDownClass(cls):
print '最后结束执行'
def setUp(self):
print '每次首先执行1'
# def tearDown(self):
# print '每次结束执行1'
# 测试用例
def test_case1(self):
print '用例1'
# 测试用例
def test_case2(self):
print '用例2'
if __name__ == '__main__':
unittest.main()
dome4:
整个文件使用一次,主要是针对一个文件中存在多个class
setUpModule()->class1->setUpClass->setUp->test_case->tearDown->tearDownClass->class2...->tearDownModule()
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
def setUpModule():
print '1setUpModule'
def tearDownModule():
print '1tearDownModule'
class MyTestCase1(unittest.TestCase):
@classmethod
def setUpClass(cls):
print"1setUpClass"
@classmethod
def tearDownClass(cls):
print"1tearDownClass"
def setUp(self):
print '1setUp'
def tearDown(self):
print '1tearDown'
def test_case1(self):
print '1test_case1'
def test_case2(self):
print '1test_case2'
class MyTestCase2(unittest.TestCase):
@classmethod
def setUpClass(cls):
print"2setUpClass"
@classmethod
def tearDownClass(cls):
print"2tearDownClass"
def setUp(self):
print '2setUp'
def tearDown(self):
print '2tearDown'
def test_case1(self):
print '2test_case1'
def test_case2(self):
print '2test_case2'
if __name__ == '__main__':
unittest.main()
unittest suite加载testcase的几种方法
dome1
def suite():
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase('test_default_size'))
suite.addTest(WidgetTestCase('test_resize'))
return suite
def suite():
tests = ['test_default_size', 'test_resize']
return unittest.TestSuite(map(WidgetTestCase, tests))
多个测试套件构建成更大的测试套件
suite1 = module1.TheTestSuite()
suite2 = module2.TheTestSuite()
alltests = unittest.TestSuite([suite1, suite2])
默认的测试套件
suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
dome5
按给定的文件名称执行测试用例
test_dir = './'
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py')
构造测试用例集
def suite1():
# 构造测试用例集
suite = unittest.TestSuite()
# 加载测试用例
suite.addTest(test_class.TestCases('test_baidu_search'))
suite.addTest(test_class.TestCases('test_search2'))
suite.addTest(test_class.TestCases('test_get_title'))
return suite
def suites():
# 构造测试用例集
t_suite = unittest.TestSuite()
# 加载一个类文件下所有的测试用例
suite = t_suite(unittest.makeSuite(test_class.TestCases))
return suite
def suite2():
# 测试用例存放目录
test_dir = './'
# 加载test_dir路径下所有以test_开头.py结尾的文件中的测试用例
suite = unittest.TestLoader().discover(test_dir, pattern='test_*.py')
return suite