Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest cannot find package when I put tests in a separate directory

I have a very basic example of a Python package and a test which I'm trying to run with pytest

package
 \--
   __init__.py
   spam.py
   spam_test.py

__init__.py is empty, spam.py defines a single function func() and spam_test.py is:

import package.spam

def test_func():
    assert(package.spam.func() == 5)

In the root directory I run py.test and it all works, returning one passing test.

I thought that I could also structure things in this way:

package
 \--
   __init__.py
   spam.py
tests
 \--
   spam_test.py

But now when I run py.test in the root I get:

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: C:\Users\MattUser\Documents\m_drive\notebooks\testing, inifile:
collected 0 items / 1 errors

=================================== ERRORS ====================================
_____________________ ERROR collecting tests/spam_test.py _____________________
ImportError while importing test module 'C:\Users\MattUser\Documents\m_drive\not
ebooks\testing\tests\spam_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests\spam_test.py:1: in 
    import package.spam
E   ModuleNotFoundError: No module named 'package'
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.19 seconds ===========================

I've tried various things to no luck. What am I doing wrong??

like image 320
Matthew Daws Avatar asked Mar 10 '17 17:03

Matthew Daws


People also ask

Where do I put the pytest test?

While the pytest discovery mechanism can find tests anywhere, pytests must be placed into separate directories from the product code packages. These directories may either be under the project root or under the Python package.

Why do I need __ init __ py?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.

How do I run a pytest on a specific file?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .

How do I run multiple pytest files?

Run Multiple Tests From a Specific File and Multiple Files To run all the tests from all the files in the folder and subfolders we need to just run the pytest command. This will run all the filenames starting with test_ and the filenames ending with _test in that folder and subfolders under that folder.


1 Answers

Well, I got it working. Add an empty __init__.py file in the tests directory.

Why does this work? From the official docs:

determine basedir: this is the first “upward” (towards the root) directory not containing an __init__.py.

So with tests not containing __init__.py, pytest determines that to be the basedir. Adding __init__.py forces pytest to recurse up to the root, from whence it can import what it needs to...

like image 117
Matthew Daws Avatar answered Oct 10 '22 15:10

Matthew Daws