1

So I have a shared library created with boost::python (C++). For the C++ functions inside I have unit-tests that check that they are working. Now I would like to use unit-test to see if I implemented the python interface correctly. For this I thought about using using the python package unittest.

Now my Folder setup is roughly:

project
  |
   -- C++ source (library and boost::python stuff)
  |
   -- build (here the shared library is located)
  |
   -- Test (here I have the python classes that should test the interface)

The test folder has some subfolders that mirror the structure of the python interface, containing lots of small python modules testing the different aspects of the library.

So the question now:

How do I import the shared library into the test?

What I tried so far was in my test_main.py

import sys
sys.path.insert(0,'../build')

But this does not help for the modules inside the test folder. And anyways hardcoding this path into the test-code seems to be hackish. I also don't want to install an untested library just to figure out the tests failed to then uninstall it again.

NOhs
  • 2,780
  • 3
  • 25
  • 59

1 Answers1

1

What you could do is run the tests while you are in the root directory in your case project. You can do python Test/test_name.py. Make sure your build library has a __init__.py file

The only change to the test is you'd have

from build import blah #blah is the component you testing
#test code here
Mike Tung
  • 4,735
  • 1
  • 17
  • 24
  • Actually, this doesn't work on my machine. When I am in the root directory and open a python shell and type: `import build.blah` it works. But If I call `python Test/my_test.py` with `import build.blah` in this file, it does not work. – NOhs Sep 05 '16 at 13:05
  • ... your test.py should have `from build.blah import blah` – Mike Tung Sep 05 '16 at 17:05
  • That is not the issue (I tried your suggestion). It complains that "no module named build". I think in `Test/my_test.py` it still looks relative to `Test` even though it is called from the parent folder. – NOhs Sep 06 '16 at 13:13
  • 1
    If you run python with `PYTHONPATH=.${PYTHONPATH:+:}$PYTHONPATH python Test/test_name.py` this will work. Basically the same as your `sys.path` stuff, but at least you moved it outside and kept your Python tidier. You may have to meddle with `LD_LIBRARY_PATH` also, if your code want to load natively compiled modules. – clacke Sep 08 '16 at 08:46
  • @clacke This seems to work. Only issue now (but I forgot to mention it in the question) is that if I have different build-configurations (debug/release) as subfolders, the configuration is hardcoded in the import statement. But for now I will stick with your suggestion until I feel it becomes too cluttered. – NOhs Sep 08 '16 at 09:13
  • You probably want to move that out of the import statement and into the `*PATH` variables, to be handled by some build / test script. – clacke Sep 08 '16 at 10:59
  • @MrZ I recommend you `pip install nose` and run `nosetests Test/` from root of app. – Mike Tung Sep 11 '16 at 13:47