473,320 Members | 1,848 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,320 software developers and data experts.

Import path for unit tests

Howdy all,

My practice when writing unit tests for a project is to make 'test/'
subdirectories for each directory containing modules I want to test.

project-foo/
+-- lib/
| +-- test/
+-- data/
+-- gui/
| +-- test/
+-- server/
+-- test/

This means that I need to use relative paths to import the subject
code into the unit test module.

import unittest

import sys
sys.path.append('..')
import foomodule

class Test_Foo(unittest.TestCase):
# ...

This works, so long as the foomodule is *not* in the path before the
appended '..' directory. When writing unit tests for a development
version of a package that is already installed at an older version in
the Python path, this fails: the unit tests are not importing the
development version of foomodule.

What is the common idiom here? I can conceive of several possible ways
to get around it, all of which seem hackish to some degree.

--
\ "It's not what you pay a man, but what he costs you that |
`\ counts." -- Will Rogers |
_o__) |
Ben Finney
Dec 2 '05 #1
2 2032
Ben Finney wrote:
This works, so long as the foomodule is *not* in the path before the
appended '..' directory. When writing unit tests for a development
version of a package that is already installed at an older version in
the Python path, this fails: the unit tests are not importing the
development version of foomodule.
Why not just insert foomodule's directory in front of the other entries in
sys.path instead of at the end?

What is the common idiom here? I can conceive of several possible ways
to get around it, all of which seem hackish to some degree.


I don't know if it is the common idiom, but I tend to write:

TESTDIR = os.path.dirname(os.path.abspath(__file__))
PROJECTDIR = os.path.dirname(TESTDIR)
if not TESTDIR in sys.path:
sys.path.insert(1, TESTDIR)
if not PROJECTDIRDIR in sys.path:
sys.path.insert(1, PROJECTDIR)

That gets put in a framework.py file in the test directory. run_tests.py
(in the same folder) looks like:

import unittest, os, sys
if __name__ == '__main__':
execfile(os.path.join(sys.path[0], 'framework.py'))

if __name__=='__main__':
suite = unittest.TestSuite()
for testfile in os.listdir(TESTDIR):
if testfile.startswith('test_') and testfile.endswith('.py'):
testfile = os.path.splitext(testfile)[0]

module = __import__(testfile)
suite.addTest(module.suite())

unittest.TextTestRunner(verbosity=2).run(suite)

and all the test files import framework. That way I can run an individual
test_xxx.py, or use run_tests.py to run all test files, and I can start
them from the test directory, the project directory, or any other
directory.
Dec 2 '05 #2
Duncan Booth <du**********@invalid.invalid> wrote:
Ben Finney wrote:
What is the common idiom here? I can conceive of several possible
ways to get around it, all of which seem hackish to some degree.


I don't know if it is the common idiom, but I tend to write:

TESTDIR = os.path.dirname(os.path.abspath(__file__))
PROJECTDIR = os.path.dirname(TESTDIR)
if not TESTDIR in sys.path:
sys.path.insert(1, TESTDIR)
if not PROJECTDIRDIR in sys.path:
sys.path.insert(1, PROJECTDIR)


Thankyou, that works better than what I was doing.

But it's my idea of "hackish" :-) It works, and is comprehensible. But
it's pretty heavyweight for something I need to do all the time (i.e.
writing unit test modules for various projects).

Anyone else have a preferred way of doing this? Or will I have to wait
until PEP 328 is integrated?

<URL:http://www.python.org/peps/pep-0328.html>

--
\ "Laurie got offended that I used the word 'puke.' But to me, |
`\ that's what her dinner tasted like." -- Jack Handey |
_o__) |
Ben Finney
Dec 3 '05 #3

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

Similar topics

6
by: Tom Verbeure | last post by:
Hello All, so I'm now convinced that unit testing is really the way to go and I want to add it to an existing project. The first thing that I find out, is that, well, it's hard work. Harder than...
0
by: Remy C. Cool | last post by:
Hello, My application uses a remote import scheme using the sys.path_hooks solution as explained in PEP302 (http://www.python.org/peps/pep-0302.html) The importer works fine for packages,...
97
by: Kjetil Torgrim Homme | last post by:
often when re-factoring code, I need to change the indent level of some chunk of code. due to the lack of an end marker, my Emacs has to use heuristics when re-indenting, and this will...
14
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
10
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to...
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
6
by: Vyacheslav Maslov | last post by:
Hi all! I have many many many python unit test, which are used for testing some remote web service. The most important issue here is logging of test execution process and result. I strongly...
0
by: Victor Lin | last post by:
Hi, I got some problem with import. I have some python file that should be executed. Something like unit-test or other small tool. The project is not so small. So I want to separate these files...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.