Iho*_*nar 3 python testing automated-tests design-patterns pytest
我需要为几个相关的应用程序创建自动化测试,并且在测试之间的测试数据管理方面遇到了一个问题。问题是必须在多个应用程序和/或不同的 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
Run Code Online (Sandbox Code Playgroud)
以下是 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)
Run Code Online (Sandbox Code Playgroud)
为此目的使用fixture非常舒服,因为后置条件,范围和其他甜蜜的东西(请注意,应用程序有很多逻辑和关系,所以我不能简单地对数据进行硬编码或将其迁移到json文件中)例如可以找到类似的东西在tests/app1/conftest.py 和tests/app2/conftest.py 中用于相应地在app1 和app 2 中使用的数据。
所以,这里有两个问题: 1. conftest.py 变成了一个有很多代码的怪物 2. 据我所知,对测试数据使用 conftest 是一种不好的方法还是我错了?
提前致谢!
我将 conftest.py 用于测试数据。
夹具是向测试提供测试数据的推荐方式。
conftest.py 是在多个测试文件之间共享夹具的推荐方式。
至于#2。我认为测试数据使用 conftest.py 是可以的。
现在对于#1,“conftest.py 变得太大了”。
特别是对于顶层 conftest.py 文件,在 test/conftest.py,您可以将该内容移动到一个或多个 pytest 插件中。由于 conftest.py 文件可以被认为是“本地插件”,因此将它们转换为插件的过程并不太困难。
见https://docs.pytest.org/en/latest/writing_plugins.html
您可能有兴趣查看pytest-cases
:它实际上是为了解决这个问题而设计的。您会在文档中找到大量示例,并且案例可以位于专用模块、类或测试文件中 - 这实际上取决于您的需求。例如将两种测试数据生成器放在同一个模块中:
from pytest_cases import parametrize_with_cases, parametrize
def data_a():
return 'a'
@parametrize("hello", [True, False])
def data_b(hello):
return "hello" if hello else "world"
def user_bob():
return "bob"
@parametrize_with_cases("data", cases='.', prefix="data_")
@parametrize_with_cases("user", cases='.', prefix="user_")
def test_with_data(data, user):
assert data in ('a', "hello", "world")
assert user == 'bob'
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅文档。顺便说一下,我是作者;)
归档时间: |
|
查看次数: |
3121 次 |
最近记录: |