unittest

M

Mag Gam

I am writing an application which has many command line arguments.
For example: foo.py -args "bar bee"

I would like to create a test suit using unittest so when I add
features to "foo.py" I don't want to break other things. I just heard
about unittest and would love to use it for this type of thing.

so my question is, when I do these tests do I have to code them into
foo.py? I prefer having a footest.py which will run the regression
tests. Any thoughts about this?

TIA
 
R

Richard Thomas

I am writing an application which has many command line arguments.
For example: foo.py -args "bar bee"

I would like to create a test suit using unittest so when I add
features to "foo.py" I don't want to break other things. I just heard
about unittest and would love to use it for this type of thing.

so my question is, when I do these tests do I have to code them into
foo.py? I prefer having a footest.py which will run the regression
tests. Any thoughts about this?

TIA

You should certainly keep your test suite separate. There's a quick
example of how to write unit tests in the unittest documentation:
http://docs.python.org/library/unittest.html#basic-example

Richard.
 
M

Mag Gam

So, in this example:

"import random"

In my case I would do "import foo" ? is there anything I need to do for that?
 
S

Steven D'Aprano

So, in this example:

"import random"

In my case I would do "import foo" ? is there anything I need to do for
that?


Suppose you have a file mymodule.py containing your code, and you want
some unit tests.

If you only have a few, you can probably put them inside mymodule.py, but
let's say you have lots and want to keep them in a separate file. So
create a new module mymoduletests.py, and start it like this:

# mymoduletests.py

import unittest
import mymodule

class MyTests(unittest.TestCase): # Inherit from the TestCase class.
# Put your tests inside this class
def test_module_has_docstring(self):
"""Fail if the module has no docstring, or if it is empty."""
docstring = mymodule.__doc__
self.assert_(docstring is not None)
self.assert_(docstring.strip() != '')

if __name__ == '__main__':
# only execute this part when you run the module
# not when you import it
unittest.main()




Now to actually run the tests, from command-line, type:

python mymoduletests.py

and hit enter. (You do this from the operating system shell, not the
Python interactive interpreter.) You should see something like this:

..
 

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,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top