Python pytest夹具';狗';找不到

Python pytest夹具';狗';找不到,python,python-3.x,pytest,fixtures,Python,Python 3.x,Pytest,Fixtures,例如,我使用pytest调用以下代码 class Dog: def __init__(self, name, age, breed): self.name = name self.age = age self.breed = breed def test_dog_age(dog): dog_age = dog.age assert dog_age == 7 if __name__ == '__main__':

例如,我使用pytest调用以下代码

class Dog:
   def __init__(self, name, age, breed):
        self.name = name
        self.age = age
        self.breed = breed

def test_dog_age(dog):
    dog_age = dog.age
    assert dog_age == 7
    
if __name__ == '__main__':
    dog1 = Dog('rex', 7, 'labrador')
    test_dog_age(dog1)
我在说一个错误

未找到E夹具“dog” 可用装置:缓存、capfd、capfdbinary、caplog、capsys、capsysbinary、doctest_命名空间、monkeypatch、pytestconfig、record_属性、record_testsuite_属性、record_xml_属性、recwarn、tmp_路径、tmp_路径_工厂、tmpdir、tmpdir_工厂 使用“pytest--fixtures[testpath]”获取关于它们的帮助


第1行出现了意外的缩进。

为了更清楚地说明这一点:pytest基本上是通过收集它找到的所有测试,应用所有定义的固定装置和挂钩并执行测试来工作的

在您的情况下,您的测试将类似于:

def test_dog_age():
狗=狗('rex',7',拉布拉多')
断言dog.age==7
您可以在命令行调用
pytest
,以执行测试。您也可以使用特定的测试文件调用它,比如说
pytest\u dog.py


添加到测试函数中的任何参数都将作为测试夹具处理,如果未找到具有该名称的夹具(仅按名称查找夹具),则会出现上述错误。

如果为测试函数提供参数,pytest会将其视为夹具并查找具有该名称的夹具,因此,消息-否则测试功能不应具有任何参数。您不能直接调用测试函数-您必须调用pytest,它将发现测试。检查一下。我在下面启动狗。我该怎么做呢?