【发布时间】:2023-03-24 21:28:01
【问题描述】:
我想要以下项目结构:
|--folder/
| |--tests/
| |--project/
让我们写一个简单的例子:
|--test_pytest/
| |--tests/
| | |--test_sum.py
| |--t_pytest/
| | |--sum.py
| | |--__init__.py
sum.py:
def my_sum(a, b):
return a + b
test_sum.py:
from t_pytest.sum import my_sum
def test_my_sum():
assert my_sum(2, 2) == 5, "math still works"
让我们运行它:
test_pytest$ py.test ./
========== test session starts ===========
platform linux -- Python 3.4.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/step/test_pytest, inifile:
collected 0 items / 1 errors
================= ERRORS =================
___ ERROR collecting tests/test_sum.py ___
tests/test_sum.py:1: in <module>
from t_pytest import my_sum
E ImportError: No module named 't_pytest'
======== 1 error in 0.01 seconds =========
它看不到 t_pytest 模块。它是像 httpie 一样制作的:
https://github.com/jkbrzt/httpie/
https://github.com/jkbrzt/httpie/blob/master/tests/test_errors.py
为什么?我该如何纠正?
【问题讨论】:
-
t_pytest是否已安装或在导入路径上? -
import t_pytest一般工作得好吗?如果不是,这就是它对测试不起作用的原因。 -
我想,不。导入路径上的含义是什么? sys.patch 中的 t_pytest?
-
谢谢,我想我明白了。
标签: python testing pytest project-structure