Testing¶
Hazwaz provides the module hazwaz.unittest
with helpers based
on unittest
to write unit tests for command line behaviour.
The class hazwaz.unittest.HazwazTestCase
can be used instead
of unittest.TestCase
and works just as its parent: methods
whose name start with test
are run as individual tests, and you can
use all the usual unittest assert methods.
To write a test that runs the command as if from the command line, with
certain parameters, you can use the method
hazwaz.unittest.HazwazTestCase.run_with_argv()
as in the
following example:
import hazwaz.unittest
import greeter
class testGreeter(hazwaz.unittest.HazwazTestCase):
def test_greet_world(self):
cmd = greeter.Greet()
stream = self.run_with_argv(cmd, [
"./greeter.py",
"world",
])
self.assertEqual(
stream["stdout"].getvalue(),
"Hello world!\n"
)
The first parameter should be the name of the command itself, as if this was the full command line.
If the tests are in their own module, there is a convienence function
hazwaz.unittest.main()
that runs unittest.main()
,
to be used e.g.:
if __name__ == "__main__":
hazwaz.unittest.main()
However, if you’re writing a self-contained script you can use the
command hazwaz.unittest.TestCommand
to add a subcommand called
test
which runs all tests from a list of unittest.TestCase
:
class Greet(hazwaz.MainCommand):
"""
Greet people in different ways.
"""
commands = (
World(),
Individual(),
hazwaz.unittest.TestCommand([TestGreeter]),
)