Paul
Paul

Reputation: 21985

How to import the src from the tests module in python

I have an application that I'd like to test using unittest but I have some problems. My directory structure is as follows:

root_dir
├── src
│   ├── cmds
│   │   ├── baz.py
│   │   ├── __init__.py
│   │   └── bar.py
│   └── foo.py
└── tests
    ├── cmds.py
    └── __init__.py

I want to test the baz and bar modules from cmds and I'm trying to do

root_dir> python2.7 -m unittest tests.cmds

But in tests.cmds I cannot import the cmds package in my src dir.

How can I make this work?

Basically I want to test the application from root_dir with the src and tests directories separately.

I tried appending src to sys.path, but when I import cmds.baz from tests/cmds.py I still get an AttributeError: 'module' object has no attribute 'cmds' from unittest.

Edit: My import and sys.path statement is:

import sys
sys.path.append('../src')
from cmds.baz import about

And the traceback:

Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/lib/python2.7/unittest/__main__.py", line 12, in <module>
    main(module=None)
  File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs
    self.createTests()
  File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests
    self.module)
  File "/usr/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'cmds'

Upvotes: 5

Views: 5478

Answers (2)

Zaur Nasibov
Zaur Nasibov

Reputation: 22679

A very wrong thing to do is appending a relative path to sys.path. If you want to be sure about the path, make it as follows:

# assuming that the code is in test's __init__.py
import os
import sys
sys.path.insert(0, os.path.abspath( os.path.join(os.path.dirname(__file__), 
                                               '../src/') ))
# now you can be sure that the project_root_dir/src comes first in sys.path

Upvotes: 5

Reto Aebersold
Reto Aebersold

Reputation: 16644

I think you got it almost right. But as you run your tests form the root directory, your path ('../src') is wrong I guess. Maybe you can do something like this:

import os
import sys

ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
sys.path.append(os.path.join(ROOT, 'src'))

from cmds.baz import about

Upvotes: 2

Related Questions