Tudor Timi
Tudor Timi

Reputation: 7573

Python repository organization w.r.t. tests and PyDev

I'm trying to set up a Python repository for some code. I've read the Structuring Your Project tutorial and set everything up as suggested. Concretely, I have the following directory structure:

repo_root/
   some_module.py
   tests/
       context.py
       test_some_module.py

The context.py file contains exactly what the tutorial suggests:

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import some_module

The test_some_module.py file imports some_module like so:

from context import some_module

instead of

from .context import some_module

as the site suggests.

I'm using PyDev and running the unit tests by right-clicking on the test file and selecting Run As/unittest. This causes the test to run in the tests directory, where context is visible and the import is successful. PyDev shows Unresolved import. I'm guessing this is because it runs the parser from the project root. If I add tests to the PYTHONPATH it still doesn't work. Only if I add an __init__.py file in tests does PyDev stop showing errors, because it's treating tests as a library.

What is the correct way to set up my repository? Are tests supposed to only be called in the tests directory or should they also be start-able from the repository root?

Upvotes: 0

Views: 51

Answers (1)

Fabio Zadrozny
Fabio Zadrozny

Reputation: 25332

PyDev doesn't really work well if you don't have __init__ files in the package (even if it's for tests), so, my suggestion is adding __init__ there.

Upvotes: 1

Related Questions