【发布时间】:2022-01-05 06:48:16
【问题描述】:
我正在使用 pytest 来测试我正在进行的项目。我有一个像
这样的项目结构|my_project
||__init__.py
||my_code.py
||test.py
和test.py 看起来像
# contents of test.py
import .my_code
def test_function():
...
...
要运行测试,我可以从这个目录运行python -m pytest。到目前为止一切顺利。
但是要远程运行代码,我必须使用 Pants 构建一个虚拟环境,以便导入语句实际上看起来像:
import long.path.to.my_project.my_code as my_code
我想确保代码在这个虚拟环境中仍然有效,所以现在我有一个名为 test_venv.py 的不同文件,其中包含相同的测试,唯一的区别是导入。
# contents of test_venv.py
import long.path.to.my_project.my_code as my_code
def test_function():
...
...
这确实有效,但是拥有两个几乎相同的测试文件非常烦人。有没有办法让 import 语句参数化,这样我就可以在运行测试时告诉 pytest 我想从哪里导入?
【问题讨论】:
标签: python pytest virtualenv python-module