在自动化测试的时候我们可能会遇到某些原因,如模块不稳定等,出现一些测试失败,此时我们想要针对单个用例或者单个模块重复执行多次,以确定测试失败的真正原因。在Pytest中可以通过插件pytest-repeat来实现。
安装方式:pip install pytest-repeat
main函数使用pytest -s -v ./xxx.py --count=3
pytest.main(["-vs","xxx.py","--count=3"])
示例:
def test_1():
print("测试1")
assert True
def test_2():
print("测试2")
assert False
"""
执行结果
mark/repeat/repeat_count.py::test_1[1-3] 测试1
PASSED
mark/repeat/repeat_count.py::test_1[2-3] 测试1
PASSED
mark/repeat/repeat_count.py::test_1[3-3] 测试1
PASSED
mark/repeat/repeat_count.py::test_2[1-3] 测试2
FAILED
mark/repeat/repeat_count.py::test_2[2-3] 测试2
FAILED
mark/repeat/repeat_count.py::test_2[3-3] 测试2
FAILED
"""
在pytest.ini配置文件中addopts添加count参数
[pytest]
addopts = -s -v --count 2
testpaths = scripts
python_files = test_*.py
python_classes = Test*
python_functions = test*
示例:
def test_1():
print("测试1")
assert True
def test_2():
print("测试2")
assert False
"""
执行结果
mark/repeat/repeat_count.py::test_1[1-2] 测试1
PASSED
mark/repeat/repeat_count.py::test_1[2-2] 测试1
PASSED
mark/repeat/repeat_count.py::test_2[1-2] 测试2
FAILED
mark/repeat/repeat_count.py::test_2[2-2] 测试2
FAILED
"""
从上面的示例中可以看到测试1先执行了3次,然后测试2再执行3次,有些时候我们想整体执行3次,这就需要用到--repeat-scope参数。类似于fixture的scope也可以设置范围
session:重复整个测试会话,即所有收集到的测试用例测试完成一次,再进行下一次测试module:重复整个模块,即模块内所有收集到的测试用例测试完成一次,再进行下一次测试class:重复所有类,即以每个类为集合所有测试用例测试完成一次,再进行下一次测试function:默认范围,重复所有函数和方法,即一个测试函数或方法重复执行完毕,再对下一个测试函数重复执行class TestClass_1:
def test_method_1(self):
print("类1测试方法1")
def test_method_2(self):
print("类1测试方法2")
class TestClass_2:
def test_method_1(self):
print("类2测试方法1")
def test_method_2(self):
print("类2测试方法2")
pytest -s -v xxx.py --count=2 --repeat_scope=class"""
执行结果
mark/repeat/repeat_scope.py::TestClass_1::test_method_1[1-2] 类1测试方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_2[1-2] 类1测试方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_1[2-2] 类1测试方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_2[2-2] 类1测试方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_1[1-2] 类2测试方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_2[1-2] 类2测试方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_1[2-2] 类2测试方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_2[2-2] 类2测试方法2
PASSED
"""
pytest -s -v xxx.py --count=2 --repeat-scope=module"""
执行结果
mark/repeat/repeat_scope.py::TestClass_1::test_method_1[1-2] 类1测试方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_2[1-2] 类1测试方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_1[1-2] 类2测试方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_2[1-2] 类2测试方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_1[2-2] 类1测试方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_2[2-2] 类1测试方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_1[2-2] 类2测试方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_2[2-2] 类2测试方法2
PASSED
"""
还可以在测试代码中使用@pytest.mark.repeat(count)装饰器来标记需要重复执行的测试。
import pytest
@pytest.mark.repeat(2)
def test_1():
print("测试函数1")
def test_2():
print("测试函数2")
class TestClass:
def test_1(self):
print("类中测试方法1")
@pytest.mark.repeat(3)
def test_2(self):
print("类中测试方法2")
"""
执行结果
mark/repeat/repeat_mark.py::test_1[1-2] 测试函数1
PASSED
mark/repeat/repeat_mark.py::test_1[2-2] 测试函数1
PASSED
mark/repeat/repeat_mark.py::test_2 测试函数2
PASSED
mark/repeat/repeat_mark.py::TestClass::test_1 类中测试方法1
PASSED
mark/repeat/repeat_mark.py::TestClass::test_2[1-3] 类中测试方法2
PASSED
mark/repeat/repeat_mark.py::TestClass::test_2[2-3] 类中测试方法2
PASSED
mark/repeat/repeat_mark.py::TestClass::test_2[3-3] 类中测试方法2
PASSED
"""
注意:使用装饰器标记的重复执行用例不受参数执行的影响,即当上面例子在执行时使用--count=5,标记过的测试用例以装饰器标记次数为准,其他未标记的测试用例会执行5次。
如果遇到间歇性bug,可以在命令中--count与-x结合使用,重复执行测试用例,直到测试失败停止。
pytest -vs -x xxx.py --count=100
这样将运行100次,一旦遇到失败就会停止。
我在使用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
我遵循了教程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
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
如何检查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-检查是否
//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
我从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
Transformers开始在视频识别领域的“猪突猛进”,各种改进和魔改层出不穷。由此作者将开启VideoTransformer系列的讲解,本篇主要介绍了FBAI团队的TimeSformer,这也是第一篇使用纯Transformer结构在视频识别上的文章。如果觉得有用,就请点赞、收藏、关注!paper:https://arxiv.org/abs/2102.05095code(offical):https://github.com/facebookresearch/TimeSformeraccept:ICML2021author:FacebookAI一、前言Transformers(VIT)在图
我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption
我写了一个非常简单的rake任务来尝试找到这个问题的根源。namespace:foodotaskbar::environmentdoputs'RUNNING'endend当在控制台中执行rakefoo:bar时,输出为:RUNNINGRUNNING当我执行任何rake任务时会发生这种情况。有没有人遇到过这样的事情?编辑上面的rake任务就是写在那个.rake文件中的所有内容。这是当前正在使用的Rakefile。requireFile.expand_path('../config/application',__FILE__)OurApp::Application.load_tasks这里
如果names为nil,则以下中断。我怎样才能让这个map只有在它不是nil时才执行?self.topics=names.split(",").mapdo|n|Topic.where(name:n.strip).first_or_create!end 最佳答案 其他几个选项:选项1(在其上执行map时检查split的结果):names_list=names.try(:split,",")self.topics=names_list.mapdo|n|Topic.where(name:n.strip).first_or_create!e