views:

76

answers:

1

I have problem concerning python packages and testing. I'm writing an application using wx python and have the following basic folder/package structure for the gui parts. The mainframe.py window has a dependency to the logpane.py panel, which is easily imported using an absolute import in mainframe.py:

import guiapp.utilviews.logpane

Folder/package layout:

mainapp
   - main.py
   guiapp
       - __init__.py
       utilviews
           - __init__.py
           - logpane.py
           - other stuff...
       mainview
           - __init__.py
           - mainframe.py
           - other stuff here...
       other views...

However, for the gui parts of the application I'd like to, in addition to unit tests (and they pose no problem), have a small "test"/example for each view. That is, for the logpane, I'd like to have small runnable example that will lauch a bare bone application showing the view to test that layout is ok etc. The way I started out was that I had a

if __name__ == "__main__":
    # create some small window and a wx app
    # instanciate the panel and launch the app
    pass

in the actual implementation file of the panel/view itself. This worked wonders for the logpane. I could just run the file standalone to have a look at the view and see that it held together.

But of course the strategy broke down for the mainframe.py since the dependency to logpane.py couldn't be resolved. This since the current main module is the mainframe.py and the only way mainframe.py can reach the utilviews package is if the main/launched module is further up in the tree.

So how do I structure these types of tests? For unit tests I use nose which is nice and takes care of the problem since nose is launched per the mainapp folder (so all intrapackage references works). But these tests are like small stand-alone programs. I simply can't clutter the mainapp folder with these python files (there will be many such files). Are there any other way I can accomplish this?

Any input is valuable.

Thanks, Rickard

+2  A: 

You could use PYTHONPATH. Set it to your main project directory, before executing your test file. It will then be able to resolve all your imports just as if you would be executing from that directory.

$ find
.
./test
./test/test.py
./some
./some/__init__.py

$ cat some/__init__.py 
x = 10

$ cat test/test.py 
import some
print some.x

$ cd test
$ export PYTHONPATH=..
$ python test.py
10
Frank
Thank you. This is a solution. It adds a bit of hassle though since I'd like other developers to be able to just check the project out from SVN and then be able to run the tests. Maybe I could add some logic in the if __name__ == "__main__" clause that traveses upwards from the CWD and appends to the path programmatically ()via sys.path) when it reaches the mainapp folder. That should make it possible to have the working copy anywhere. I'll try that! Thanks!
Rickard
Yes, modifying `sys.path` works too, but I consider that quite hackish. If you combine your tests into some `test/` directory though, you could simply provide a small shell script for executing the tests, which sets up `PYTHONPATH` before.
Frank