More Than One Bug

The second bug is easier to find and understand. The error message shows the number is being converted but is wildy wrong. The Kelvin scale starts at absolute zero so cannot be negative!

$ python tests/test_PlotSource.py Test_TemperaturePlotSource.test_kelvin
test_kelvin (__main__.Test_TemperaturePlotSource)
Create the source in Kelvin mode and test with some values. ... FAIL

======================================================================
FAIL: test_kelvin (__main__.Test_TemperaturePlotSource)
Create the source in Kelvin mode and test with some values.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/test_PlotSource.py", line 101, in test_kelvin
    msg="Checking converted temperature is correct")
AssertionError: -253.14999999999998 != 293.15 within 7 places : Checking converted temperature is correct

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

In this case the programmer's mistake can be seen by looking at the:

  • calculation (value * self._scale + self._offset) and
  • the values used in it (self._scale = 1.0 and self._offset = -273.15).

The offset has the wrong sign, it should be a positive value. Once fixed, the tests can all be re-executed to check this fix.

$ python tests/test_PlotSource.py
test_celsius (__main__.Test_TemperaturePlotSource)
Create the source in Celsius mode and test with some values. ... ok
test_fahrenheit (__main__.Test_TemperaturePlotSource)
Create the source in Fahrenheit mode and test with some values. ... ok
test_kelvin (__main__.Test_TemperaturePlotSource)
Create the source in Kelvin mode and test with some values. ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.002s

OK

These tests were written after the code was created to test some code which had not been exercised by the sensor plotter program. Some other more complex tests were also written to help understand some bugs in the more complex Plotter class and prevent regressions. A slightly different software engineering approach is to write the tests before the code - this is known as test-driven development (TDD).

Once tests have been created they can also be integrated into the development and deployment processes. Continuous integration is one way to execute them, often as a quality gate.

The full set of tests can be viewed on GitHub.

This guide was first published on Apr 01, 2020. It was last updated on Jan 28, 2020.

This page (Bug 2) was last updated on Mar 26, 2020.

Text editor powered by tinymce.