473,396 Members | 2,106 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,396 software developers and data experts.

How do you do unittest?

I have a unittest testfile like this:

----------------------- test_mod.py ---------------------
import sys
sys.path.append('..')
import unittest
import mod

class Test_rmlutils(unittest.TestCase):

def testNormalCase(self):
self.assertEqual(....
if __name__ == '__main__':
unittest.main()
---------------------------------------------------------

It is exactly the same as example 17-2 in "Python in a
Nutshell" (PiaN), except for the first two lines. To
quote PiaN:
"...name the test module...with a prefix such as 'test_',
and put it in a subdirectory named 'test' of the directory
where you keep the sources."

PiaN did not mention the ugly sys.path.append('..') trick
I had to use when following its advice. I need to use it
because mod.py is in the directory above the 'test'
directory.

To run a test from the source directory I have to change
to the 'test' directory and run test_mod.py there from the
commandline. Okay, I can do that from a batchfile:

----------------------- test.bat ------------------------
cd test
test_mod.py
cd ..
---------------------------------------------------------

But: I would like to get rid of the need for
sys.append('..') and I don't see a nice way to do that, do
you? Maybe something from a Python script instead of a
DOS script...I am thinking about importing sys there and
then doing some advanced unittest function like
'runsuite(blah, foo)' but I am still reading the unittest
docs and there must be somebody who did this before?

I guess the author of PiaN does not use a 'test' subdirectory
himself, as it is now the example 17-2 does only work in the
source directory.
Jul 18 '05 #1
5 2299
hw***@hotmail.com (Will Stuyvesant) wrote in message news:<cb**************************@posting.google. com>...
I have a unittest testfile like this:

----------------------- test_mod.py ---------------------
import sys
sys.path.append('..')
import unittest
import mod

class Test_rmlutils(unittest.TestCase):

def testNormalCase(self):
self.assertEqual(....
if __name__ == '__main__':
unittest.main()
---------------------------------------------------------

It is exactly the same as example 17-2 in "Python in a
Nutshell" (PiaN), except for the first two lines. To
quote PiaN:
"...name the test module...with a prefix such as 'test_',
and put it in a subdirectory named 'test' of the directory
where you keep the sources."

PiaN did not mention the ugly sys.path.append('..') trick
I had to use when following its advice. I need to use it
because mod.py is in the directory above the 'test'
directory.

To run a test from the source directory I have to change
to the 'test' directory and run test_mod.py there from the
commandline. Okay, I can do that from a batchfile:

----------------------- test.bat ------------------------
cd test
test_mod.py
cd ..
---------------------------------------------------------

But: I would like to get rid of the need for
sys.append('..') and I don't see a nice way to do that, do
you? Maybe something from a Python script instead of a
DOS script...I am thinking about importing sys there and
then doing some advanced unittest function like
'runsuite(blah, foo)' but I am still reading the unittest
docs and there must be somebody who did this before?

I guess the author of PiaN does not use a 'test' subdirectory
himself, as it is now the example 17-2 does only work in the
source directory.


It is interesting that I had a similar issue with doctest, and I
was forced to add
import sys; sys.path.append('.')


to my tests. Quite ugly and at the end I wrote a script that did this
automatically. I would be curious to know what's the origin of this
path issue.

Michele
Jul 18 '05 #2
Quoth Will Stuyvesant:
[...]
PiaN did not mention the ugly sys.path.append('..') trick
I had to use when following its advice. I need to use it
because mod.py is in the directory above the 'test'
directory.

To run a test from the source directory I have to change
to the 'test' directory and run test_mod.py there from the
commandline. [...]


Why not just stay in the directory containing mod.py and run
python test/test_mod.py
?

--
Steven Taschuk st******@telusplanet.net
"Its force is immeasurable. Even Computer cannot determine it."
-- _Space: 1999_ episode "Black Sun"

Jul 18 '05 #3

"Will Stuyvesant" <hw***@hotmail.com> wrote in message
news:cb**************************@posting.google.c om...
I have a unittest testfile like this:

----------------------- test_mod.py ---------------------
import sys
sys.path.append('..')
import unittest
import mod

class Test_rmlutils(unittest.TestCase):

def testNormalCase(self):
self.assertEqual(....
if __name__ == '__main__':
unittest.main()
---------------------------------------------------------

It is exactly the same as example 17-2 in "Python in a
Nutshell" (PiaN), except for the first two lines. To
quote PiaN:
"...name the test module...with a prefix such as 'test_',
and put it in a subdirectory named 'test' of the directory
where you keep the sources."

PiaN did not mention the ugly sys.path.append('..') trick
I had to use when following its advice. I need to use it
because mod.py is in the directory above the 'test'
directory.

To run a test from the source directory I have to change
to the 'test' directory and run test_mod.py there from the
commandline. Okay, I can do that from a batchfile:

----------------------- test.bat ------------------------
cd test
test_mod.py
cd ..
---------------------------------------------------------

But: I would like to get rid of the need for
sys.append('..') and I don't see a nice way to do that, do
you? Maybe something from a Python script instead of a
DOS script...I am thinking about importing sys there and
then doing some advanced unittest function like
'runsuite(blah, foo)' but I am still reading the unittest
docs and there must be somebody who did this before?

I guess the author of PiaN does not use a 'test' subdirectory
himself, as it is now the example 17-2 does only work in the
source directory.


I'm not sure what the author of Python in a Nutshell was
thinking of. I have never had any problem with running
unit tests, but my setup has the unit test modules in the
same directory as the source modules, and I also have a
script file (well, a Windows batch or command file) for
each test module, as well as a test module that creates
a suite with all of the other test modules. It works
wonderfully well; never a problem.

What's happening is that Python always puts the
directory with the module you invoked into the path
as the first directory, so I would suspect that your
project directory isn't in the path. If you only execute
out of that one directory, everything just works. If
you try to import one of those modules from somewhere
else, it doesn't work.

If you want to keep your tests in a separate library
(and there are good arguements for that - I just ignore
them for myself) your actual program directory has
to be on the Python path. The ".." workaround you've
mentioned is one way of doing that. The way I work,
I just set the Python path to what I want in the
invocation scripts, or in the script that invokes the
command prompt I use, and then simply use it.
No muss, no fuss.

John Roth
Jul 18 '05 #4

"Will Stuyvesant" <hw***@hotmail.com> wrote in message
news:cb**************************@posting.google.c om...
I have a unittest testfile like this:

----------------------- test_mod.py ---------------------
import sys
sys.path.append('..')
import unittest
import mod

class Test_rmlutils(unittest.TestCase):

def testNormalCase(self):
self.assertEqual(....
if __name__ == '__main__':
unittest.main()
---------------------------------------------------------

It is exactly the same as example 17-2 in "Python in a
Nutshell" (PiaN), except for the first two lines. To
quote PiaN:
"...name the test module...with a prefix such as 'test_',
and put it in a subdirectory named 'test' of the directory
where you keep the sources."

PiaN did not mention the ugly sys.path.append('..') trick
I had to use when following its advice. I need to use it
because mod.py is in the directory above the 'test'
directory.
You can avoid that sort of mambo jambo by avoiding
unittest.main() and running the tests directly. I added
a simple example to the Py2.3 docs:

http://www.python.org/doc/current/li...l-example.html
Raymond Hettinger
To run a test from the source directory I have to change
to the 'test' directory and run test_mod.py there from the
commandline. Okay, I can do that from a batchfile:

----------------------- test.bat ------------------------
cd test
test_mod.py
cd ..
---------------------------------------------------------

But: I would like to get rid of the need for
sys.append('..') and I don't see a nice way to do that, do
you? Maybe something from a Python script instead of a
DOS script...I am thinking about importing sys there and
then doing some advanced unittest function like
'runsuite(blah, foo)' but I am still reading the unittest
docs and there must be somebody who did this before?

I guess the author of PiaN does not use a 'test' subdirectory
himself, as it is now the example 17-2 does only work in the
source directory.

Jul 18 '05 #5
> [Raymond Hettinger]
You can avoid that sort of mambo jambo by avoiding
unittest.main() and running the tests directly. I added
a simple example to the Py2.3 docs:

http://www.python.org/doc/current/li...l-example.html


Thanks, I now use unittest.TextTestRunner (what a name!) in
test_mod.py in a test_mod.test function. Together with a general
test.py in the 'test' directory that does the sys.path.append('..') I
can now run tests in the test directory so I don't have all the
clutter in the source directory, and I don't have to do
sys.path.append('..') in every test_xxx.py, only in the test.py that
looks like:

# To be executed in the 'test' directory
import sys
sys.path.append('..')
import test_mod
test_mod.test()
import test_mod2
test_mod2.test()
....
Jul 18 '05 #6

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

Similar topics

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 %<--------------------------------------------------------------------
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...
1
by: Thomas Heller | last post by:
I'm trying to integrate some doctest tests with unittest. The tests must be exposed as one or more subclasses of unittest.TestCase, so I'm collecting them with a call to doctest.DocTestSuite(),...
41
by: Roy Smith | last post by:
I've used the standard unittest (pyunit) module on a few projects in the past and have always thought it basicly worked fine but was just a little too complicated for what it did. I'm starting a...
7
by: Jorgen Grahn | last post by:
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good...
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: Chris Fonnesbeck | last post by:
I have a module for which I am trying to code a unit test. However, when I run unittest.main(), I get: In : import PyMC In : PyMC.unittest.main() ...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.