Chapter 3
■
Unittest
44
You already know that -v stands for verbose mode. The
following is the output in
verbose mode:
test_case01 (test_module07.TestClass08) ...
In test_case1()
ok
test_case02 (test_module07.TestClass08) ... FAIL
test_case03 (test_module07.TestClass08) ...
In test_case3()
ok
======================================================================
FAIL: test_case02 (test_module07.TestClass08)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pi/book/code/chapter03/test/test_module07.py", line 11, in
test_case02
self.assertTrue("Python".isupper())
AssertionError:
False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.012s
FAILED (failures=1)
The
option -q stands for quiet mode. Run the following command to demonstrate
quiet mode:
python3 -m unittest -q test_module07
The output is as follows:
In test_case1()
In test_case3()
======================================================================
FAIL: test_case02 (test_module07.TestClass08)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pi/book/code/chapter03/test/test_module07.py", line 11, in
test_case02
self.assertTrue("Python".isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.005s
FAILED (failures=1)
Chapter 3
■
Unittest
45
The option -f stands for
failsafe. It forcefully stops execution
as soon as the first test
case fails. Run the following command to initiate failsafe mode:
python3 -m unittest -q test_module07
The following is the output in failsafe mode:
In test_case1()
.F
======================================================================
FAIL: test_case02 (test_module07.TestClass08)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pi/book/code/chapter03/test/test_module07.py", line 11, in
test_case02
self.assertTrue("Python".isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 2 tests in 0.004s
FAILED (failures=1)
You can also use more than one option. For example, you
can combine verbose with
failsafe using the following command:
python3 -m unittest -fv test_module07
The output is as follows:
test_case01 (test_module07.TestClass08) ...
In test_case1()
ok
test_case02 (test_module07.TestClass08) ... FAIL
======================================================================
FAIL: test_case02 (test_module07.TestClass08)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pi/book/code/chapter03/test/test_module07.py", line 11, in
test_case02
self.assertTrue("Python".isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 2 tests in 0.005s
FAILED (failures=1)
Chapter 3
■
Unittest
46
As
an exercise, try to use different combinations of command-line options.
Creating a Test Package
Up until now, you have created and executed test modules individually. However, you
can use Python’s built-in packaging feature to create a package of tests.
This is standard
practice in complex projects with large codebases.
Figure
3-1
shows a snapshot of the current test directory where you are saving your
test modules.
Now, let’s create a package of test modules. Create an __init__.py file
in the test
directory. Add the code in Listing
3-9
to the __init__.py file,
Do'stlaringiz bilan baham: