Nosetest & imports

I am pretty new to Python. Currently I am trying out PyCharm and I am encountering some weird behavior that I can't explain when I run tests.

The project I am currently working on is located in a folder called PythonPlayground. This folder contains some subdirectories. Every folder contains a __init__.py file (including the top-level directory). Some of the folders contain nosetest tests.

When I run the tests with the nosetest runner from the command line inside the project directory, I have to put "PythonPlayground" in front of all my local imports. E.g. when importing the module called "model" in the folder "ui" I have to import it like this:

from PythonPlayground.ui.model import *


But when I run the tests from inside Pycharm, I have to remove the leading "PythonPlayground" again, otherwise the tests don't work. Like this:

from ui.model import *


I am also trying out the mock framework, and for some reason this framework always needs the complete name of the module (including "PythonPlayground"). It doesn't matter whether I run the tests from command line or from inside PyCharm:

with patch('PythonPlayground.ui.models.User') as mock


Could somebody explain the difference in behavior to me? What is the correct behavior? And how can I make PyCharm behave like the command line tool, or vise versa?
1 comment
Comment actions Permalink
Most likely the main difference is in the working directory from which you start the tests, and in the way PYTHONPATH is initialized. When you run tests from PyCharm, it adds the root directory of the project to PYTHONPATH, so you don't need to prepend it to your import statements. I would expect that the nosetests runner would do the same, but looks like it doesn't.
0

Please sign in to leave a comment.