【发布时间】:2016-02-04 03:11:54
【问题描述】:
问题
如何在测试文件中导入辅助函数而不在test 目录中创建包?
上下文
我想创建一个可以在多个测试中导入的测试辅助函数。说,像这样:
# In common_file.py
def assert_a_general_property_between(x, y):
# test a specific relationship between x and y
assert ...
# In test/my_test.py
def test_something_with(x):
some_value = some_function_of_(x)
assert_a_general_property_between(x, some_value)
使用 Python 3.5,搭配 py.test 2.8.2
当前的“解决方案”
我目前正在通过在我的项目的 test 目录(现在是一个包)中导入一个模块来做到这一点,但如果可能的话,我想用其他机制来做到这一点(这样我的 test 目录没有包,只有测试,测试可以在包的安装版本上运行,推荐here in the py.test documentation on good practices)。
【问题讨论】:
-
pytest 不鼓励
__init__.py-files 似乎很疯狂,但同时除了在测试之间共享帮助函数之外别无选择。我的头发因此变白了。
标签: python unit-testing pytest