一、
unittest
测试框架
1
导入包
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import unittest
2
创建类
2.1
全局变量
driver=None
定义驱动器
放在模块(即文件中),不要放在类中
2.2
创建测试类
class
类名
(unittest.TestCase)
括号内表示继承
不能省略
2.3
创建初始化和还原环境的函数
setUpClass(cls)
必须使用
@classmethod
装饰器
cls
不能省略
所有测试函数运行前运行一次
tearDownClass(cls)
必须使用
@classmethod
装饰器
cls
不能省略
所有测试函数运行完后运行一次
setUp(self)
每个测试函数运行前运行
self
不能省略
tearDown(self)
每个测试函数运行完后执行
self
不能省略
说明
这些函数都放在类中。
2.4
测试函数
def
函数名
(self):
u'''
测试描述
'''
global driver
其他测试代码
函数名必须以
test
开头
self
不能省略
u"""
测试描述
"""
可以省略,省略时显示函数名
global driver
用于指定
driver
是全局变量,只放在
setUpClass
中即可
多个测试函数按照函数名的
ASCII
顺序执行
2.5
运行测试
if __name__ == "__main__":
unittest.main(verbosity=2)
上述代码放在模块内,类之外
__
是双下划线
if __name__ == "__main__"
表示当单独运行模块时才会被执行,
import
到其他
脚本中是不会被执行
verbosity=2
显示每个用例的详细信息
可以省略,但测试结果不详细
测试结果
.
代表测试通过。
F
代表测试失败,
F
代表
failure
。
E
代表测试出错,
E
代表
error
。
s
代表跳过该测试,
s
代表
skip
。
3
断言
assert
str1
in str2
,断言失败时的消息文本
assert
表达式
1==
表达式
2
self.assertIn(str1,str2)
断言
str1
是否在
str
中
self.assertEqual(
实际,预期
)
4
参数化
4.1
安装
nose_parameterized
pip install …
4.2
导入包
parameterized
包名不是
nose_parameterized
import parameterized
4.3
定义参数数据
data=[ [
数据
11
,数据
12 ] ,
[
数据
21
,数据
22 ]
]
参数放在列表中
4.4
引用参数
@parameterized.parameterized.expand(data)
def test…(self ,
参数
1,
参数
2,…):
self
不能省略
参数的个数应与列表中的个数一致
可以实现局部迭代
5
测试套件
批量运行测试套件中的测试用例。
5.1
指定运行一个模块中的一个测试
suite=unittest.TestSuite()
创建测试套件(测试用例的集合,测试容器)
suite.addTest(
类名
("
测试函数名
"))
将一个测试用例到测试套件中
不能省略类名
runner=unittest.TextTestRunner()
指定使用
TextTestRunner
运行测试用例
runner.run(suite)
运行测试套件
5.2
指定按顺序运行一个模块中的多个测试
suite=unittest.TestSuite()
tests=[
类名
("
测试函数
1") ,
类名
("
测试函数
2") , …]
只执行指定的测试函数
按照编写的顺序执行测试
多个测试放到列表中
不能省略类名
若一个测试的执行依赖于其他测试,可以使用此方式解决
suite.addTests(tests)
将测试添加到测试套件中
runner=unittest.TextTestRunner(verbosity=2)
可以指定测试结果的详细程度
runner.run(suite)
5.3
自动发现多个测试
创建多个测试模块
在模块内编写测试类和测试函数
最好创建运行测试套件的单独的模块
创建类与否,均可
创建测试套件
suite=unittest.TestSuite()
指定测试用例的识别规则
tests=unittest.defaultTestLoader.discover("
测 试
模
块
所
在
目
录
",
pattern='test*.py')
识别所有
test
开头的
py
文件为测试用例
按模块名称顺序执行
suite.addTests(tests)
runner=unittest.TextTestRunner(verbosity=2)
runner.run(suite)
6
测试报告
测试报告可以生成多种形式,如
txt
、
html
、
xml
等。
最好创建运行测试套件的单独的模块
导入包
import HTMLTestRunner
可以自行修改此文件
创建测试套件
suite=unittest.TestSuite()
tests=unittest.defaultTestLoader.discover("
测 试
模
块
所
在
目
录
",
pattern='test_*.py')
suite.addTests(tests)
运行测试,保存报告
now = time.strftime("%Y%m%d %H%M%S",time.localtime())
获取当前时间并设置时间格式
reportFile="./"+now+"_result.html"
测试报告的存放路径及文件名,文件名中加了当前时间以便每次生成
不同的测试报告
fp=open(reportFile,'wb')
runner=HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'
报告标题功
能测试报告
', description=u'
报告的说明与描述
', tester=u'
测试员姓名
')
runner.run(suite)
fp.close()
关闭文件流,不关的话生成的报告是空的
二、
pytest
测试框架
1
pytest
简介与安装
1.1 pytest
简介
pytest
是一个非常成熟的全功能的
Python
测试框架,主要特点:
简单灵活,容易上手,文档丰富;
支持参数化,可以细粒度地控制要测试的测试用例;
能够支持简单的单元测试和复杂的功能测试,还可以用来做
selenium/appnium
等自动化测试、接口自动化测试(
pytest+requests
)
;
pytest
具有很多第三方插件,并且可以自定义扩展。
1.2 pytest
的命名规则
测试模块名建议使用
test_
开头或
_test
结尾
测试类必须以
test
开头或结尾
测试函数必须以
test
开头或结尾
1.3
安装
pytest
pip install --no-index --find-links=
所有依赖包包位置
-r requirement.txt
安装完成后,
python
→
import pytest
不报错说明安装成功
2
编写测试脚本
2.1
编写测试类和测试函数
class
类名
():
@fixture(scope=?)
def
初始化函数名
(
必须写
self
或
cls):
代码
def
测试函数名
(
必须写
self
或
cls ,
初始化函数名
):
代码
类不需要继承
fixture
的功能
传入测试中的数据集
配置测试前系统的初始状态
为批量测试提供数据源
scope=?
scope="function"
每个
test
都运行。
scope="class"
每个
class
的所有
test
只运行一次
scope="module"
每个
module
的所有
test
只运行一次
scope="session"
每个
session
只运行一次
初始化函数名被
@fixture
修饰,不带括号,根据实际情况添加
2.2
运行测试
IDLE
界面中启动
if __name__=='__main__':
pytest.main(['-s','
模块名
.py'])
命令行中运行测试
推荐此种方式
pytest
参数
py
文件名
按照类中测试函数编写的先后顺序执行。
允许省略参数
-v
用于显示每个测试函数的执行结果
-q
只显示整体测试结果
-s
用于显示测试函数中
print()
函数输出
2.3
断言
assert
表达式,断言失败消息文本
2.4
参数化
@pytest.mark.parametrize("
参数
1 ,
参数
2" ,
[ ['
数据
11' , '
数据
12' ] ,
['
数据
21' , '
数据
22' ]
])
def
测试函数名
(self
或
cls ,
参数
1
,参数
2):
代码
3
批量测试
3.1
运行一个指定的测试函数
pytest
模块名
.py::
类名
::
函数名
pytest
-k keyword
模块名
.py
运行函数名中带有
keyword
关键字的测试函数
3.2
运行多个指定的测试
pytest
模块名
.py::
类名
::
函数名
1
,模块名
.py::
类名
::
函数名
2
,
…
pytest
-k "keyword1 or keyword2"
模块名
.py
运行函数名中带有
keyword1
或
keyword2
关键字的测试函数
3.3
运行一个类中的所有测试
pytest
测试模块名
.py
3.3
运行目录中的的所有测试
当前目录
pytest
只运行符合命名规则的测试
指定目录
pytest
目录
只运行符合命名规则的测试
4
测试报告
需要先安装
pytest-html
无需导包
pytest
模块名
.py --html=report.html --self-contained-html
直接
html
独立显示
美化报告
复制
conftest.py
到测试模块目录
可自定义此文件