Python UI自动化 —— pytest常用运行参数解析、pytest执行顺序解析

这篇具有很好参考价值的文章主要介绍了Python UI自动化 —— pytest常用运行参数解析、pytest执行顺序解析。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Python UI自动化 —— pytest常用运行参数解析、pytest执行顺序解析,自动化测试,软件测试,测试工程师,python,pytest,自动化测试,软件测试,功能测试

pytest常用Console参数:

  • -v 用于显示每个测试函数的执行结果
  • -q 只显示整体测试结果
  • -s 用于显示测试函数中print()函数输出
  • -x 在第一个错误或失败的测试中立即退出
  • -m 只运行带有装饰器配置的测试用例
  • -k 通过表达式运行指定的测试用例
  • -h 帮助

首先来看什么参数都没加的运行情况

class TestClass():
    def test_zne(self):
        print(1)
        assert 1==2

    def test_two(self):
        print(2)
        assert 1==2

    def test_a(self):
        print(3)
        assert 1==1

if __name__ == '__main__':
    pytest.main()

============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 items

test_page.py FF.                                                         [100%]

================================== FAILURES ===================================

-v 用于显示每个测试函数的执行结果

用于打印显示每条用例的执行情况

import pytest
class TestClass():
    def test_zne(self):
        print(1)
        assert 1==2

    def test_two(self):
        print(2)
        assert 1==2

    def test_a(self):
        print(3)
        assert 1==1

if __name__ == '__main__':
    pytest.main(['-v'])

============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Users\72036454\AppData\Local\Programs\Python\Python38\python.exe
cachedir: .pytest_cache
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collecting ... collected 3 items

test_page.py::TestClass::test_zne FAILED                                 [ 33%]
test_page.py::TestClass::test_two FAILED                                 [ 66%]
test_page.py::TestClass::test_a PASSED                                   [100%]

================================== FAILURES ===================================

-q 只显示整体测试结果

简化测试整体结果。F:代表测试失败、.:代表测试通过

import pytest
class TestClass():
    def test_zne(self):
        print(1)
        assert 1==2

    def test_two(self):
        print(2)
        assert 1==2

    def test_a(self):
        print(3)
        assert 1==1

if __name__ == '__main__':
    pytest.main(['-q'])

FF.                                                                      [100%]
================================== FAILURES ===================================

-s 用于显示测试函数中print()函数输出

显示测试用例中 print() 中的值

import pytest
class TestClass():
    def test_zne(self):
        print(1)
        assert 1==2

    def test_two(self):
        print(2)
        assert 1==2

    def test_a(self):
        print(3)
        assert 1==1

if __name__ == '__main__':
    pytest.main(['-s'])

============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 items

test_page.py 1
F2
F3
.

================================== FAILURES ===================================

-x 在第一个错误或失败的测试中立即退出

第一条用例执行失败,立即退出不在往下执行用例

import pytest
class TestClass():
    def test_zne(self):
        print(1)
        assert 1==2

    def test_two(self):
        print(2)
        assert 1==2

    def test_a(self):
        print(3)
        assert 1==1

if __name__ == '__main__':
    pytest.main(['-x','-s'])

============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 items

test_page.py 1
F

================================== FAILURES ===================================

-m 只运行带有装饰器配置的测试用例

用例中,第二和第三条用例加上了装饰器,装饰器最后一个单词分别为“slow” 和 “faster” ,-m 拿着两个单词去识别带这个装饰器的用例,识别到就执行,没有识别到的就不执行。

-m后面接的是表达式:['-s','-m slow or faster'] 、['-s','-m slow and faster']、['-s','-m not slow'] 这些表达式都支持。

import pytest
class TestClass():
    def test_zne(self):
        print(1)
        assert 1==2

    @pytest.mark.slow
    def test_two(self):
        print(2)
        assert 1==2

    @pytest.mark.faster
    def test_a(self):
        print(3)
        assert 1==1

if __name__ == '__main__':
    pytest.main(['-s','-m slow or faster'])

============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 items / 1 deselected / 2 selected

test_page.py 2
F3
.

================================== FAILURES ===================================

-k 通过表达式运行指定的测试用例

通过表达式匹配用例的函数名去执行用例,not test_zne 意思是不执行“test_zne”这条用例,所以就会执行第二第三条。同理 ['-s','-k test_zne'] 表示只执行第一条。

import pytest
class TestClass():
    def test_zne(self):
        print(1)
        assert 1==2

    @pytest.mark.slow
    def test_two(self):
        print(2)
        assert 1==2

    @pytest.mark.faster
    def test_a(self):
        print(3)
        assert 1==1

if __name__ == '__main__':
    pytest.main(['-s','-k not test_zne'])

============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 items / 1 deselected / 2 selected

test_page.py 2
F3
.

================================== FAILURES ===================================

-h 帮助

这才是重点,学会使用这个,剩余的都学会了

import pytest
class TestClass():
    def test_zne(self):
        print(1)
        assert 1==2

    @pytest.mark.slow
    def test_two(self):
        print(2)
        assert 1==2

    @pytest.mark.faster
    def test_a(self):
        print(3)
        assert 1==1

if __name__ == '__main__':
    pytest.main(['-h'])

Python UI自动化 —— pytest常用运行参数解析、pytest执行顺序解析,自动化测试,软件测试,测试工程师,python,pytest,自动化测试,软件测试,功能测试

pytest的执行顺序:

  • 默认情况下,pytest的执行顺序是自上往下的。
  • 可以通过第三方插件pytest-ordering实现自定义用例执行顺序
  • 官方文档: https://pytest-ordering.readthedocs.io/en/develop/

安装插件:

pip install pytest-ordering

pytest-ordering使用:

方式一

  • 第一个执行:@pytest.mark.first
  • 第二个执行:@pytest.mark.second
  • 倒数第二个执行:@pytest.mark.second_to_last
  • 最后一个执行:@pytest.mark.last

方式二

  • 第一个执行:@pytest.mark.run('first')
  • 第二个执行:@pytest.mark.run('second')
  • 倒数第二个执行:@pytest.mark.run('second_to_last')
  • 最后一个执行:@pytest.mark.run('last')

方式三

  • 第一个执行:@pytest.mark.run(order=1)
  • 第二个执行:@pytest.mark.run(order=2)
  • 倒数第二个执行:@pytest.mark.run(order=-2)
  • 最后一个执行:@pytest.mark.run(order=-1)

对于以上三张方法,经常使用的不多,第一个执行和最后一个执行比较常用。

如果对你有帮助的话,点个赞收个藏,给作者一个鼓励。也方便你下次能够快速查找。      文章来源地址https://www.toymoban.com/news/detail-701055.html

到了这里,关于Python UI自动化 —— pytest常用运行参数解析、pytest执行顺序解析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 基于Python+Pytest+Playwright+BDD的UI自动化测试框架

    本框架是基于Python+Pytest+Playwright+BDD的UI自动化测试框架。 测试社区地址: https://www.gitlink.org.cn/zone/tester 入群二维码:https://www.gitlink.org.cn/floraachy/apiautotest/issues/1 对于框架任何问题,欢迎联系我! 支持通过命令行指定浏览器,选择需要运行的浏览器。 支持通过命令行指定运行

    2024年02月07日
    浏览(37)
  • python+playwright+pytest+allure+pom+yaml实现UI自动化测试

    https://gitee.com/giteetangll/playwright-demo Auth:登录认证保存后的认证信息 BasePage:封装playwright的基础方法 BuildInLibrary:环境变量存放文件夹,可进行用例参数关联 Common:存放公共方法抽离文件夹 Config:配置文件存放文件夹 Logs:存放断言失败的记录 Pages:存放页面对象文件 Test

    2024年02月11日
    浏览(42)
  • pytest自动化测试实战之执行参数

    上一篇介绍了如何运行pytest代码,以及用例的一些执行规则,执行用例发现我们中间print输出的内容,结果没有给我们展示出来,那是因为pytest执行时,后面需要带上一些参数。 我们可以在cmd中通过输入 pytest -h 或者pytest --help 来查看帮助内容 奈何安静屏幕小,只写了一部分

    2024年02月13日
    浏览(26)
  • python ui自动化测试元素定位常用语法

    第一部分是css样式定位方法 选择器 示例 示例说明 CSS . class .intro 选择所有class=\\\"intro\\\"的元素 1 # id #firstname 选择所有id=\\\"firstname\\\"的元素 1 * * 选择所有元素 2 element p 选择所有p元素 1 element,element div,p 选择所有div元素和p元素 1 element   element div p 选择div元素内的所有p元素 1 element

    2024年02月13日
    浏览(34)
  • pytest自动化框架运行全局配置文件pytest.ini

    还记得在之前的篇章中有讲到Pytest是目前主要流行的自动化框架之一,他有基础的脚本编码规则以及两种运行方式。 pytest的基础编码规则是可以进行修改,这就是今日文章重点。 看到这大家心中是否提出了两个问题:pytest的基础编码规则在哪可以修改?又是如何修改? 让我

    2024年02月09日
    浏览(39)
  • Pytest UI自动化测试实战实例

    序号 库/插件/工具 安装命令 1 确保您已经安装了python3.x 2 配置python3+pycharm+selenium2开发环境 3 安装pytest库 pip install pytest 4 安装pytest -html 报告插件 pip install pytest-html 5 安装pypiwin32库(用来模拟按键) pip install pypiwin32 6 安装openpyxl解析excel文件库 pip install openpyxl 7 安装yagmail发送报告

    2024年02月05日
    浏览(51)
  • 【自动化测试】Pytest+Appium+Allure 做 UI 自动化的那些事

    文本主要介绍下 Pytest+Allure+Appium 记录一些过程和经历。 法主要用了啥: Python3 Appium Allure-pytest Pytest Appium 不常见却好用的方法 Appium 直接执行 adb shell 方法 #Appium 启动时增加 --relaxed-security 参数 Appium 即可执行类似adb shell的方法 appium -p 4723 --relaxed-security #使用方法 def adb_shell(se

    2024年01月25日
    浏览(36)
  • Pytest+selenium UI自动化测试实战实例

    今天来说说pytest吧,经过几周的时间学习,有收获也有疑惑,总之最后还是搞个小项目出来证明自己的努力不没有白费。 1    确保您已经安装了 python3.x 2    配置 python3+pycharm+selenium2 开发环境     3    安装pytest库 pip install pytest 4    安装pytest -html 报告插件 pip install pytest

    2024年02月05日
    浏览(41)
  • Pytest+Selenium UI自动化测试实战实例(全)

    🍅 视频学习: 文末有免费的配套视频可观看 🍅 关注公众号【互联网杂货铺】,回复 1 , 免费获取软件测试全套资料,资料在手,涨薪更快 今天来说说pytest吧,经过几周的时间学习,有收获也有疑惑,总之最后还是搞个小项目出来证明自己的努力不没有白费 1    确保您

    2024年03月19日
    浏览(54)
  • Pytest+Webdriver+Alluer的UI自动化测试框架

    作为web自动化的入门学习,搭建框架练习下 一、熟悉项目的测试框架的整体目录 二、 PIP安装完所需框架 1、编写main.py 2、设计登录获取鉴权 3、设计页面测试用例 testwzm.py 4、设计conftest.py 优化报告样式

    2024年02月11日
    浏览(49)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包