pytest运行指定的测试参数

这篇具有很好参考价值的文章主要介绍了pytest运行指定的测试参数。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言


一.mark

pytest提供了标记机制,允许你使用marker对测试函数做标记,一个测试函数可以有多个marker,一个marker也可以用来标记多个测试函数

1.背景:

当我们需要进行冒烟测试,不可能把所有的用例都跑一遍,我们可以挑选一些重要的用例进行冒烟测试,为了把选定的测试加入冒烟测试,可以对他们添加@pytest.mark.smoke装饰器。

案例1:只执行smoke类型的测试用例

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言
test_menus1.py

# encoding=utf-8
import pytest
import logging
import random
import time

@pytest.mark.smoke
class TestMenus:

    @pytest.mark.smoke
    def test_menu1(self):
        logging.info('执行测试用例1')
        assert 2 == 2

    #@pytest.mark.repeat(2)
    @pytest.mark.smoke
    def test_menu2(self):
        logging.info('执行测试用例2')
        assert 1 == 1

    def test_menu3(self):
        logging.info('执行测试用例3')
        assert 2 == 2


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

test_work1.py

#encoding=utf-8

import pytest


class TestWork:

    @pytest.mark.smoke
    def test_h(self,home_url):
        print("用例:%s" % home_url)

    def test_work1(self):
        assert 1 == 1

    def test_work2(self):
        assert 1 == 1


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

执行命令行:

pytest

我们可以看到,
测试模块menus—>test_menus1.py文件—>类上加了装饰器,也就是说需要执行3条用例;
测试模块menus—>test_menus1.py文件—>test_h加了装饰器,说明执行1条用例
总需要执行4条用例
-m参数执行一下,这样我们标记的smoke的用例就被执行了,其他的没有被执行

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

-m后面也可以使用表达式,可以在标记之间添加and,or,not关键字

执行命令:表示执行没有标记smoke的测试用例

pytest -m "not smoke"

特别注意
pytest -m “not smoke”:必须使用双引号
pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

二.跳过测试skip

要跳过某个测试,只需要简单的在测试函数上方添加@pytest.mark.skip()装饰器即可
pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

案例1:pytest.mark.skip():跳过某些用例

test_menus.py

# encoding=utf-8
import pytest
import logging
import random
import time

@pytest.mark.smoke
class TestMenus:

    @pytest.mark.skip()
    @pytest.mark.smoke
    def test_menu1(self):
        logging.info('执行测试用例1')
        assert 2 == 2

    #@pytest.mark.repeat(2)
    @pytest.mark.smoke
    def test_menu2(self):
        logging.info('执行测试用例2')
        assert 1 == 1

    def test_menu3(self):
        logging.info('执行测试用例3')
        assert 2 == 2


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

test_work1.py

#encoding=utf-8

import pytest


class TestWork:

    @pytest.mark.smoke
    def test_h(self,home_url):
        print("用例:%s" % home_url)

    @pytest.mark.skip()
    def test_work1(self):
        assert 1 == 1

    def test_work2(self):
        assert 1 == 1


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

执行测试命令

pytest

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

案例2:如果在整个类中添加@pytest.mark.skip():表示整个类中的测试用例都将跳过

#encoding=utf-8

import pytest

@pytest.mark.skip()
class TestWork:

    @pytest.mark.smoke
    def test_h(self,home_url):
        print("用例:%s" % home_url)

    @pytest.mark.skip()
    def test_work1(self):
        assert 1 == 1

    def test_work2(self):
        assert 1 == 1


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

命令行执行:

pytest

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

三.跳过测试skipif

我们可以给要跳过的测试添加理由和条件,这时应当使用skipif来替代skip

前面的是个条件,可以是python的表达式,表达式的值为True时跳过,为false时执行。后面的是原因,在来执行一次

test_menus1.py

# encoding=utf-8
import pytest
import logging
import random
import time

@pytest.mark.smoke
class TestMenus:

    @pytest.mark.skipif(24<8,reason='版本不匹配')
    @pytest.mark.smoke
    def test_menu1(self):
        logging.info('执行测试用例1')
        assert 2 == 2

    #@pytest.mark.repeat(2)
    @pytest.mark.smoke
    def test_menu2(self):
        logging.info('执行测试用例2')
        assert 1 == 1

    def test_menu3(self):
        logging.info('执行测试用例3')
        assert 2 == 2


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

test_work1.py

#encoding=utf-8

import pytest

#@pytest.mark.skip()
class TestWork:

    @pytest.mark.smoke
    def test_h(self,home_url):
        print("用例:%s" % home_url)

    @pytest.mark.skipif(8<24,reason='版本不匹配')
    def test_work1(self):
        assert 1 == 1

    def test_work2(self):
        assert 1 == 1


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

执行命令
-rs:控制台查看跳过信息

pytest -rs

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

四.运行测试子集

运行测试有很多方式,不但可以选择运行某个测试目录、文件、类中的测试,还可以选择运行某一个测试用例

目录结构
pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

1.运行单个目录

test_menus1.py

# encoding=utf-8
import pytest
import logging
import random
import time

@pytest.mark.smoke
class TestMenus:

    @pytest.mark.skipif(24<8,reason='版本不匹配')
    @pytest.mark.smoke
    def test_menu1(self):
        logging.info('执行测试用例1')
        assert 2 == 2

    #@pytest.mark.repeat(2)
    @pytest.mark.smoke
    def test_menu2(self):
        logging.info('执行测试用例2')
        assert 1 == 1

    def test_menu3(self):
        logging.info('执行测试用例3')
        assert 2 == 2


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

test_work1.py

#encoding=utf-8

import pytest

#@pytest.mark.skip()
class TestWork:

    @pytest.mark.smoke
    def test_h(self,home_url):
        print("用例:%s" % home_url)

    @pytest.mark.skipif(8<24,reason='版本不匹配')
    def test_work1(self):
        assert 1 == 1

    def test_work2(self):
        assert 1 == 1


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

案例1:运行menus目录下的文件

执行命令

D:\project_development\api_pytest>pytest testcases/menus
collecting ... ----这是前置方法----

 testcases\menus\test_menus1.py::TestMenus.test_menu1 ✓                                                                                   33% ███▍

 testcases\menus\test_menus1.py::TestMenus.test_menu2 ✓                                                                                   67% ██████▋

----这是后置方法----
 testcases\menus\test_menus1.py::TestMenus.test_menu3 ✓                                                                                  100% ██████████


Results (0.10s):
       3 passed

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

2.运行单个测试文件

运行单个文件里的全部测试,以路径名加文件名作为pytest参数即可

案例2:运行test_menus1.py文件中的所有用例

执行命令

D:\project_development\api_pytest>pytest testcases/menus/test_menus1.py

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

3.运行单个测试函数

运行单个测试函数,只需要在文件名后添加::符号和函数名

案例3:运行test_menus1.py文件中的test_menu1这条用例

执行命令

D:\project_development\api_pytest>pytest testcases\menus\test_menus1.py::TestMenus::test_menu1

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言
特别注意:运行单个测试函数时,下面的方法不可取,会报错

ERROR: not found: D:\project_development\api_pytest\testcases\menus\test_menus1.py::test_menu1
(no name 'D:\\project_development\\api_pytest\\testcases\\menus\\test_menus1.py::test_menu1' in any of [<Module test_menus1.py>])

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

4.运行单个测试类

与运行单个函数类似,只需要在文件名后面加上::符号和类名

案例4:执行test_menus1.py下的TestMenus类

执行命令

D:\project_development\api_pytest>pytest testcases\menus\test_menus1.py::TestMenus

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言

5.用测试名划分测试集合

-k选项允许用一个表达式指定需要运行的测试,该表达式可以匹配测试名(或者子串)。表达式中可以包含and,or,not

案例:执行用例中包含’2’的测试用例

test_menus1.py

# encoding=utf-8
import pytest
import logging
import random
import time

@pytest.mark.smoke
class TestMenus:

    @pytest.mark.skipif(24<8,reason='版本不匹配')
    @pytest.mark.smoke
    def test_menu1(self):
        logging.info('执行测试用例1')
        assert 2 == 2

    #@pytest.mark.repeat(2)
    @pytest.mark.smoke
    def test_menu2(self):
        logging.info('执行测试用例2')
        assert 1 == 1

    def test_menu3(self):
        logging.info('执行测试用例3')
        assert 2 == 2


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

test_work1.py

#encoding=utf-8

import pytest

#@pytest.mark.skip()
class TestWork:

    @pytest.mark.smoke
    def test_h(self,home_url):
        print("用例:%s" % home_url)

    @pytest.mark.skipif(8<24,reason='版本不匹配')
    def test_work1(self):
        assert 1 == 1

    def test_work2(self):
        assert 1 == 1


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

执行命令

D:\project_development\api_pytest>pytest -k 2

pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言


pytest连跑收集和实际跑的数量不一致,# pytest,pytest,python,开发语言文章来源地址https://www.toymoban.com/news/detail-834048.html

到了这里,关于pytest运行指定的测试参数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • 提升测试效率,轻松并行运行测试——探秘Pytest插件pytest-xdist

    在软件开发中,测试是确保代码质量的重要一环。然而,随着项目规模的增大,测试用例的数量也随之增多,测试的执行时间可能成为一个瓶颈。为了解决这个问题,Pytest提供了丰富的插件生态系统,其中  pytest-xdist  插件是一个强大的工具,能够帮助我们并行运行测试,提

    2024年01月16日
    浏览(56)
  • Python测试框架pytest:常用参数、查找子集、参数化、跳过

    Pytest是一个基于python的测试框架,用于编写和执行测试代码。pytest主要用于API测试,可以编写代码来测试API、数据库、UI等。 pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个优点: 简单灵活,容易上手。pytest的语法简洁明了,易于理解和使用。 支持参数化。

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

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

    2024年02月13日
    浏览(27)
  • 软件测试|详解 Pytest 参数化:简化测试用例的编写

    简介 Pytest 是一个广泛使用的 Python 测试框架,它提供了丰富的功能来编写和执行测试用例。其中一个强大的特性是参数化,它允许我们通过一种简洁的方式运行多个输入参数的相似测试用例,从而减少冗余的代码。本文将详细介绍 Pytest 的参数化功能以及如何使用它来简化测

    2024年01月20日
    浏览(43)
  • python+appium+pytest自动化测试-参数化设置

    来自APP Android端自动化测试初学者的笔记,写的不对的地方大家多多指教哦。(所有内容均以微博V10.11.2版本作为例子) 在自动化测试用例执行过程中,经常出现执行相同的用例,但传入不同的参数,导致我们需要重复的写用例,这样会使我们的用例变得很长,冗余,很多地

    2023年04月08日
    浏览(34)
  • Pytest框架测试用例规则和运行方式

    目录 一、默认的测试用例规则 二、测试用例执行顺序 三、测试用例运行方式 3.1.主函数模式 3.1.1.主函数模式:4种运行方式  3.1.2.文件框架如下图  3.2.命令行模式 3.2.1.命令行模式:4种运行方式  3.2.2.第2种运行方式框架 3.3.通过读取配置文件pytest.ini运行 3.3.1.pytest.ini 文件注

    2024年02月11日
    浏览(42)
  • 自动化测试框架pytest系列之21个命令行参数介绍(二)

    第一篇 :  自动化测试框架pytest系列之基础概念介绍(一)-CSDN博客 接上文 3.pytest功能介绍 3.1 第一条测试用例 首先 ,你需要编写一个登录函数,主要是作为被测功能,同时编写一个测试脚本 ,进行测试登录功能 。 登录函数脚本: login.py 测试脚本 :test01_login.py 3.2 pytest的运行

    2024年02月02日
    浏览(31)
  • 实战干货,pytest自动化测试-Git中的测试用例运行(详细)

    我们每天写完自动化用例后都会提交到 git 仓库,随着用例的增多,为了保证仓库代码的干净,当有用例新增的时候,我们希望只运行新增的未提交 git 仓库的用例。 pytest-picked 插件可以实现只运行未提交到git仓库的代码。 pytest-picked 使用命令行安装 可使用参数 使用示例:

    2024年02月09日
    浏览(46)
  • 爆肝整理,Python自动化测试-Pytest参数化实战封装,一篇打通...

    参数化? 通俗点理解就是,定义一个测试类或测试函数,可以传入不同测试用例对应的参数,从而执行多个测试用例。 例如: 对登录接口进行测试,假设有3条用例,正确账号正确密码登录、正确账号错误密码登录、错误账号正确密码登录,那么我们只需要定义一个登陆测试

    2024年02月13日
    浏览(29)
  • 测试框架pytest教程(5)运行失败用例-rerun failed tests

    运行这个文件,2个失败,48个通过。 要运行上次失败的测试用例,可以使用 --lf (或 --last-failed )选项来告诉pytest只运行上次运行时失败的测试。 命令行示例: 或者在pytest配置文件(比如pytest.ini)中设置: 这样,pytest会检测上次运行时失败的测试用例,并只运行这些失败

    2024年02月11日
    浏览(30)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包