我是Python的新手,正在尝试为我的第一个应用程序编写测试。
简单的应用程序结构:
- app
- tests
__init__.py
- test_main.py
- __init__.py
- main.py
main.py
包含main_func
test_main.py:
from main import main_func
def test_check():
assert main_func() is True
如果我手动运行测试,在
app
目录下输入命令pytest
- 会出现以下错误:
C:\Users\*****\PycharmProjects\checker3>pytest
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-3.0.3, py-1.4.31, pluggy-0.4.0
rootdir: C:\Users\*****\PycharmProjects\app, inifile:
collected 0 items / 1 errors
=================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_main.py _____________________
ImportError while importing test module 'C:\Users\*****\PycharmProjects\app\tests\test_main.py'.
Original error message:
'No module named 'main''
Make sure your test modules/packages have valid Python names.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.14 seconds ===========================
包名是正确的。此外,我使用 PyCharm
集成开发环境,它没有跟踪到任何导入包错误。
此外,当我通过IDE执行pytest测试配置时,测试是正常工作的。
tests
文件夹中,你需要一个空的__init__.py
文件。 - 2ps