473,549 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(u nittest.TestCas e):

def testNormalCase( self):
self.assertEqua l(....
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 2305
hw***@hotmail.c om (Will Stuyvesant) wrote in message news:<cb******* *************** ****@posting.go ogle.com>...
I have a unittest testfile like this:

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

class Test_rmlutils(u nittest.TestCas e):

def testNormalCase( self):
self.assertEqua l(....
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******@telusp lanet.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.goo gle.com...
I have a unittest testfile like this:

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

class Test_rmlutils(u nittest.TestCas e):

def testNormalCase( self):
self.assertEqua l(....
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.goo gle.com...
I have a unittest testfile like this:

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

class Test_rmlutils(u nittest.TestCas e):

def testNormalCase( self):
self.assertEqua l(....
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.TextTe stRunner (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
1281
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
2028
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 TestCase.skipIf(expr, msg): skips if expr is true These can be called either in setUp() or in the test methods. I also added reporting of skipped tests to...
1
1498
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(), and then add them to a TestCase class I have created. The tests seem to run, but they always seem to succeed - I have no idea why. Any ideas? ...
41
10241
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 new project now and I'm thinking of trying py.test (http://codespeak.net/py/current/doc/test.html). It looks pretty cool from the docs. Is there...
7
2061
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 old lines at the end: if __name__ == "__main__": unittest.main() This is great, because each of the modules are runnable, and I can select...
3
3422
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. I've had some experience with unit test support in other languages.
2
2441
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 assert the output. It's reasonable to use "unittest" here, but the problem is that "unittest" doesn't support (== I haven't found how) dynamic...
0
2301
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 self.sampler = DisasterSampler()
1
3932
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() ---------------------------------------------------------------------- Ran 0 tests in 0.000s
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7809
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.