1.unittest组件
-
unittest 测试用例,方法命名基于test开头; 测试用例自行排序执行顺序规则:A-Z,a-z,0-9,eg:test01/test02; add的用力不是用test开始就不会被识别,所以不会允许; -
组件1 设置前置条件setUp; 后置条件tearDown; 每条测试用例方法执行前后都要执行前,后置条件 -
eg:Python_unitest/test_1.py
import unittest
class Test(unittest.TestCase):
def setUp(self) -> None:
print("打开浏览器")
def test1(self):
'''wangji 测试函数注释'''
print('case 1')
def test2(self):
print('case 2')
def tearDown(self) -> None:
print('关闭浏览器')
if __name__ == '__main__':
unittest.main()
结果:
[root@node-1 wangji]
打开浏览器
case 1
关闭浏览器
.打开浏览器
case 2
关闭浏览器
.
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
- 组件2
前置setUpClass; 后置tearDownClass; 所有用力执行前只会执行一次前置和后置; 方法前必须加装饰器:@classmethod - eg:Python_unitest/test_2.py
import unittest
class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("打开浏览器")
def test1(self):
print('case 1')
def test2(self):
print('case 2')
@classmethod
def tearDownClass(cls):
print('关闭浏览器')
if __name__ == '__main__':
unittest.main()
[root@node-1 wangji]
打开浏览器
case 1
.case 2
.关闭浏览器
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
2.Unittest套件-执行指定用例
-
执行指定单条用例 unittest.TestSuite()创建测试套件实例对象; suit.addTest(类名(“用例”)); -
执行指定多条用例 通过addTests([…])添加多个测试用例 -
eg:Python_unitest/test_3.py
import unittest
class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("打开浏览器")
def test1(self):
print('case 1')
def test2(self):
print('case 2')
@classmethod
def tearDownClass(cls):
print('关闭浏览器')
if __name__ == '__main__':
suit = unittest.TestSuite()
suit.addTest(Test("test2"))
run = unittest.TextTestRunner()
run.run(suit)
[root@node-1 wangji]
打开浏览器
case 2
.关闭浏览器
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
3.Unittest套件-运行单个类中所有用例
- 运行类中用例
先通过unittest.TestSuit()创建测试套件实例对象; 再通过unittest.TestLoader()创建加载对象,加载测试用例类; 再通过addTest()添加; - eg:执行一个类,Python_unitest/test_5.py
import unittest
class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("打开浏览器")
def test1(self):
print('case 1')
def test2(self):
print('case 2')
@classmethod
def tearDownClass(cls):
print('关闭浏览器')
if __name__ == '__main__':
suit = unittest.TestSuite()
load = unittest.TestLoader()
suit.addTest(load.loadTestsFromTestCase(Test))
run = unittest.TextTestRunner()
run.run(suit)
- eg:执行二个类,/home/wangji/Python_unitest/test_6.py
import unittest
class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("打开浏览器")
def test1(self):
print('case 1')
def test2(self):
print('case 2')
@classmethod
def tearDownClass(cls):
print('关闭浏览器')
class Test2(unittest.TestCase):
def test3(self):
print('case 3')
def test4(self):
print('case 4')
if __name__ == '__main__':
load = unittest.TestLoader()
suit1 = load.loadTestsFromTestCase(Test)
suit2 = load.loadTestsFromTestCase(Test2)
suits = unittest.TestSuite([suit1, suit2])
run = unittest.TextTestRunner()
run.run(suits)
4.Unittest套件-批量运行用例
import unittest
test_dir = "/home/wangji/Python_unitest"
dis = unittest.defaultTestLoader.discover(test_dir, pattern="test*.py")
suit = unittest.TestSuite()
suit.addTest(dis)
run = unittest.TextTestRunner()
run.run(suit)
5.Unittest生成测试报告
- HTMLTestRunner下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html
- 下载后需要修改:
94行引入的名称要改,从 import StringIO修改成 import io 539行 self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer=io.StringIO() 631行 print >>sys.stderr, ‘\nTime Elapsed: %s’ % (self.stopTime-self.startTime)修改成print (sys.stderr, - ‘\nTime Elapsed: %s’ %(self.stopTime-self.startTime)) 642行,if not rmap.has_key(cls): 修改成 if not cls in rmap: 766行的uo = o.decode(‘latin-1’),修改成 uo=o 772行,把 ue = e.decode(‘latin-1’) 直接改成 ue = e 将HTMLTestRunner.py模块放到Linux位置:/usr/lib64/python3.6
>>> import sys
>>> sys.path
['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/local/lib64/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']
- eg:Python_unitest/main.py
import unittest
from unittest import suite
import HTMLTestRunner
test_dir = "/home/wangji/Python_unitest"
report_dir = "/home/wangji/Python_unitest/report/"
file = open(report_dir + "result.html", "wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=file, title="标题", description="报告的描述")
suite = unittest.defaultTestLoader.discover(test_dir, pattern="test*.py")
runner.run(suite)
- 测试:
6.Unittest断言
- 断言即测试用例执行结果和预期对比,符合就测试用例通过,反之不通过
- 只能再继承unittest.TestCase的类中使用这些方法
- 常见的几个断言方法
- eg:Python_unitest/test_7.py
import unittest
from selenium import webdriver
class Test(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Chrome()
self.driver.get("http://www.baidu.com")
self.title = self.driver.title
print(self.title)
return self.title
def test1(self):
self.assertEqual(self.setUp(), '百度一下')
if __name__ == '__main__':
unittest.main()
7.功能测试用例与Unittest
-
Execel用例编写模板 -
ddt:Data Driven Testing数据驱动,即:测试数据的参数化 ddt是第三方库,通过pipi install ddt下载; 主要用到:data,unpack,file_data; 装饰器:格式:@装饰名 -
常见数据驱动方式 ddt:yaml,excel,xmind,mysql(表中一条就代表一个用例); 工作场景:去读取文件中,获取所有数据;再用ddt *,unpack解析成具体的用例; -
ddt简介:DDT包含类的装饰器ddt,方法装饰器data ddt.ddt:装饰类,用于unittest.TestCase子类的类装饰器; ddt.data(value):添加到unittest.TestCase测试用例上的方法装饰器,作用:专门给测试用例导入数据的,value是直接操作的数据; @data(a,b)那么a和b各自作为参数使用,各运行一次用例,共执行2次用例; @data([a,b],[c,d]),那么[a,b]当成一个参数传入用例运行,共执行2次用例; ddt.unpack:传递的是复杂的数据结构时使用,主要用于拆分数据。比如使用元组或者列表,添加unpack之后,ddt会自动把元组或者列表中的值拆分成多个值,以此传给多个参数接收; -
eg:Python_unitest/test_8.py
'''
unittest
测试用例-TestCase
规则:一个用例就是一个函数,函数名必须以test开头
测试集
测试报告
测试环境处理
'''
import unittest
from selenium import webdriver
import time
from ddt import ddt,data,unpack
from read_data import read_data
@ddt
class Test(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
self.driver.get("http://ww.wangji.com")
def tearDown(self) -> None:
time.sleep(3)
self.driver.quit()
def test01(self):
'''test1'''
self.driver.find_element_by_name('accounts').send_keys('66')
self.driver.find_element_by_name('pwd').send_keys('11')
self.driver.find_element_by_xpath('/html/body/wangji').click()
exp_url = 'http://www.jiji.com'
act_url = self.driver.current_url
self.assertEqual(exp_url, act_url, msg='不一致')
def test02(self):
'''test2'''
self.driver.find_element_by_name('accounts').send_keys('666')
self.driver.find_element_by_name('pwd').send_keys('1111')
self.driver.find_element_by_xpath('/html/body/wangji').click()
exp_url = 'http://www.jiji.com'
act_url = self.driver.current_url
self.assertEqual(exp_url, act_url, msg='不一致')
@data(*[('useer1','passwd1'), ('useer2','passwd2')])
@unpack
def test03(self, username, pwd):
self.driver.find_element_by_name('accounts').send_keys(username)
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_xpath('/html/body/wangji').click()
exp_url = 'http://www.jiji.com'
act_url = self.driver.current_url
self.assertEqual(exp_url, act_url, msg='不一致')
@data(*read_data('login_data.xlsx'))
@unpack
def test04(self, username, pwd):
self.driver.find_element_by_name('accounts').send_keys(username)
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_xpath('/html/body/wangji').click()
exp_url = 'http://www.jiji.com'
act_url = self.driver.current_url
self.assertEqual(exp_url, act_url, msg='不一致')
if __name__ == '__main__':
unittest.main()
|