简体   繁体   English

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

[英]How to run pytest with tests files in a tests directory

I have a package with the following structure:我有一个具有以下结构的包:

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

In test_classa.py , I use the statement import dira .test_classa.py ,我使用了import dira语句。 The if I run the pytest tests command from the directory mypackage I get the following error:如果我从目录mypackage运行pytest tests命令,我会收到以下错误:

(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 !!!!!!!!!!!!!!!!!!!

Note that I have created a test configuration with PyCharm which works (like a charm).请注意,我已经使用 PyCharm 创建了一个测试配置,它可以工作(就像一个魅力)。 What am I missing?我错过了什么?

Hope you can help,希望能帮到你

Allia艾莉亚

It should be import mypackage.dira or from ..dira import ???它应该是import mypackage.dirafrom ..dira import ??? . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM