引言
再《Jenkins+windows+selenium+python自动化持续集成搭建》中已经介绍了基本环境的搭建,本文章是再此基础上加入了pytest框架和allure框架,让我们的自动化测试更简单和美观。
1.Pytest介绍
pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:
1、简单灵活,容易上手;
2、支持参数化;
3、能够支持简单的单元测试 和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests);
4、pytest具 有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测 试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等;
5、测试用例的 skip和xfail处理;
6、可以很好的和jenkins集成;
题外话:之前本身选择nose框架,但是nose生成中文html报告出现问题,pytest和nose框架类似,并且支持更多的扩展,果断用之。
2.Pytest的使用
1. 安装
安装比较简单,直接在python环境下使用pip安装:
pip install -U pytest #pytest框架
pip install -U pytest-html #生成html报告的插件
pip install -U pytest-rerunfailures #执行用例如果失败,可再次运行此用例的插件
2. 执行用例
默认执行当前目录下的所有以test_为前缀(test_*.py)或以test为后缀(*_test.py)的文件中以test为前缀的函数
3. 常用命令(摘录)
python -m pytest调用:
python -m pytest [...] 效果和py.test [...] 一样
获取版本,选项名,环境变量
py.test --version 看版本
py.test --fixtures 查看内置参数
py.test -h | --help 命令行和配置文件帮助
失败后停止
首次失败后停止执行:py.test -x
py.test --maxfail=2 两次失败之后停止执行
执行选择用例
py.test test_mod.py,执行模块中的用例
py.test somepath,执行路径中用例
py.test -k stringexpr,执行字符串表达式中的用例,比如"and not method",选择TestMyClass.test_something,排除了TestMyClass.test_method_simple。
py.test --pyargs pkg,导入pkg,使用其文件系统位置来查找和执行用例。执行pypkg目录下的所有用例。
调试输出:
py.test --showlocals 在traceback中显示本地变量
py.test -l 在traceback中显示本地变量(快捷方式)
py.test --tb=long 默认的traceback信息格式化形式
py.test --tb=native 标准库格式化形式
py.test --tb=short 更短的格式
py.test --tb=line 每个错误一行
失败时调用PDB (Python Debugger):
Python带有一个内置的Python调试器称为PDB。pytest可以在命令行选项指定调用:
py.test –pdb
这将每次失败时调用Python调试器。
py.test -x –pdb # 失败时调用pdb,然后退出测试。
py.test –pdb - maxfail=3# 前3次失败调用pdb。
设置断点:
import pytest
def test_function():
...
pytest.set_trace() # invoke PDB debugger and tracing
以前的版本中只有通过py.test-s禁用命令行捕捉才可以进入pdb调试。
Profiling测试执行时间:得到最执行慢的10个测试:
py.test –durations=10
创建JUnitXML格式的文件
创建Hudson或其他持续集成服务器的结果文件:
py.test –junitxml=path
创建resultlog格式的文件
要创建纯文本的机器可读的结果文件,用于PyPy-testweb展示等。
py.test –resultlog=path
发送测试报告给在线pastebin服务
bpaste可以为你的文本生成url连接,下面为创建每个测试失败创建一个url:
py.test –pastebin=failed
py.test –pastebin=all
py.test –pastebin=failed -x
目前只支持:py.test –pastebin=failed
禁用插件
py.test -p no:doctest
在python代码中调用pytest
pytest.main([’-x’, ’mytestdir’])
pytest.main("-x mytestdir")
# 指定插件
# content of myinvoke.py
import pytest
class MyPlugin:
def pytest_sessionfinish(self):
print("***test run reporting finishing")
pytest.main(“-qq”, plugins=[MyPlugin()])
上图就是实际运行的情况;
Python –m pytest test_case –html=./report/result.html –junitxml=./report/result.xml – resultlog=./ report/result.txt
以上命令就可以满足我们日常使用,pytest是只执行当前目录下的所有带test_开头或者_test结尾的文件,所有命令中指定了运行case的路径目录,并在report目录中生成我们可能用到的xml/html/txt格式的输出报告。
4. rerunfailures的使用
因再执行自动化脚本时,总会有些case因为网络等原因执行失败,这里可以使用我们之前装的rerunfailures来设置失败重跑功能,具体命令如下:
pytest –reruns 5 –reruns-delay 1
–reruns 是再case失败的情况下,重跑多少次
–reruns-delay 是重跑的间隔
除了在命令行的命令之外,我们还可以再指定的case中添加@pytest.mark.flaky(reruns=5)来指定此用例如果失败,可以重跑几次
5. 用例中的pytest的一些简单使用
Pytest是以修饰符的形式对用例做了扩展,例如(摘录)
以上是对用例进行参数化的例子(因笔者是unittest中添加pytest,所有类似于参数化的一些用法必须剔除掉unittest才能使用)
上一个例子是使用@pytest.mark.smokingtest,将用例定义关键字smokingtest,可以在命令行中使用smokingtest关键字来只执行加了此关键字的用例
更多使用可以参考网上的一些教程或官网来学习,这里不过多说明
6. pytest运行时还可以支持配置文件
我们可以目录下新建setup.cfg或者pytest.ini文件
[pytest]
addopts = -v test_case –html=./report/result.html –junitxml=./report/result.xml
python_classes = *_sta
python_functions = *_test
adopts 指定运行命令,python_classes指定运行class的命名,python_functions指定运行函数的命名,方便我们不用每次再命令行中输入过多信息
3.Allure
Allure介绍
allure 是一个轻量级的,灵活的,支持多语言,多平台的report框架。是的,它是支持多语言并在很多开源框架中做了适配集成的。我这次终于不是只介绍java的项目了。其目的就是使用简单方便的方式构建一个完善的report体系。
上一些效果图:
- Allure安装
首先需要安装pytest的插件
pip install pytest-allure- adaptor
然后再https://bintray.com/qameta/generic/allure2下载allure,解压到本地后,将解压目录添加到环境变量中(下载allure2,allure1已经不再提供支持)
然后再命令行执行时添加 –alluredir ./report/allure-result就可以生成暂时需要的allure 的xml格式的文件:
最后再命令行中输入allure open可以打开所生成的report
(也可以再命令行输入allure查看help)
- Allure用例中的使用
allure.MASTER_HELPER.environment(report=u’XXPC自动化测试报告’, browser=u’XXPC版本’,hostname=”win7”)
environment是描述一些环境信息的,其中变量名和value可以自己定义
@allure.MASTER_HELPER.severity(allure.MASTER_HELPER.severity_level.CRITICAL)
Severity可以定义用例的优先级别,其中包含TRIVIAL、NORMAL、MINOR、LOCKER、CRITICAL
此处可以和执行命令一起搭配,例如:
Py.test -v test_case –alluredir ./report/allure-result –allure_severities = critical,normal
使用–allure_severities可以指定只执行相匹配等级的用例
with allure.MASTER_HELPER.step(‘登录’):
setp可以定义用例的执行步骤
@allure.MASTER_HELPER.story(u”错误登录,弹出提示”)
@allure.MASTER_HELPER.feature(u”登录功能”)
feature和story可以组织用例,大功能使用feature定义,feature下的子功能点使用story定义
@allure.MASTER_HELPER.issue(“http:www.baidu.com”)
@allure.MASTER_HELPER.testcase(“http:www.baidu.com”)
Issue可以指向此用例的bug链接地址;testcase可以指向此用例的case的链接地址
f = self.driver.get_screenshot(“test”)
a = open(f,’rb’).read()
allure.MASTER_HELPER.attach(‘screenshot’,a,type=allure.MASTER_HELPER.attach_type.PNG)
attach可以给此用例添加截图或文本等信息
使用范例:
- Allure与jenkins的集成
需要下载 Allure Jenkins Plugin插件并进行安装;
安装完后,在系统设置-Global Tool Configuration中配置Allure Commandline
输入别名和安装目录(下载allure程序https://bintray.com/qameta/generic/allure2,解压到master,然后这里输入此处的目录路径)
Master机器上还需要安装JDK环境,必须1.8版本以上,然后在系统设置-Global Tool Configuration下配置JDK环境
然后在你所添加的job-配置中添加allure report
配置完成后可在job首页查看到allure report
更详细配置可以查看官网https://docs.qameta.io/allure/#_jenkins