5

This is currently the way I'm organizing it.

myProject/
    README
    subproject1/
        __init__.py
        classA.py
        classB.py
        driver.py
        basicTest.py

Driver imports from classA and classB and provides some additional functionality. basicTest also imports from Driver and classA.

I've been using nosetests to discover and run all the tests from basicTest.py. That works great if I'm in the subproject1 directory. However, it doesn't work if I'm in the toplevel myProject directory. The imports fail since the relative paths are then all wrong. I need to run nosetests from the top directory since I'm integrating Travis-CI and it runs nosetests from the top level.

It seems like numpy does all imports relative to the top directory. But, then I can't run the tests from any of the subdirectories, which can be annoying.

Looking around it seems like the only way to make the tests runnable from both directories would be to mess with sys.path at the top of basicTest.py. That seems like a bad idea. This suggests what also seems to be a hacky solution.

Is there a clean solution out there, or is the best approach to just import everything relative to the root and run all the tests from there?

Update: I decided a temporary solution would be to tell Travis-CI to cd into subproject1 and then run nosetests. This required the fewest number of changes on my part to get everything working. It feels funny though and I'm still looking for a better layout.

3

I recommend you to use py.test : it's a good testing framework

and you have two way to structure your project, 1st one is:

myProject/
    README
    subproject1/
        __init__.py
        classA.py
        classB.py
        driver.py
        tests/
            basicTest.py

and the 2nd one is:

myProject/
    README
    subproject1/
        __init__.py
        classA.py
        classB.py
        driver.py
    tests/
        subproject1/
            basicTest.py
        subproject2/
            test2.py

it depends on your needs, personally I always the second method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.