【发布时间】:2020-07-09 22:31:47
【问题描述】:
我需要为几个相关的应用程序创建自动化测试,并且在测试之间的测试数据管理方面遇到了一个问题。 问题是必须在多个应用程序和/或不同的 API 之间共享相同的数据。 现在我有了 pytest 的下一个结构,它对我很有用,但我怀疑在 conftest.py 中使用测试数据管理是正确的方法:
整体结构如下:
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中的测试数据示例:
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 文件中) 类似的东西可以在 tests/app1/conftest.py 和 tests/app2/conftest.py 中找到相应的在 app1 和 app 2 中使用的数据。
所以,这里有两个问题: 1. conftest.py 变成了一大堆代码的怪物 2.据我所知,使用 conftest 测试数据是一种不好的方法还是我错了?
提前致谢!
【问题讨论】:
标签: python testing design-patterns automated-tests pytest