草庐IT

Pytest框架 — 14、Pytest的标记(五)(控制测试用例执行顺序)

qishuaiRisen 2023-04-16 原文

目录

1、前言

在执行自动化测试时,我们通常都希望能够控制执行测试用例的顺序。

  • unittest框架中默认按照ACSII码的顺序加载测试用例并执行,顺序为:0~9A~Za~z,测试目录、测试模块、测试类、测试方法/测试函数都按照这个规则来加载测试用例。
  • pytest测试框架中,默认从上至下执行,也可以通过pytest-ordering插件来自定义执行顺序。

安装方式:
pip install pytest-ordering

2、使用

直接在要控制顺序的测试用例上使用@pytest.mark.order(order=顺序值)装饰器来标记执行顺序。
示例:

import pytest

@pytest.mark.run(order=4)
def test_pay():
    print("第四步:支付订单")

@pytest.mark.run(order=2)
def test_add_cart():
    print("第二步:加入购物车")

@pytest.mark.run(order=1)
def test_login():
    print("第一步:登录")

@pytest.mark.run(order=3)
def test_place_order():
    print("第三步:下订单")

"""
执行结果
mark/ordering/pytest_ordering.py::test_login 第一步:登录
PASSED
mark/ordering/pytest_ordering.py::test_add_cart 第二步:加入购物车
PASSED
mark/ordering/pytest_ordering.py::test_place_order 第三步:下订单
PASSED
mark/ordering/pytest_ordering.py::test_pay 第四步:支付订单
PASSED
"""

注意:

  1. @pytest.mark.run()必须以order=顺序值这种形式传递顺序值
  2. order值可以为正数或负数,但遵从值越小优先级越高原则
  3. order值混用正负数时,采用正数的优先级更高
  4. 没有标记顺序的用例优先级高于标记为负数的用例

3、标记最先执行和最后执行

可以通过@pytest.mark.firt@pytest.mark.last来标记用例的最先执行和最后执行。
示例:

import pytest

@pytest.mark.first
def test_login():
    print("登录")

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

def test_place_order():
    print("下单")

def test_pay():
    print("支付")

"""
执行结果
mark/ordering/order_first_and_last.py::test_login 登录
PASSED
mark/ordering/order_first_and_last.py::test_place_order 下单
PASSED
mark/ordering/order_first_and_last.py::test_pay 支付
PASSED
mark/ordering/order_first_and_last.py::test_logout 注销
PASSED
"""

提示:
当我们在使用@pytest.mark.first@pytest.mark.last装饰器时,python会把firstlast当成自定义标记,从而出现如下提示

PytestUnknownMarkWarning: Unknown pytest.mark is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.last

此时我们可以在命令行中添加-p no:warnings来屏蔽错误提示。

有关Pytest框架 — 14、Pytest的标记(五)(控制测试用例执行顺序)的更多相关文章

  1. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  2. 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

  3. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  4. ruby - Ruby 的 Hash 在比较键时使用哪种相等性测试? - 2

    我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。

  5. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  6. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  7. 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

  8. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  9. ruby-on-rails - 带 Spring 锁的 Rails 4 控制台 - 2

    我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.

  10. ruby-on-rails - openshift 上的 rails 控制台 - 2

    我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新ruby​​gems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems

随机推荐