pytest使用实践

pytest使用

1、pytest简介

官档是最好的教程

pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点:

  • 简单灵活,容易上手,支持参数化。
  • 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests)。
  • pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等。
  • 测试用例的skip和xfail处理。
  • 可以很好的和jenkins集成,CICD使用。
  • report框架--allure也支持了pytest。

安装:

python3 -m pip install pytest

各种测试框架众多,测试业务集成框架的难度不一,排除框架已有便利不说,个人感觉关键在于下面几点:

  • 业务相关核心代码的封装,提供统一的接口,使用例补充调用便捷,非web类测试业务尤甚。
  • 清晰的调试过程信息输出,密切联系业务逻辑,进行关键性信息的埋点输出,并提供信息等级配置等。
  • 测试用例的补充,断言的合理性。

2、如何使用

假定的我的目录结构为下图所示:

|- msc_py                               # 根目录
    |- bvt_test                         # bvt自动化功能回归(pytest)
        -  isr_test.py                  # 听写测试

isr_test.py部分代码如下:

@pytest.mark.aqc2vinfo
class TestAqc2vinfoClass(object):
    def test_aqc2vinfo_equal_one(self):
        print("\r\n")
        res_kv = mac_test(
            msc_params="sub=iat,ent=sms16k,aue=raw,prs=1,ars=1,auf=audio/L16;rate=16000,aqc=1,vinfo=1")
        res_json_list = res_kv['result_json_list']
        record_common_msg(res_kv)
        assert "aqc" in res_json_list[0]
        assert "vad" in res_json_list[0]

    def test_vinfo_equal_one(self):
        print("\r\n")
        res_kv = mac_test(
            msc_params="sub=iat,ent=sms16k,vad_speech_tail=3000,prs=1,ptt=0,rate=16000,rst=json,rse=utf8,aqc=1,vinfo=0")
        res_json_list = res_kv['result_json_list']
        record_common_msg(res_kv)
        assert "aqc" in res_json_list[0]
        assert "vad" not in res_json_list[0]

2.1 编写pytest测试样例

编写pytest测试样例非常简单,只需要按照下面的规则:

  • 测试文件以test_开头(以_test结尾也可以)。
  • 测试类以Test开头,并且不能带有init方法。
  • 测试函数以test_开头。
  • 断言使用基本的assert即可。

2.2 运行模式

运行时,直接切到测试用例目录执行pytest即可,下面是一些参数介绍。

2.2.1 运行后生成测试报告(htmlReport)

生成html报告需要安装一个插件。

python3 -m pip install pytest-html

运行命令:

pytest --html=./report.html

2.2.2 运行指定的用例

如需要运行执行的用例时,有几种场景。

1、直接运行isr_test.py下面所有用例。

pytest isr_test.py

输出如下:

(venv) F:\desktop\work\python\py36-32\batrec\bvt_test>pytest isr_test.py
================================================================================================ test session starts =================================================================================================
platform win32 -- Python 3.6.7, pytest-4.2.0, py-1.7.0, pluggy-0.8.1
rootdir: F:\desktop\work\python\py36-32\batrec\bvt_test, inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.5.5
collected 80 items                                                                                                                                                                                                    

isr_test.py

……

不需要这些版本等信息的话,可以加参数如下,quiet模式执行:

pytest -q isr_test.py

这里会全部用例挨个执行,一个点一个点的打出来表示成功。

对应的有打印详细信息:

pytest -v isr_test.py

2、直接运行isr_test.pyTestAqc2vinfoClass下的两个用例。

pytest isr_test.py::TestAqc2vinfoClass

这里可以看到类名上面有@pytest.mark.aqc2vinfo这个装饰器,这个的作用是选择特定的用例执行。

only run tests matching given mark expression.example: -m 'mark1 and not mark2'.

pytest isr_test.py -v -s -m 'aqc2vinfo'

效果等同上面指定类名运行,但它可扩展运行更多的用例。

3、直接运行isr_test.pyTestAqc2vinfoClass下的test_aqc2vinfo_equal_one方法用例。

pytest isr_test.py::TestAqc2vinfoClass::test_aqc2vinfo_equal_one

以上运行时,不利于调试,类似print、log等打屏信息没有输出,可加参数设置如下:

pytest --capture=no isr_test.py::TestAqc2vinfoClass::test_aqc2vinfo_equal_one

或短写形式:

pytest -s isr_test.py::TestAqc2vinfoClass::test_aqc2vinfo_equal_one

2.2.3 多进程运行用例

当用例过多时,运行所有的用例也会用时过长,如果想缩短用例运行时间,即要多进程来运行。

安装pytest-xdist插件:

python3 -m pip install pytest-xdist

运行模式:

pytest -v -s isr_test.py -n NUM 

NUM为想要并发的进程数目。

效果如下:

(venv) F:\desktop\work\python\py36-32\batrec\bvt_test>pytest -v -s -n 5 isr_test.py
================================================================================================ test session starts =================================================================================================
platform win32 -- Python 3.6.7, pytest-4.2.0, py-1.7.0, pluggy-0.8.1 -- f:\desktop\work\python\py36-32\venv\scripts\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.7', 'Platform': 'Windows-10-10.0.16299-SP0', 'Packages': {'pytest': '4.2.0', 'py': '1.7.0', 'pluggy': '0.8.1'}, 'Plugins': {'xdist': '1.26.1', 'metadata': '1.8.0', 'html': '1.20.0', 'forked
': '1.0.2', 'allure-pytest': '2.5.5'}, 'JAVA_HOME': 'E:\\software\\java\\jdk1.8'}
rootdir: F:\desktop\work\python\py36-32\batrec\bvt_test, inifile:
plugins: xdist-1.26.1, metadata-1.8.0, html-1.20.0, forked-1.0.2, allure-pytest-2.5.5
[gw0] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw1] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw2] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw3] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw4] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw0] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
[gw1] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
[gw2] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
[gw3] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
[gw4] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
gw0 [80] / gw1 [80] / gw2 [80] / gw3 [80] / gw4 [80]
scheduling tests via LoadScheduling

2.2.4 重试运行用例

在做接口测试时,有时会因为网络等问题,导致用例运行失败,这时可以通过重试运行用例来解决。

安装pytest-rerunfailures插件:

python3 -m pip install pytest-rerunfailures

运行模式:

pytest -v -s isr_test.py -retuns NUM 

NUM为想要重试的次数。

3、常用插件介绍

推荐已经使用过常用插件:

  • pytest-html,生成html格式报告,本地调试推荐使用。(已集成)
  • pytest-xdist,开启多个worker进程,同时执行多个测试用例,达到并发运行的效果,大大提升构建效率。(已集成)
  • pytest-rerunfailures,自动重跑失败用例。
  • pytest-cache,重跑上次失败的用例,持续集成中很实用,提高分析效率。
  • pytest-sugar,改变了pytest的默认外观,增加了一个进度条,并立即显示失败的测试。(已集成)
  • pytest-ordering,可指定一个测试套中的所有用例执行顺序。
  • pytest-assume,多断言,第一个断言失败,第二个仍会执行。
  • pytest-cov,单元测试过程中,指标:行覆盖率。

pytest插件及兼容性,这里可以看到更多的插件。

4、高阶使用

4.1 pytest相关

4.1.1 pytest原生

此节段暂不适用,保留待补充。

4.1.2 pytest插件

1、allsure使用

持续构建,集成CICD时,需要生成漂亮的allure报告,网上坑比较多,感谢pytest+allure配置使用。

这里以平台win为例,其它平台参见allsure官档。

a、安装PowerShell (win10自带有,其他系统自行安装)。

b、打开PowerShell,输入命令。

set-executionpolicy remotesigned -s cu

有提示的话输入[Y]。

再输入:

iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

scoop help,出现选项帮助即安装成功。

c、安装allsure.

scoop install allure

d、进入isr_test.py文件的目录下,执行。

 pytest --alluredir=reports

这里可能会报错,不行就把reports换成绝对路径F:\desktop\work\python\py36-32\batrec\bvt_test\reports

pytest --alluredir=F:\desktop\work\python\py36-32\batrec\bvt_test\reports

然后当前目录中会多一个reports文件夹,里面是各种txt和json文件。

继续当前目录执行:

allure generate reports

存放用例的目录中会多一个allure-reports文件夹,更新allure-reports文件夹内容使用

allure generate reports --clean 

最后复制allure-reports目录下index路径,火狐打开,不要用其它浏览器,会有问题。

同样的allure也有一些用法,具体见上面的allure-pytest官档。

使用实例:

pytest -v -s -m aqc2vinfo --alluredir=F:\desktop\work\python\py36-32\batrec\bvt_test\reports isr_test.py

再allure生成在线报告,它会在默认的浏览器中打开:

allure serve reports

终极使用

pytest -v -s -n 5 --alluredir=F:\desktop\work\python\py36-32\batrec\bvt_test\reports isr_test.py

4.2 奇技淫巧

这个命名,足以表明内心很兴奋的样子了,即python必不可少的ide编辑器pycharm。它本身就不介绍了,下面是说它集成pytest。

打开设置:

File | Settings | Tools | Python Integrated Tools | default test runner | py.test

接下来就是很舒服的东西了,再次打开isr_test.py代码,每个类,方法,它的左侧就有一个可执行的绿色三角按钮,点一下,就可以跑用例了,较与上面的pytest使用不遑多让,用例补充调试可谓很爽了。

5、其它

脚本运行期间如遇python抛错,脚本添加下面类似代码进行运行调试,找对应的维护人员。

test = TestAqc2vinfoClass()
test.test_aqc2vinfo_equal_one()

再运行:

python3 ist_test.py

你可能感兴趣的:(pytest使用实践)