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

trouble with unittest and namespaces - can anyone help?

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?
Jul 18 '05 #1
4 1432
Lol McBride wrote:

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
Jul 18 '05 #2
On Tue, 11 Nov 2003 18:55:16 -0500, Peter Hansen wrote:
<< snip snip >>
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 ;-)
Jul 18 '05 #3
Lol McBride wrote:

On Tue, 11 Nov 2003 18:55:16 -0500, Peter Hansen wrote:
<< snip snip >>
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
Jul 18 '05 #4
thanks for the tip Peter it worked a treat.
I'm also busy looking through the docs as you mentioned.
Thanks again.
Lol
Jul 18 '05 #5

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

Similar topics

3
by: Gonçalo Rodrigues | last post by:
Hi, I use the unittest module for testing a given module's functionality, by lumping several test classes and then at the end a simple if __name__ == "__main__": unittest.main() In one of...
12
by: Paul Moore | last post by:
One of the things I really dislike about Unittest (compared, say, to a number of adhoc testing tricks I've used in the past, and to Perl's "standard" testing framework) is that the...
0
by: Danny Shevitz | last post by:
Why doesn't the following code snippet work? The error is ImportError: No module named myTestCase2 TIA, Danny %<--------------------------------------------------------------------
3
by: Jan Decaluwe | last post by:
I'm working on a unit test for a finite state machine (FSM). The FSM behavior is specified in a dictionary called transitionTable. It has a key per state with a tuple of possible transitions as...
0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
2
by: MrBlueSky | last post by:
Morning! I'm writing my first Python program, so please have patience! I'd like to redirect the output from my application's unit tests ("import unittest") to a Tkinter Text object. I found the...
3
by: David Vincent | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hello I'm hoping to get some insight into a situation that seems odd to me. My Python experience is limited; I've just started using the unittest module....
2
by: Oleg Paraschenko | last post by:
Hello, I decided to re-use functionality of "unittest" module for my purposes. More precisely, I have a list of folders. For each folder, code should enter to the folder, execute a command and...
0
by: Chris Fonnesbeck | last post by:
I have built the following unit test, observing the examples laid out in the python docs: class testMCMC(unittest.TestCase): def setUp(self): # Create an instance of the sampler...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.