pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点:
安装pytest:
pip install -U pytest
验证安装的版本:
pytest --version
如何编写pytest测试样例
通过上面2个实例,我们发现编写pytest测试样例非常简单,只需要按照下面的规则:
运行模式
Pytest的多种运行模式,让测试和调试变得更加得心应手,下面介绍5种常用的模式。在介绍之前需要提醒一句,运行pytest时会找当前目录及其子目录中的所有test_*.py 或 *_test.py格式的文件以及以test开头的方法或者class,不然就会提示找不到可以运行的case了。
1.运行后生成测试报告(htmlReport)
安装pytest-html:
pip install -U pytest-html
运行模式:
pytest --html=report.html
报告效果:
class TestClassOne(object): def test_one(self): x = "this" assert 't'in x
def test_two(self): x = "hello" assert hasattr(x, 'check') class TestClassTwo(object): def test_one(self): x = "iphone" assert 'p'in x def test_two(self): x = "apple" assert hasattr(x, 'check') |
运行模式:
模式1:直接运行test_se.py文件中的所有cases:
pytest test_se.py |
模式2:运行test_se.py文件中的TestClassOne这个class下的两个cases:
pytest test_se.py::TestClassOne |
模式3:运行test_se.py文件中的TestClassTwo这个class下的test_one:
pytest test_se.py::TestClassTwo::test_one |
注意:定义class时,需要以T开头,不然pytest是不会去运行该class的。
3.多进程运行cases
当cases量很多时,运行时间也会变的很长,如果想缩短脚本运行的时长,就可以用多进程来运行。
安装pytest-xdist:
pip install -U pytest-xdist |
运行模式:
pytest test_se.py -n NUM |
其中NUM填写并发的进程数。
4.重试运行cases
在做接口测试时,有事会遇到503或短时的网络波动,导致case运行失败,而这并非是我们期望的结果,此时可以就可以通过重试运行cases的方式来解决。
安装pytest-rerunfailures:
pip install -U pytest-rerunfailures |
运行模式:
pytest test_se.py --reruns NUM |
NUM填写重试的次数。
5.显示print内容
在运行测试脚本时,为了调试或打印一些内容,我们会在代码中加一些print内容,但是在运行pytest时,这些内容不会显示出来。如果带上-s,就可以显示了。
运行模式:
pytest test_se.py -s |
另外,pytest的多种运行模式是可以叠加执行的,比如说,你想同时运行4个进程,又想打印出print的内容。可以用:
pytest test_se.py -s -n 4 |
安装pytest的allure插件
pip install pytest-allure-adaptor |
安装allure,因为allure依赖于java环境,所以必须先安装java环境并设置环境变量(自行百度就可以)。
去以下链接,下载一个包,解压后将bin路径设置到环境变量path中https://github.com/allure-framework/allure2/releases
path环境变量如下图所示,
这样就可以通过cmd中的allure命令来生成测试报告
我在测试过程中提示module ‘pytest’ has no attribute ‘allure’,以下这篇文章可以解决:
https://www.cnblogs.com/lansan0701/p/10345142.html
pip uninstall pytest-allure-adaptor
pip install allure-pytest
装好以后,来写个demo
#coding=utf-8
import pytest import allure
marketid = ['101','102'] testlist = ['1','test','test1'] testlist2 = ['test0','test2','test3']
#市场id对应也生成相同的数组
@pytest.fixture(scope='module',autouse=True) @allure.feature('测试准备') def prepare(): print('这是测试准备哦') yield print('module 测试结束')
@allure.feature('期权强平测试') class Test_module1: @allure.feature('期权市价委托强平') @pytest.mark.parametrize('str1',testlist) @pytest.mark.parametrize('str2',testlist2) def test1(self,str1,str2): """ 用例描述:期权强平市价委托 """ stradd = str1 + str2 print('这是test1') assert 1 == 2
@allure.feature('期权限价委托强平') def test2(self): with allure.step("调节持仓"): x = '1' allure.attach(x) print('这是test2') with allure.step('进行强平的操作'): print('DTE')
class Test_module2:`在这里插入代码片` def test3(self): with allure.step("调节持仓"): x = '1' allure.attach(x) print('这是test2') with allure.step('进行强平的操作'): print('DTE') |
我用的是pycharm,在pycharm的Terminal中输入命令:
pytest -s -q --alluredir report |
这时候生成的是一堆json文件,不方便查看,大概长这样:
需要用刚才安装的allure command-line工具生成html格式的测试报告,在cmd中输入:
allure generate --clean report |
来看一下生成的报告路径:
打开上面图那个html,看一眼呗:(如果要用chrome打开,则必须在pycharm右键使用chrome打开,如果直接打开文件的话会404,不清楚原因,不过直接用firefox浏览器打开文件可以正常显示)
allure的其他用法,可以去官网看它的document和examples:
http://allure.qatools.ru/
1 篇文章
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!