py.test can't import my module

I am struggeling getting a python import right. What I want to achieve is to have a module with several source files and a test folder with unit tests.

No matter what I do, I can't get py.test-3 to execute my tests. My directory layout looks like this:

.
├── module
│   ├── __init__.py
│   └── testclass.py
└── tests
    └── test_testclass.py

The __init__.py file looks like this:

__all__ = ['testclass']

The testclass.py file looks like this:

class TestClass(object):

    def __init__(self):
        self.id = 1

And my unit test like this:

import pytest
from module import TestClass

def test_test_class():
    tc = TestClass()
    assert(tc.id==1)

No matter how I call py.test-3 I will end up with a:

E   ImportError: No module named 'module'

Answers


First, unless you change the tests/test_testclass.py, you need to change module/__init__.py as follow:

from .testclass import TestClass

__all__ = ['TestClass']

And, when you run py.test set PYTHONPATH environment variable to let the interpreter know when to find modules:

PYTHONPATH=. py.test

I would put in a header of testfile the path execution for pytest:

example:

import os, sys                                                                  
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))

With this I could know the path of any subfolder in my (test) project


Need Your Help

Nodelist with values and parent's attribute as names

xml xpath jenkins

I'm new to XML files and am trying to get out some values using XPath. I will plot this values in Jenkins using the Plot plugin. This takes the element names on the x-axis and the element value on ...

Display diagram and text together (grouped)

css twitter-bootstrap kendo-ui

I'd like to show graphics and text together. The graph on the left side and the corresponding text to its right. I have tried different things, for example, I have tried to implement this with a Pa...