문제

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

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top