测试框架pytest(五)
接上一篇,本章讲pytest的配置文件pytest.ini以及allure测试框架
前面我们有讲到在pytest.ini中添加标签markers的方式去掉告警信息,今天会增加其他更多的配置项
[pytest] markers = smoke : smoke tags dev: dev tags
可以通过如下命令来查看pytest.ini的配置项,具体太多,就不一一显示:
pytest -h
addopts配置
我们在通过python或者pytest的方式执行测试类的时候会添加很多参数诸如 -v -s ,为了方便可以通过在pytest.ini增加addopts配置的方式来简化
修改后的pytest.ini如下:
[pytest]
markers =
smoke : smoke tag
dev : dev tag
addopts = -sv -m 'smoke'
testpaths = ./
testpaths配置
pytest默认是搜索执行当前目录以及子目录下的所有用例,有时候需要自定义执行目录,可以增加testpaths配置来实现,可以具体到某个文件或者目录
[pytest]
markers =
smoke : smoke tag
dev : dev tag
addopts = -sv -m 'smoke'
testpaths = ./
allure
支持多语言的测试报告工具,可以提供日志,截图,视频等数据展示在测试报告上
allure的安装
分2部分,一个是pytest的插件allure-pytest,用来可以在pytest中使用allure功能,另外一部分是allure服务端,用来解析pytest运行后生成的json文件,生成html格式的文档
插件的安装
pip install allure-pytest
常见功能特性介绍
@allure.feature 功能
@allure.story 子功能
@allure.step/with allure.step() 测试步骤
@allure.attach 添加附件,比如图片,视频等
对前面的test_demo.py改造如下:
#!/usr/bin/env python # -*-coding:utf-8 -*- import pytest import allure from get_data import get_data def add_demo(a, b): return a+b @pytest.fixture() def login(): print('execute before') @pytest.mark.smoke @allure.feature("logins") class TestDemo: @pytest.mark.parametrize("a,b,expected", get_data(), ids=["level1", "level2", "level3"]) def test_add_demo(self, a, b, expected): assert add_demo(a, b) == expected @allure.story('测试二') def test_two(self, login): with allure.step('步骤一'): print('测试步骤1') assert 2 != 3 @pytest.mark.usefixtures("login") def test_three(self): assert 4 != 3 @pytest.mark.usefixtures("login_se") def test_four(self): assert 6 != 9 def test_five(self, login_se): assert 9 != 0 @pytest.mark.parametrize("login_with_param", [{"user": "lucy", "password": "123456"}], ids=["level1"], indirect=True) def test_six(self, login_with_param): username = login_with_param['user'] password = login_with_param['password'] assert username == 'lucy' assert password == '123456' @pytest.mark.parametrize("login_with_params", [{"user": "lucy", "password": "123456"}], ids=["level1"], indirect=True) def test_seven(self, login_with_params): username = login_with_params['user'] password = login_with_params['password'] assert username == 'lucy' assert password == '123456' def test_eight(self, login_and_logout): username = login_and_logout['user'] password = login_and_logout['password'] assert username == 'lucy' assert password == '123456' if __name__ == "__main__": pytest.main()
同时改造pytest.ini如下:
[pytest] markers = smoke : smoke tag dev : dev tag addopts = -sv -m 'smoke' --allure-features='logins' --alluredir=./results testpaths = ./
上述代码运行后会在当前目录下新建results目录并且在里面写入allure的json数据,然后需要把这些数据生成html报告,需要做如下动作
- 运行命令allure serve ./results 这个命令会生成临时目录,并启动一个web服务器来可以在线访问测试报告,一般可以用来临时调试
- 运行allure generate ./results -o ./results_html 这个会将results目录下的数据生成html测试报告,放在./results_html目录下
上述需要本地安装allure应用,配置环境变量以及jdk,具体安装步骤百度
如果想让别人访问你生成的报告的话,可以使用如下命令:
allure open -h 127.0.0.1 -p 8094 ./results_html/
浙公网安备 33010602011771号