gpt4 book ai didi

python - 如何在不安装包的情况下运行测试?

转载 作者:太空狗 更新时间:2023-10-29 17:48:08 24 4
gpt4 key购买 nike

我有一些 Python 包和一些测试。文件布局如下 http://pytest.org/latest/goodpractices.html#choosing-a-test-layout-import-rules

Putting tests into an extra directory outside your actual application code, useful if you have many functional tests or for other reasons want to keep tests separate from actual application code (often a good idea):

setup.py   # your distutils/setuptools Python package metadata
mypkg/
__init__.py
appmodule.py
tests/
test_app.py

我的问题是,当我运行测试 py.test 时,出现错误

ImportError: No module named 'mypkg'

我可以通过安装包 python setup.py install 来解决这个问题,但这意味着测试是针对已安装包运行的,而不是本地包,这使得开发非常困难乏味。每当我进行更改并想要运行测试时,我都需要重新安装,否则我将测试旧代码。

我能做什么?

最佳答案

我知道这个问题已经结束了,但我经常使用的一个简单方法是通过 python -m 从根(包)。

$ python -m pytest tests

这是有效的,因为 -m 选项将当前目录添加到 python 路径,因此 mypkg 被检测为本地包(而不是已安装)。

参见: https://docs.pytest.org/en/latest/usage.html#calling-pytest-through-python-m-pytest

关于python - 如何在不安装包的情况下运行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23984973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com