How do you do unittest?

John Roth newsgroups at jhrothjr.com
Fri Aug 1 18:24:11 EDT 2003


"Will Stuyvesant" <hwlgw at hotmail.com> wrote in message
news:cb035744.0308010633.3fd4e805 at 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.

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






More information about the Python-list mailing list