pythonpytest

Why doesn't my current directory show up in the path using pytest on Windows?


I have the following folder structure;

myapp\
  myapp\
     __init__.py
  tests\
     test_myapp.py

and my pwd is

C:\Users\wwerner\programming\myapp\

I have the following test setup:

import sys
import pprint

def test_cool():
    pprint.pprint(sys.path)
    assert False

That produces the following paths:

['C:\\Users\\wwerner\\programming\\myapp\\tests',
 'C:\\Users\\wwerner\\programming\\envs\\myapp\\Scripts',
 'C:\\Windows\\system32\\python34.zip',
 'C:\\Python34\\DLLs',
 'C:\\Python34\\lib',
 'C:\\Python34',
 'C:\\Users\\wwerner\\programming\\envs\\myapp',
 'C:\\Users\\wwerner\\programming\\envs\\myapp\\lib\\site-packages']

And when I try to import myapp I get the following error:

ImportError: No module named 'myapp'

So it looks like it's not adding the current directory to my path.

By changing my import line to look like this:

import sys
sys.path.insert(0, '.')
import myapp

I am then able to import myapp with no problems.

Why does my current directory not show up in the path when running pytest? Is my only workaround to insert . into the sys.path? (I'm using Python 3.4 if it matters)


Solution

  • Ahah!

    After comparing the layout of my cookiecutter repo, it turns out to be way more simple (and better) than that.

    tests/
        __init__.py
        test_myapp.py
    

    A simple addition of the __init__.py file to my test dir allows me to run py.test from my main directory.