473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

organizing python code with tests

Hello,

At the moment, I place all the code of my project in a src/ directory,
and all the tests in a sibling tests/ directory, so for instance a
sample project can look like this:

doc/
...
src/
play.py
write.py
tests/
test_play.py
test_write.py

While this works fine, I have a couple of questions:

1) To make this scheme work, each test file begins with
sys.path.insert(0, '../src'). I wonder if there's a better way to do
this, because this one doesn't look flexible (what if the code gets
complex and I subdivide it to directories in src/...)

2) Is there a better way to organize this ? Preferably I'm looking for
a single method that is applicable to:
* Complete stand-alone applications with a single entry-point
* Libraries with several files that should be imported by the users

Perhaps this is too much to ask, but hope dies last :-)

Thanks in advance


Aug 15 '08 #1
1 1728
eliben wrote:
Hello,

At the moment, I place all the code of my project in a src/ directory,
and all the tests in a sibling tests/ directory, so for instance a
sample project can look like this:

doc/
...
src/
play.py
write.py
tests/
test_play.py
test_write.py

While this works fine, I have a couple of questions:

1) To make this scheme work, each test file begins with
sys.path.insert(0, '../src'). I wonder if there's a better way to do
this, because this one doesn't look flexible (what if the code gets
complex and I subdivide it to directories in src/...)
You would then make packages and access your modules within tests using
Python dot notation. In this case though, you'll probably want to rename
src into a more specific package name.

If you don't want to mess up with the python path within each test file,
you may want to make the package your testing available globally for
import. A quick way to do it is by setting the PYTHONPATH environment
variable. This can quickly become tedious if you work on multiple
packages. Another solution would be to link your packages within
site-packages, which is (hopefully ;) already in your path. The ultimate
solution to do that is to make you package an egg and install it in
development mode (ie: python setup.py develop). This is made very easy
by Paste Script and its basic_package template (paster create -t
basic_package), see http://pythonpaste.org/script/developer.html for
more on that. A good thing with having your package in your Python path
is that you can fire a interactive interpreter from anywhere and play
with your API.
2) Is there a better way to organize this ? Preferably I'm looking for
a single method that is applicable to:
* Complete stand-alone applications with a single entry-point
* Libraries with several files that should be imported by the users
Does it actually make a difference when it comes to unit testing? Even
for standalone apps you'll want to unit test each module composing the
app, won't you ? ;-)
Aug 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
38
by: kbass | last post by:
In different articles that I have read, persons have constantly eluded to the productivity gains of Python. One person stated that Python's productivity gain was 5 to 10 times over Java in some in...
36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
25
by: abhinav | last post by:
Hello guys, I am a novice in python.I have to implement a full fledged mail server ..But i am not able to choose the language.Should i go for C(socket API) or python for this project? What are the...
5
by: ivarnelispam | last post by:
Hello all, I'm starting work on what is going to become a fairly substantial Python project, and I'm trying to find the best way to organize everything. The project will consist of: - A few...
10
by: Brendan Miller | last post by:
What would heavy python unit testers say is the best framework? I've seen a few mentions that maybe the built in unittest framework isn't that great. I've heard a couple of good things about...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.