草庐IT

Pytest框架 — 13、Pytest的标记(四)(分组执行)

qishuaiRisen 2023-04-16 原文

目录

1、前言

在自动化测试工作中我们有时候并不需要测试所有的测试用例,比如在冒烟测试阶段,我们只需要测试基本功能是否正常就可以了。在pytest中提供了mark标记功能来实现分组执行。

2、mark的使用

步骤:

  1. pytest.ini中注册标记(名称可自定义)
  2. 使用@pytest.mark.上一步注册的名称标记需要执行的用例
  3. 执行

(一)注册自定义标记

pytest.ini中添加markers

[pytest] # 固定的section名
markers= # 固定的option名称,注意缩进。
    标签名1: 标签名的说明内容。
    标签名2: 不写也可以
    标签名N

示例:

[pytest]
markers =
    smoke: 冒烟测试
    back

(二)在测试用例上标记

通过@pytest.mark.标记名标记要测试的用例。
示例:

import pytest

@pytest.mark.smoke
def test_1():
    print("测试1")

@pytest.mark.back
def test_2():
    print("测试2")

def test_3():
    print("测试3")

(三)执行

通过在命令行增加-m参数指定要测试的分组。
示例:
执行冒烟用例:pytest -vs xxx.py -m smoke

"""
执行结果
collected 3 items / 2 deselected / 1 selected                                                                                                 

mark/mark/mark.py::test_1 测试1
PASSED
"""

执行回归用例:pytest -vs xxx.py -m back

"""
执行结果
collected 3 items / 2 deselected / 1 selected                                                                                                 

mark/mark/mark.py::test_2 测试2
PASSED
"""

3、扩展

(一)在同一个测试用例上使用多个标记

import pytest

@pytest.mark.back
def test_register():
    # 注册
    print("注册")

@pytest.mark.smoke
@pytest.mark.back
def test_login():
    # 登录
    print("登录")

@pytest.mark.back
def test_logout():
    # 注销
    print("注销")

@pytest.mark.smoke
def test_add_cart():
    # 加购物车
    print("加购物车")

@pytest.mark.smoke
def test_place_order():
    # 下单
    print("下单")

@pytest.mark.smoke
@pytest.mark.pay
def test_pay_order():
    # 支付订单
    print("支付订单")

在执行时支持通过and,or,or来连接多个标记,如下

  • pytest -vs -m "smoke or pay" xxx.py,此时只有登录,加购物车,下单,支付订单这4个用例执行
  • pytest -vs -m "smoke and pay" xxx.py,此时只有支付订单这个用例执行
  • pytest -vs -m "not smoke" xxx.py,此时只有注册,注销这2个用例执行

(二)在测试类上使用标记

import pytest

@pytest.mark.smoke
class TestLogin:
    def test_login(self):
        print("登录")

有关Pytest框架 — 13、Pytest的标记(四)(分组执行)的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  3. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  4. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  5. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

  6. ruby - 安装libv8(3.11.8.13)出错,Bundler无法继续 - 2

    运行bundleinstall后出现此错误:Gem::Package::FormatError:nometadatafoundin/Users/jeanosorio/.rvm/gems/ruby-1.9.3-p286/cache/libv8-3.11.8.13-x86_64-darwin-12.gemAnerroroccurredwhileinstallinglibv8(3.11.8.13),andBundlercannotcontinue.Makesurethat`geminstalllibv8-v'3.11.8.13'`succeedsbeforebundling.我试试gemin

  7. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  8. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  9. TimeSformer:抛弃CNN的Transformer视频理解框架 - 2

    Transformers开始在视频识别领域的“猪突猛进”,各种改进和魔改层出不穷。由此作者将开启VideoTransformer系列的讲解,本篇主要介绍了FBAI团队的TimeSformer,这也是第一篇使用纯Transformer结构在视频识别上的文章。如果觉得有用,就请点赞、收藏、关注!paper:https://arxiv.org/abs/2102.05095code(offical):https://github.com/facebookresearch/TimeSformeraccept:ICML2021author:FacebookAI一、前言Transformers(VIT)在图

  10. ruby - 在 Ruby 中创建按公共(public)键值分组的新哈希 - 2

    假设我有一个在Ruby中看起来像这样的哈希:{:ie0=>"Hi",:ex0=>"Hey",:eg0=>"Howdy",:ie1=>"Hello",:ex1=>"Greetings",:eg1=>"Goodday"}有什么好的方法可以将它变成如下内容:{"0"=>{"ie"=>"Hi","ex"=>"Hey","eg"=>"Howdy"},"1"=>{"ie"=>"Hello","ex"=>"Greetings","eg"=>"Goodday"}} 最佳答案 您要求一个好的方法来做到这一点,所以答案是:一种您或同事可以在六个月后理解

随机推荐