General Question

paulc's avatar

How do you structure a Python application inclusive of unit tests?

Asked by paulc (2924points) June 25th, 2008

I’ve seen a variety of application structures out there just browsing some projects on Google Code. I’ve also read the docs on http://python.org regarding the use of the unittest package. My problem is that I want to have my app structured something like this but I can’t for the life of me get the pathing to see the modules in my source directory. I’d rather not have to put the tests in with the actual code but I’m not sure how to get those tests to run when they can’t import the modules I need. Please forgive my ignorance as I’m just beginning with Python.

Observing members: 0 Composing members: 0

5 Answers

andrew's avatar

Just make sure your “app” directory is on your PYTHONPATH. Then you can say in your test files:

from app.source.models import horror

paulc's avatar

@andrew, Thanks. Do you know if there is a way to specify pathing without using an environment variable? Does having a lot of paths specified in PYTHONPATH slow down anything?

andrew's avatar

@paulc: One thing we do is just do a symlink (or alias) in your site-packages directory. What OS are you on?

I have maybe 20 packages there, so no, it doesn’t slow things down.

paulc's avatar

@andrew, I’m running OS X so the symlink is a good option which I’ll likely use. I’m just used to being able to specify my paths per application if needed (in other languages that is).

Thanks for your help.

andrew's avatar

@paulc, you can also make your symlink to your “source” directory, but call the symlink whatever you want. So you can do:

from somename.models import horror

Also, we make symlinks to the site-packages in ~ so we don’t have to keep typing /Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/site-packages/

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther