Pytest测试框架
测试用例的识别和运行
测试文件: test_*.py
或者*_test.py
用例识别: Test*
类包含的所有test_*
的方法,测试类不带有__init__
方法,不在class
中的所有test_*
方法
用例显示: pytest
也可以执行unittest
框架写的用例和方法
运行测试文件
1.终端输入pytest 测试文件相对路径名
test_hello.py文件
# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
执行结果
2.修改测试文件,使用python解释器运行
import pytest
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
class TestDemo:
def test_a(self):
print("a")
def test_b(self):
print("b")
if __name__ == '__main__':
pytest.main(['pytest_learn/test_hello.py::TestDemo', '-v'])
3.一些选项设置的介绍
(1)-v
详细打印日志信息
使用pytest 文件名 -v
或者 pytest.main(['pytest_learn/test_hello.py', '-v'])
,也可以利用 pytest.main(['pytest_learn/test_hello.py::testDemo', '-v'])
实现限制测试范围的测试。
(2) -k
使用特定的测试用例测试
pytest 测试文件名 -k 测试用例函数名 -v
注意这里是一个贪婪匹配,也就是测试用例函数名完全一致或者以此开头的都会纳入考虑
(4) 装饰器修饰函数在特定的函数执行
@pytest.fixture()
def login():
username = "JERRY"
return username
class TestDemo:
def test_a(self, login):
print(f"a, username = {login}")
参数化实例详细介绍
@pytest.mark.parametrize(argnames, argvalues)
argnames:要参数化的变量,string(逗号分割), list, tuple(不同的数据结构拥有的方法不同)
argvalues:参数化的值,list, list[tuple],如果传入的是字典,只会传递key值
1.代码中直接输入测试用例
import pytest
class TestData:
@pytest.mark.parametrize(("a", "b"), [(10,20), (10,5), (3,9)])
def test_data(self, a, b):
print(a + b)
2.使用yaml
首先编写data.yml
文件
-
- 10
- 20
-
- 30
- 40
-
- 5
- 25
在代码中导入数据
import pytest
import yaml
class TestData:
@pytest.mark.parametrize(("a", "b"), yaml.safe_load(open("pytest_learn\data.yaml")))
def test_data(self, a, b):
print(a + b)
测试报告定制
allure介绍
Allure框架是一种灵活的轻量级多语言测试报告工具,它不仅能够以简洁的web报告形式显示已测试的内容,而且允许参与开发过程的每个人从测试的日常执行中提取最大限度的有用信息。
1.安装allure
allure下载
找到allure.bat双击运行
2.配置allure系统环境变量
将allure中的bin文件路径放到Path目录下面
3.cmd窗口验证配置
打印这么多东西说明安装成功了
4. 简单使用allure
安装pytest-allure
,编写测试用例代码
import pytest
def test_success():
assert true
def test_failure():
assert False
def test_skip():
pytest.skip("for a reason!")
def test_broken():
raise Exception("oops")
pytest
编译对应的代码文件(pytest/test_allure.py
)并且输出道指定文件夹(result/1
)下
pytest pytest_learn/test_allure.py --alluredir=result/1
直接输出日志报告在线查看:allure serve result/1
从结果中生成报告,这是一个启动tomcat
服务,两个步骤
生成报告:allure generate result/1 -o report/ --clean
打开报告:allure open -h 127.0.0.1 -p 8883 report/
allure常用特性
1. 使用较多的装饰器
场景:希望在报告中看到测试功能,子功能或者场景,测试步骤,包括测试附加信息
解决:@Feature, @story, @step, @attach
步骤:
导入包:import allure
功能上加上@allure.feature('功能名称')
子功能加上@allure.story('子功能名称')
步骤上加上@allure.step('步骤细节')
2.常见的特性
@allure.attach('具体文本信息')
,需要附加的信息,可以是数据、文本、图片
pytest 文件名 --allure-features '购物车功能' (--allure-stories '加入购物车')
限制运行某个模块或者某个功能
@allure.link(href, name)
,附加的链接信息,href输入对应的网址,name输入对应的网址名字
@allure.issue(id, msg)
添加bug相关的说明,如果指定这个,必须在运行阶段指定连接地址--allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
id会自动填入到{}对应位置(官方文档是这么写的)
但是在vscode下面编写对应测试文件test_allure_demo.py
import allure
@allure.link("www.baidu.com")
def test_with_link():
print("这是一条加了连接的测试")
pass
TEST_CASE_LINK = "https://leetcode.cn"
@allure.testcase(TEST_CASE_LINK, '登陆用例')
def test_with_testcase():
print("这是一条测试用例的连接")
pass
@allure.issue('140', "这是一个issue")
def test_with_issue_link():
pass
然后使用pytest pytest_learn\test_allure_demo.py --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{} --alluredir=result/5
,会报错ERROR: -o/--override-ini expects option=value style (got: 'utputFormat')
,
仔细看这个错误是叫我应该传入一个具体的值,然后将上述命令转换为pytest pytest_learn\test_allure_demo.py --allure-link-pattern='issue:http://www.mytesttracker.com/issue/{}' --alluredir=result/5
,问题得到解决
按照重要性级别进行一定范围性的测试
场景:通常测试有P0、冒烟测试、验证上线测试
解决:
附加pytest.mark
标记
通过allure.feature, allure.story
也可以通过allure.severity
标记五个级别:Trivial不重要, Minor不太重要, Normal正常, Critical严重, Blocker阻塞
步骤:
在方法和类上面添加说明执行级别为trivial
@allure.severity(allure.severity_level.TRIVIAL)
import allure
@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
pass
@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
pass
@allure.severity(allure.severity_level.NORMAL)
class TestClassWithNormalSeverity(object):
def test_inside_the_normal_severity_test_class(self):
pass
@allure.severity(allure.severity_level.CRITICAL)
def test_inside_the_normal_severity_test_class_overriding(self):
pass
执行级别为normal
和critical
,注意用逗号隔开但是不要多打空格,否则会报错 pytest -s -v文件名 --allure-severities normal,critical
前端自动化测试
场景:前端自动化测试经常需要附加图片或者html,在适当的地方适当的时机截图
解决:@allure.attach
现实不同类型提供的附件,可以补充测试步骤或测试结果
步骤:
在测试报告中添加网页:allure.attach(body(内容), name, attachment_type, extension)
在测试报告里附加图片:allure.attach.file(source, name, attachment_type, extension)
import allure
import pytest
def test_attach_text():
allure.attach("这是一个纯文本", attachment_type=allure.attachment_type.TEXT)
def test_attach_html():
allure.attach("<body>这是一段html的body块</body>", "html测试块", attachment_type=allure.attachment_type.HTML)
#注意这里只能使用file来添加这个图片,直接使用attach存在问题
def test_attach_photo():
allure.attach.file("resources/boji.jpg", name="这是一个图片", attachment_type=allure.attachment_type.JPG)