pytest执行方式详解(一)

  Pytest单元测试框架

 

 

 

 Pytest介绍:

  Pytest是python2自带的自动化测试框架,python3的版本的话pytest框架独立出来,是一款python的单元测试

框架,相比unittest更加灵活,更加容易上手,包括使用Pytest结合 Selenium丶Appium丶Requests进行web丶app丶api

实现自动化测试,

优势:

1.测试用例的skip和xfail处理,可以跳过指定用例,或对某些预期失败的case标记成失败。

2.可以很好的和jenkins集成,实现持续集成,

3.支持与allure结合自动化生成测试报告

4.支持运行由nose、unittest编写的测试用例,

5.支持参数化方式,ddt数据驱动;

 

一丶Pytest需要pip进行安装:

1.Python3使用pip install  pytest -i https://pypi.douban.simple安装

2.pycharm下载file-->setting-->project项目 点击+号 搜索 pytest --->install packge下载

 

二丶Pytest支持的插件:

1.Pytest-xdist: 用于自动化用例执行分布式执行丶提高代码执行速度

2.Pytest-ordering:用于改变测试用例执行顺序丶以及进行管理组织分组管理测试用例执行

3.Pytest-requnfailures:用于测试用例执行失败重新执行操作

4.allure-Pytest:用来生成自动化的测试报告

 

三丶Pytest的默认的测试用例执行规则

1.模块名称必须以test进行开头

2.测试类必须以Test进行开头

3.相关函数方法必须以test进行开头

4.pytest执行用例顺序不按照ASCII顺序进行执行,是按照自己写的case顺序进行执行用例

 

四丶Pytest运行方式:

1):主函数运行模式

import pytest

if __name__ == '__main__':
    # pytest.main() #运行所有内容
    

参数解释:

-v:执行完用例显示,更加详细的信息

1
2
3
4
5
6
<br>#返回结果case_api/api_ddt_demo03.py::Test::test01 PASSED                          [ 16%]
case_api/api_ddt_demo03.py::Test::test02 SKIPPED (unconditional skip)    [ 33%]
case_api/api_ddt_demo03.py::Test::test03 PASSED                          [ 50%]
case_api/api_ddt_demo03.py::Test::test04 PASSED                          [ 66%]
case_api/api_ddt_demo03.py::Test::test05 PASSED                          [ 83%]
case_api/api_ddt_demo03.py::Test::test06 PASSED 

 

-s: 显示输出调试信息,包含print打印输出的值

1
2
3
4
5
6
7
8
<br>#返回结果<br>case_api\api_ddt_demo03.py hello word(1)
.shello word(3)
.hello word(4)
.hello word(5)
.hello word(6)
.
 
============= 5 passed, 1 skipped in 0.48s ==================================

  

-k:根据用例的部分字符串内容进行运行执行的测试用例

1
2
3
4
5
pytest.mian(["-k 05"])<br>
#执行结果
case_api\api_ddt_demo03.py .                                             [100%]
 
============== 1 passed, 5 deselected in 0.50s ================================

  

-n: 支持多线程或者分布式的运行方式,同时多cpu进行执行测试用例

1
2
3
4
5
pytest.main(["-n 2"])#返回结果<br>gw0 I / gw1 I
gw0 [6] / gw1 [6]
 
s.....                                                                   [100%]
======================== 5 passed, 1 skipped in 2.42s =======================

  

--maxfail: 出现错误的用例,就停止测试

1
pytest.main(["--maxfail=2"])

--reruns: 失败用例从新进行执行

1
2
3
4
5
6
7
8
9
10
11
12
13
pytest.main(['-vs',"--reruns=2"])#执行用例
 
执行结果:
case_api/api_ddt_demo03.py::Test::test03 hello word(3)
PASSED
case_api/api_ddt_demo03.py::Test::test04 hello word(4)
PASSED
case_api/api_ddt_demo03.py::Test::test05 hello word(5)
PASSED
case_api/api_ddt_demo03.py::Test::test06 RERUN
case_api/api_ddt_demo03.py::Test::test06 RERUN
case_api/api_ddt_demo03.py::Test::test06 FAILED
=============== 1 failed, 4 passed, 1 skipped, 2 rerun in 0.58s ===============

  

 

2)命令行运行模式

pytest:通过dos命令行同样可以进行执行相关用例

1
2
3
4
5
6
7
Users\admin\PycharmProjects\wuhan>pytest #执行测试用例
<br>返回结果:
case_api\api_ddt_demo03.py:211: AssertionError
========================= short test summary info ==========================
FAILED case_api/api_ddt_demo03.py::Test::test06 - AssertionError: True is ...
 
================== 1 failed, 4 passed, 1 skipped in 0.70s ==================

 

pytest -vs 打印详细的输出结果

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
C:\Users\admin\PycharmProjects\wuhan12>pytest -vs #接参数运行
 
输出结果:
=========================== test session starts ============================
 
plugins: allure-pytest-2.9.45, forked-1.4.0, html-3.1.1, metadata-2.0.1, orde
ring-0.6, rerunfailures-10.2, xdist-2.5.0
collected 6 items                                                          
 
case_api/api_ddt_demo03.py::Test::test01 hello word(1)
PASSED
case_api/api_ddt_demo03.py::Test::test02 SKIPPED (unconditional skip)
case_api/api_ddt_demo03.py::Test::test03 hello word(3)
PASSED
case_api/api_ddt_demo03.py::Test::test04 hello word(4)
PASSED
case_api/api_ddt_demo03.py::Test::test05 hello word(5)
PASSED
case_api/api_ddt_demo03.py::Test::test06 FAILED

 

posted @   多测师-星sir  阅读(592)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示