- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
请耐心等待我解释我的困境,我仍然是 Python 新手,所以我的术语可能不正确。另外,对于这篇文章不可避免的冗长,我深表歉意,但我会尝试尽可能多地解释相关细节。
快速总结:
我目前正在使用 py.test 为一组功能基本相同的网站开发一套 Selenium 测试
使用 pytest 插件将测试结果上传到 TestRail pytest-testrail.
测试用带有唯一案例 ID 的装饰器 @pytestrail.case(id) 标记
我的一个典型测试如下所示:
@pytestrail.case('C100123') # associates the function with the relevant TR case
@pytest.mark.usefixtures()
def test_login():
# test code goes here
正如我之前提到的,我的目标是创建一组代码来处理我们的许多具有(几乎)相同功能的网站,因此上面示例中的硬编码装饰器将不起作用。
我在 TestRail 中尝试了一种使用 csv 和测试列表及其案例 ID 的数据驱动方法。
例子:
website1.csv:
Case ID | Test name
C100123 | test_login
website2.csv:
Case ID | Test name
C222123 | test_login
我编写的代码将使用检查模块来查找正在运行的测试的名称,找到相关的测试 ID 并将其放入名为 test_id 的变量中:
import csv
import inspect
class trp(object):
def __init__(self):
pass
with open(testcsv) as f: # testcsv could be website1.csv or website2.csv
reader = csv.reader(f)
next(reader) # skip header
tests = [r for r in reader]
def gettestcase(self):
self.current_test = inspect.stack()[3][3]
for row in trp.tests:
if self.current_test == row[2]:
self.test_id = (row[0])
print(self.test_id)
return self.test_id, self.current_test
def gettestid(self):
self.gettestcase()
当时的想法是装饰器会根据我当时使用的 csv 动态变化。
@pytestrail.case(test_id) # now a variable
@pytest.mark.usefixtures()
def test_login():
trp.gettestid()
# test code goes here
因此,如果我为 website1 运行 test_login,装饰器将如下所示:
@pytestrail.case('C100123')
如果我为 website2 运行 test_login 装饰器将是:
@pytestrail.case('C222123')
我为自己想出这个解决方案而感到无比自豪,并进行了尝试……它没有用。虽然代码本身确实有效,但我会得到一个异常,因为 test_id 未定义(我理解为什么 - gettestcase
在装饰器之后执行,所以它当然会崩溃。
我可以处理此问题的唯一其他方法是在执行任何测试代码之前应用 csv 和 testID。我的问题是——我怎么知道如何将测试与其测试 ID 相关联?什么是优雅的、最小的解决方案?
抱歉这个冗长的问题。如果您需要更多解释,我会密切关注并回答任何问题。
最佳答案
pytest
非常擅长为测试做各种元编程。如果我正确理解你的问题,下面的代码将使用 pytestrail.case
标记进行动态测试标记。在项目根目录中,创建一个名为 conftest.py
的文件并将此代码放入其中:
import csv
from pytest_testrail.plugin import pytestrail
with open('website1.csv') as f:
reader = csv.reader(f)
next(reader)
tests = [r for r in reader]
def pytest_collection_modifyitems(items):
for item in items:
for testid, testname in tests:
if item.name == testname:
item.add_marker(pytestrail.case(testid))
现在您根本不需要使用 @pytestrail.case()
标记测试 - 只需编写其余代码,pytest
将处理标记:
def test_login():
assert True
当 pytest
启动时,上面的代码将读取 website1.csv
并存储测试 ID 和名称,就像您在代码中所做的那样。在测试运行开始之前,pytest_collection_modifyitems
Hook 将执行,分析收集的测试 - 如果测试与 csv 文件中的名称相同,pytest
将添加 pytestrail .case
带有测试 ID 的标记。
关于python - Py.test - 从 csv 将变量应用于装饰器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51365267/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How to nest OR statements in JavaScript? 有没有办法做到这一点:
在 JavaScript 中有没有办法让一个变量总是等于一个变量?喜欢var1 = var2但是当var2更新,也是var1 . 例子 var var1 = document.getElementBy
我正在努力理解这代表什么 var1 = var2 == var3 我的猜测是这等同于: if (var2 == var3): var1 = var2 最佳答案 赋值 var1 = var2
这个问题已经有答案了: What does the PHP error message "Notice: Use of undefined constant" mean? (2 个回答) 已关闭 8
我在临时表中有几条记录,我想从每条记录中获取一个值并将其添加到一个变量中,例如 color | caption -------------------------------- re
如何将字符串转为变量(字符串变量--> $variable)? 或者用逗号分隔的变量列表然后转换为实际变量。 我有 2 个文件: 列名文件 行文件 我需要根据字符串匹配行文件中的整行,并根据列名文件命
我有一个我无法解决的基本 php 问题,我也想了解为什么! $upperValueCB = 10; $passNodeMatrixSource = 'CB'; $topValue= '$uppe
这可能吗? php $variable = $variable1 || $variable2? 如果 $variable1 为空则使用 $variable2 是否存在类似的东西? 最佳答案 PHP 5
在 Perl 5.20 中,for 循环似乎能够修改模块作用域的变量,但不能修改父作用域中的词法变量。 #!/usr/bin/env perl use strict; use warnings; ou
为什么这不起作用: var variable; variable = variable.concat(variable2); $('#lunk').append(variable) 我无法弄清楚这一点
根据我的理解,在32位机器上,指针的sizeof是32位(4字节),而在64位机器上,它是8字节。无论它们指向什么数据类型,它们都有固定的大小。我的计算机在 64 位上运行,但是当我打印包含 * 的大
例如: int a = 10; a += 1.5; 这运行得很完美,但是 a = a+1.5; 此作业表示类型不匹配:无法从 double 转换为 int。所以我的问题是:+= 运算符 和= 运算符
您好,我写了这个 MySQL 存储过程,但我一直收到这个语法错误 #1064 - You have an error in your SQL syntax; check the manual that
我试图在我的场景中显示特定的奖牌,这取决于你的高分是基于关卡的目标。 // Get Medal Colour if levelHighscore goalScore { sc
我必须维护相当古老的 Visual C++ 源代码的大型代码库。我发现代码如下: bIsOk = !!m_ptr->isOpen(some Parameters) bIsOk的数据类型是bool,is
我有一个从 MySQL 数据库中提取的动态产品列表。在 list 上有一个立即联系 按钮,我正在使用一个 jquery Modal 脚本,它会弹出一个表单。 我的问题是尝试将产品信息变量传递给该弹出窗
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
jQuery Core Style Guidelines建议两种不同的方法来检查变量是否已定义。 全局变量:typeof variable === "undefined" 局部变量:variable
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: “Variable” Variables in Javascript? 我想肯定有一种方法可以在 JavaScrip
在语句中使用多重赋值有什么优点或缺点吗?在简单的例子中 var1 = var2 = true; 赋值是从右到左的(我相信 C# 中的所有赋值都是如此,而且可能是 Java,尽管我没有检查后者)。但是,
我是一名优秀的程序员,十分优秀!