Allia Bordes
Allia Bordes

Reputation: 61

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. The if I run the pytest tests command from the directory mypackage I get the following error:

(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). What am I missing?

Hope you can help,

Allia

Upvotes: 1

Views: 713

Answers (1)

Markus Unterwaditzer
Markus Unterwaditzer

Reputation: 8244

It should be import mypackage.dira or from ..dira import ???.

Upvotes: 1

Related Questions