With the test written, we begin with the implementation. For illustration purposes, we will intentionally write some incorrect implementations to see how the test behaves.
The easiest way to run tests defined with the unittest
framework, is to put a call to its main method at the end of
the test module:
unittest.main()
Let's run the test using the dummy Gray encoder shown earlier:
% python test_gray.py -v Check that only one bit changes in successive codewords ... FAIL Check that all codewords occur exactly once ... FAIL <trace backs not shown>
As expected, this fails completely. Let us try an incorrect implementation, that puts the lsb of in the input on the output:
def bin2gray(B, G, width):
### INCORRECT - DEMO PURPOSE ONLY! ###
@always_comb
def logic():
G.next = B[0]
return logic
Running the test produces:
% python test_gray.py -v
Check that only one bit changes in successive codewords ... ok
Check that all codewords occur exactly once ... FAIL
======================================================================
FAIL: Check that all codewords occur exactly once
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_gray.py", line 109, in testUniqueCodeWords
sim.run(quiet=1)
...
File "test_gray.py", line 104, in test
self.assertEqual(actual, expected)
File "/usr/local/lib/python2.2/unittest.py", line 286, in failUnlessEqual
raise self.failureException, \
AssertionError: [0, 0, 1, 1] != [0, 1, 2, 3]
----------------------------------------------------------------------
Ran 2 tests in 0.785s
Now the test passes the first requirement, as expected, but fails the second one. After the test feedback, a full traceback is shown that can help to debug the test output.
Finally, if we use the correct implementation as in section 2.4.1, the output is:
% python test_gray.py -v Check that only one bit changes in successive codewords ... ok Check that all codewords occur exactly once ... ok ---------------------------------------------------------------------- Ran 2 tests in 6.364s OK
About this document