如何使用tests目录中的tests文件运行pytest

时间:2017-02-04 23:11:45

标签: python pytest

我有一个包含以下结构的包:

mypackage
|__ __init__.py
|
|__ dira
|   |__ __init__.py
|   |__ classa.py
|
|__ tests
    |__ __init__.py
    |__ test_classa.py

test_classa.py中,我使用语句import dira。如果我从目录pytest tests运行mypackage命令,则会收到以下错误:

(myenv) D:\work\dev\mypackage>pytest tests
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: D:\work\dev\mypackage, inifile:
collected 0 items / 1 errors

=================================== ERRORS ====================================
__________________ ERROR collecting tests/test_classa.py ___________________
ImportError while importing test module 'D:\work\dev\mypackage\tests\test_classa.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests\test_classa.py:6: in <module>
    import dira
E   ImportError: No module named dira
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!

请注意,我已经使用PyCharm创建了一个测试配置(如魅力)。我错过了什么?

希望你能提供帮助,

Allia

1 个答案:

答案 0 :(得分:1)

应为import mypackage.dirafrom ..dira import ???