Level based Representation of a Binary Tree - Runing Tests

Runing tests

After we write the tests, It's time run them.

Run the following command in the project root.

Runing tests of tests.load module
 python -m unittest tests.load

You should get the following output:

Runing tests of tests.load module
 $ python -m unittest tests.load
 EEEE.
 ======================================================================
 ERROR: test_load_0 (tests.load.TestLoad)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "C:\projects\playground\tests\load.py", line 32, in test_load_0
     self.assertLeaf(tree,0)
   File "C:\projects\playground\tests\load.py", line 22, in assertLeaf
     self.assertEqual(tree.data, data)
 AttributeError: 'NoneType' object has no attribute 'data'
 ======================================================================
 ERROR: test_load_0_1 (tests.load.TestLoad)
 ...
 ======================================================================
 ERROR: test_load_0_1_2_x_4_5 (tests.load.TestLoad)
 ...
 ======================================================================
 ERROR: test_load_0_x_2 (tests.load.TestLoad)
 ...
 ----------------------------------------------------------------------
 Ran 5 tests in 0.001s

 FAILED (errors=4)

So, what this output tells us?

unittest - bottom summary message
 Ran 5 tests in 0.001s

At the bottom of the output, we can see how many tests were running and the time it takes. In our case, we ran all the 5 tests in the tests.load module in 0.001 seconds.

unittest - last summary message
 FAILED (errors=4)

We also see in the bottom of the output, whether we pass the test. There are two options to this summary message:

  • The OK is displayed if all the tests were successful
  • The FAILED (failures=X, errors=Y) if there at least one test that failed or ended with an error.

What is the difference between failure and an error:

  • An error is when a test raises an exception (other than AssertionError)
  • A failure is when an assertion does not hold and raises AssertionError, this can be from an assert statement or by calling one of the assertXXX function of unittest.TestCase

In our case, 4 tests ended with error - therefore we the second summary message is displayed.

unittest - detailed error message for test
 ======================================================================
 ERROR: test_load_0 (tests.load.TestLoad)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "C:\projects\playground\tests\load.py", line 32, in test_load_0
     self.assertLeaf(tree,0)
   File "C:\projects\playground\tests\load.py", line 22, in assertLeaf
     self.assertEqual(tree.data, data)
 AttributeError: 'NoneType' object has no attribute 'data'

For each error or failure, you can see :

  • The status of the test. It can be ERROR or FAIL
  • The name of the test. In our case, test_load_0
  • The name of the test case. In our case, tests.load.TestLoad
  • The traceback of the exception, so you can easily see the line where the exception was raised
unittest - concise top summary results
 EEEE.

At the top of the output. We can see concise summary results of all the ran tests. For each ran test, one character is displayed that shows the status of the test:

  • . means the test passed
  • F means the test failed. In other words, assertion in test did not hold
  • E means the test ended with an error.

We can see that there is a test that passed without writing any code. Can you conclude what the test that passed is from the output?

The test_empty is passed since the load stub return the expected None.

However, It is not enough - To write a correct code, all test needs to pass.

Rememeber that even a broken watch shows the right time twice a day :-)

In the next part, we write the code for the load function