- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对我在测试类的另一个方法中生成的值列表进行 pytest 测试。
问题是我得到: "@pytest.mark.parametrize("number",TestScratch.list_testing)NameError: name 'TestScratch' is not defined" 当我尝试运行时出错。我知道当我将列表作为硬编码列表传递时,即 [0,3, 54,90] 它有效。下面是我的代码:
class TestScratch(object):
@classmethod
def setup_class(cls):
cls.list_testing = []
@classmethod
def setup_method(cls):
pass
def test_populate_list(self):
for i in range(100):
self.list_testing.append(i)
@pytest.mark.parametrize("number",TestScratch.list_testing)
def test_pytest_param(self, number):
assert type(number) == int
@classmethod
def teardown_class(cls):
'''
pass
'''
我也试过 self.class.list_testing但我得到了同样的错误
环境细节:
python :3.6.8
Pytest:5.2.1
最佳答案
您不能在类定义中使用该类。当在导入时读取装饰器时,e。 G。在加载类定义时,而不是在运行时,此时类是未知的。您必须改为在类之外定义列表:
import pytest
def populate_list():
test_list = []
for i in range(100):
test_list.append(i)
return test_list
list_testing = populate_list()
class TestScratch:
def test_populate_list(self):
# this will fail if list_testing could not be populated
assert len(list_testing) > 50
@pytest.mark.parametrize("number", list_testing)
def test_pytest_param(self, number):
# this will be skipped if list_testing could not be populated
assert type(number) == int
你在装饰器中使用的任何参数在加载时只被读取一次,所以试图在运行时初始化它是行不通的。例如here您可以找到参数化工作原理的解释以及为什么无法在运行时添加参数。
关于python-3.x - 使用 "@pytest.mark.parametrize"fixture 调用类级变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61039186/
使用 python 3.8 和 pytest 5.3.2 在 conftest.py 中,我有一个从 json 配置文件读取数据的装置,为了在多个测试中重用从 json 配置文件中读取的数据。 配置.
我想知道是否可以使用 given 参数来自 pytest 的 parametrize 函数。 示例: import pytest from hypothesis import given from h
我正在尝试创建一个 PowerShell 脚本,该脚本将部署 Azure 开发测试实验室的实例,在那里创建一个环境,以及 databricks、azure 数据工厂等资源。这是我负责的实验室模板的一部
我有这段 Python 代码: import pytest class Apple: @pytest.mark.parametrize("kind", ['fruit', 'veg', 'nu
我隐含地把它变成了一个社区维基,因为答案可能非常广泛。我正在与一家初创公司合作以实现以下目标。 在医学研究中,患者的医疗记录可以包含关于患者特定诊断的无限量数据,例如吸烟者患肺癌的几率更高,但这并不一
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我有一个带有非常大集合的 mongo 数据库,我需要用 Pytest 对其运行测试。我正在尝试使用 mark.parametrize dectorator 但使用 pymongo.cursor Cur
我希望一个参数化依赖于一个较早的参数化: @pytest.mark.parametrize("locale_name", LOCALES) @pytest.mark.parametrize("mone
我有多个函数可以引发 ValueError 来测试。到目前为止,我的解决方案是: import pytest import record @pytest.fixture(name='raised')
我基于 ReaderT design pattern 构建了一个项目.我没有使用类型类方法进行依赖注入(inject),而是选择使用简单的处理程序注入(inject)作为函数参数。这部分工作正常,因为
是否可以创建一个可以使用的 Extractor 对象,例如: val x = 42 x match { case GreaterThan(80) => println("5") case Gr
我有一个测试功能,可以修补一些东西。一旦我达到超过 2 或 3 个补丁,功能开始看起来很糟糕 def test_bla(): with patch(...): with pat
我有一个测试功能,可以修补一些东西。一旦我达到超过 2 或 3 个补丁,功能开始看起来很糟糕 def test_bla(): with patch(...): with pat
我有一个遗留接口(interface),它以字符串的形式为我提供实例类型,例如“int”、“float”等。 我想出了这两个函数来解决这个问题: template T type_factory(co
当参数化引发预期异常的测试时,我一直使用以下形式: import pytest class SomeException(Exception): pass class AnotherExcept
有没有办法将可调用指定为pytest.mark.parametrize()参数,以便仅在测试时动态生成参数选择运行? 为了生成参数,我需要执行一些昂贵的操作,并且我只想在选择运行测试时执行这些操作。
我正在尝试将三个不同的固定装置传递给我的 pytest.mark.parameterize 装饰器,如下所示: @pytest.mark.parametrize("credentials, retur
从文档SpannerQueryDatabaseInstanceOperator接受查询参数。然而,没有像PostgresOperator这样聪明的东西,它也接受一个parameters参数来使用占位符
我有一个函数可以读取 yaml 文件并生成测试迭代作为字典列表,如下所示: Iterations = lib_iterations() print Iterations Iterations = [{
Sample_test.py @pytest.mark.parametrize(argnames="key",argvalues=ExcelUtils.getinputrows(__name__),s
我是一名优秀的程序员,十分优秀!