1

I'm trying to run a Python project's unit tests using py.test. Until recently I've used nose and it ran the tests against my local source code instead of the installed package, but py.test seems to want to run against the installed package, meaning I have to run python setup.py install before every test run.

The project is organized like so:

/project
    /project  
        __init__.py
        project_file.py
    /test
        __init__.py
        /test_project
            test_project_file.py

The only way I've found to get py.test to run against the local code is to run python -m pytest in the top /project folder. It's very strange, as running py.test in the same location runs against the installed code. According to the docs these commands ought to be equivalent, and --version returns the same info for both.

Is there a standard way to tell py.test to run against specific code? I haven't been able to find a clear answer in py.test's documentation.

Skylar
  • 105
  • 11

1 Answers1

2

My first mistake was setting up my project using python setup.py install instead of python setup.py develop. Pytest expects you to use the latter, which seems like good practice anyway.

If it's necessary to run against local code when the same code is installed, there does seem to be a way to do it. This SO post mentions a "hidden feature" of pytest, where you can put a conftest.py file in your root directory to tell pytest to run using your local code. Pytest seems to use the location of your overall configuration file to determine what it thinks your root directory is. This file is pytest.ini in my case.

As a curiosity, I believe that python -m pytest running using the local code where py.test did not was due to Python's default behavior of adding the directory that it's called in to sys.path.

Community
  • 1
  • 1
Skylar
  • 105
  • 11
  • The docs Skylar has updated to reflect this - https://docs.pytest.org/en/latest/nose.html#unsupported-idioms-known-issues – Jan Benes Nov 15 '17 at 16:20