pytest设备不工作为什么?

2024-06-08 16:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用PyTest,但无法获取如何设置fixture。我试过以下代码:

import pytest
import random

@pytest.fixture()
def setup():
    a = random.randint(0,10)

def test(setup):
    assert 3 > a
if __name__ == '__main__':
    pytest.main()

我得到了"NameError: name 'a' is not defined".

官方文档中的示例也不起作用。怎么了?我需要类似于设置/拆卸的功能。但我不想用unittest。有人能给我一个工作装置的例子吗(安装型和拆卸型)?我想在类中编写一些函数测试和方法测试,因此我的第二个问题是使用fixture和类/方法的工作示例。我只需要看看python中fixture的工作示例。

是否有一个不同的python3单元测试框架,其断言与PyTest中的一样简单?


Tags: 方法代码nametestimport示例pytestmain