什么是使用pytest管理测试数据的正确方法? [英] What is a correct approach to manage test data using pytest?

查看:104
本文介绍了什么是使用pytest管理测试数据的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为几个相关的应用程序创建自动化测试,并且在测试之间遇到测试数据管理方面的一个问题.问题在于,必须在多个应用程序和/或不同的API之间共享相同的数据.现在,我有了pytest的下一个结构,该结构对我来说很好,但是我怀疑在conftest.py中使用测试数据管理是正确的方法:

I need to create automated tests for several related apps and faced one problem with test data management between tests. The problem is that same data must be shared between several apps and/or different APIs. Now I have next structure with pytest that working good for me but I doubt that use test data managment in conftest.py is correct approach:

总体结构如下:

tests/
    conftest.py
    app1/
        conftest.py
        test_1.py
        test_2.py
    app2/
        conftest.py
        test_1.py
        test_2.py
test_data/
    test_data_shared.py
    test_data_app1.py
    test_data_app2.py

以下是tests/conftest.py中测试数据的示例:

Here is example of test data in tests/conftest.py:

from test_data.test_data_shared import test_data_generator, default_data

@pytest.fixture
def random_email():
    email = test_data_generator.generate_random_email()
    yield email
    delete_user_by_email(email)

@pytest.fixture()
def sign_up_api_test_data(environment, random_email):
"""
environment is also fixture, capture value from pytest options
"""
    data = {"email": random_email, "other_data": default_data.get_required_data(), "hash": test_data_generator.generate_hash(environment)}
    yield data
    do_some_things_with_data(data)

为此使用夹具非常舒适,因为后置条件,作用域和其他有趣的东西(请注意,应用程序具有很多逻辑和关系,因此,我不能简单地对数据进行硬编码或将其迁移到json文件中)对于相应地在app1和app 2中使用的数据,可以在tests/app1/conftest.py和tests/app2/conftest.py中找到类似的内容.

Its very comfort to use fixture for that purposes, because postconditions, scopes and other sweet things (note that apps have a lot of logic and relationship, so I cannot simply hardcode data or migrate it in json file for example) Similar things can be found in tests/app1/conftest.py and tests/app2/conftest.py for data that used in app1 and app 2 accordingly.

因此,这是2个问题:1. conftest.py变成了很多代码的怪兽2.据我所知,对测试数据使用conftest是一种不好的方法,或者我错了?

So, here is 2 problems: 1. conftest.py become a monster with a lot of code 2. as I know, using conftest for test data is a bad approach or I'm wrong?

提前谢谢!

推荐答案

我将conftest.py用于测试数据.
夹具是向测试提供测试数据的推荐方法.
推荐使用conftest.py在多个测试文件之间共享灯具.

I use conftest.py for test data.
Fixtures are a recommended way to provide test data to tests.
conftest.py is the recommended way to share fixtures among multiple test files.

至于#2.我认为可以使用conftest.py来测试数据.

So as for #2. I think it's fine to use conftest.py for test data.

现在#1,"conftest.py变得太大".

Now for #1, "conftest.py becoming too big".

特别是对于顶层conftest.py文件,在test/conftest.py,您可以将该内容移动到一个或多个pytest插件中.由于conftest.py文件可以被视为本地插件",因此将它们转换为插件的过程并不难.

Especially for the top level conftest.py file, at test/conftest.py, you can move that content into one or more pytest plugin. Since conftest.py files can be thought of as "local plugins", the process for transforming them into plugins is not too difficult.

请参见 https://docs.pytest.org/en/latest/writing_plugins.html

这篇关于什么是使用pytest管理测试数据的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆