trouble with unittest and namespaces - can anyone help?

L

Lol McBride

Hi all,
I'm developing an OOP program and trying to be smart by using unittest to
help me to catch as many bugs as possible during development.Unfortunately
I've hit a snag which I believe is to do with namespaces (specifically the
os.path).
The program is split into packages and modules as below:

oop_lb - toplevelpackage
__init__.py
oop_lb.py
analysis
__init__.py
prog1.py
prog2.py
tests
testprog1.py
testprog2.py
process
__init__.py
prog3.py
prog4.py
tests
testprog3.py
testprog4.py

The programs in analysis do not import classes or modules from anywhere
else and the tests in the tests directory run fine.My problem is with the
tests done for prog3 and prog4, both of which import from the analysis
package e.g
from analysis import prog1

All tests fail to run and a 'no such module as analysis message' is
displayed.
Could anyone give me an idea as to how to resolve this problem?
 
P

Peter Hansen

Lol said:
I've hit a snag which I believe is to do with namespaces (specifically the
os.path).

Probably more a problem with sys.path, but perhaps that's what you meant.
The programs in analysis do not import classes or modules from anywhere
else and the tests in the tests directory run fine.My problem is with the
tests done for prog3 and prog4, both of which import from the analysis
package e.g
from analysis import prog1

All tests fail to run and a 'no such module as analysis message' is
displayed.
Could anyone give me an idea as to how to resolve this problem?

The simplest approach is to insert the following at the top of your
tests:

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

Or something like that... basically if the directory containing
the files to be imported (or the one containing the package folder,
in the case of a Python package) is not in sys.path, you won't be
able to import the module or package properly.

Given that your tests are in a subfolder of the one where prog2.py
is found, I'm not sure how you are invoking the tests right now
such that they are working at all. How do they find "prog2"
when they import it right now?

-Peter
 
L

Lol McBride

On Tue, 11 Nov 2003 18:55:16 -0500, Peter Hansen wrote:
Given that your tests are in a subfolder of the one where prog2.py
is found, I'm not sure how you are invoking the tests right now
such that they are working at all. How do they find "prog2"
when they import it right now?

-Peter
Yes Peter you're right I did mean sys.path.
I use some code from a tutorial in Linux Format magazine to get the test
in the subfolder to work as below:
import unittest
# Import from this modules parent directory
import os
import sys
sys.path.insert(0,os.pardir)
import BallLoop
del sys.path[0]
del sys
del os

Could you expand a little on your answer - perhaps using the structure I
used as an example?I tend to need a bit of hand-holding till I understand
the gist of what I'm shown.
Thanks
Lol ;-)
 
P

Peter Hansen

Lol said:
On Tue, 11 Nov 2003 18:55:16 -0500, Peter Hansen wrote:
Given that your tests are in a subfolder of the one where prog2.py
is found, I'm not sure how you are invoking the tests right now
such that they are working at all. How do they find "prog2"
when they import it right now?

-Peter
Yes Peter you're right I did mean sys.path.
I use some code from a tutorial in Linux Format magazine to get the test
in the subfolder to work as below:
import unittest
# Import from this modules parent directory
import os
import sys
sys.path.insert(0,os.pardir)
import BallLoop
del sys.path[0]
del sys
del os

Could you expand a little on your answer - perhaps using the structure I
used as an example?I tend to need a bit of hand-holding till I understand
the gist of what I'm shown.

Okay, I understand better now. (Note that in the above example, you
could probably dispense with all the "del" stuff as it adds very little
of use.)

Here's our approach to achieving the same ultimate goal of allowing
files from other directories to be imported during unit testing. If
you look at the standard site.py module, you'll see stuff in there
about ".pth" files. Building on that support, we have the following
in our test framework, although for only a few files you could just
insert it into each test file:

import site
site.addsitedir('.')

This has the effect of adding the current directory to the sys.path,
as well as searching for all .pth files in the current directory and
adding their contents to the sys.path as well, according to the notes
in site.py. At this point, you just create a little, say, "test.pth"
file and put in it the paths to the other directories which you need
to make available for importing. I believe relative paths will work
fine, or you could use absolute paths although that's nearly always
a worse approach.

Let me know if you need more background to understand what's happening,
though I recommend perusing site.py first.

-Peter
 
L

Lol McBride

thanks for the tip Peter it worked a treat.
I'm also busy looking through the docs as you mentioned.
Thanks again.
Lol
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top