如何在测试运行之外使用pytest夹具?

时间:2019-07-17 09:11:38

标签: python database pytest fixtures

我有一组装置来设置相当大的数据集。设置结果存储在数据库(scope=function)中,用于报表渲染回归测试。这些固定装置没有参数化,因此只有一种设置,但是回归测试将其中一些相互依存的固定装置作为参数来访问对象,而无需其他查询。 pytest使用内存数据库并在每次测试后回滚,因此在完成测试后,设置将不可用。

我想在pytest之外使用该数据库设置进行演示和前端自动化测试。

如何在开发数据库中获取pytest固定装置的结果?

一个没有细节的示例,以显示灯具的结构:

@pytest.fixture
def customer():
    return mommy.make(Customer, name='Customer LTD')

@pytest.fixture(autouse=True)
def inventory_types(customer):
    return seeder.seed_inventory_types('A,B,C', customer=customer)


@pytest.fixture(autouse=True)
def inventory(
    good,
    bad,
    ugly,
    common,
    ...
):
    return

@pytest.fixture
def good(customer, patterns):
    vehicle = mommy.make(
        Inventory, 
        name='Good', 
        type=inventory_types.A, 
        customer=customer
    )

@pytest.fixture
def bad(customer, patterns):
    return make(
        Inventory, 
        name='Bad', 
        type=inventory_types.A,  
        customer=customer
    )

@pytest.fixture
def ugly(customer, patterns):
    return mommy.make(
        Inventory, 
        name='Ugly', 
        type=inventory_types.B, 
        customer=customer
    )

@pytest.fixture
def common(customer, patterns):
    return mommy.make(
        Inventory, 
        name='Common', 
        type=inventory_types.C, 
        customer=customer
    )


def test(good, customer):
    assert good in customer.inventory

1 个答案:

答案 0 :(得分:0)

看起来你可以像这样调用由fixture装饰器包装的函数:customer.__pytest_wrapped__.obj()。显然,这不是公共 API,因此可能会发生变化。

看起来另一种方法是以不同的方式构建您的夹具代码,以便它也公开一个正常的函数。请参阅此处提到的 name 参数:https://docs.pytest.org/en/stable/deprecations.html#calling-fixtures-directly