In this lab you will learn how to carry out the process of Test-Driven development (TDD) using the Arduino framework. We will be using the PlatformIO command-line tools. There is detailed documentation covering the implementation of the Unity tools.
You should now use the IDE to open the exercises/02_tdd/temp/
directory which contains a simple Arduino sketch with a library. Lets look at the files. As you can see there are some additional ones:
├── lib
│ ├── avg
│ │ ├── avg.cpp
│ │ └── avg.h
│ └── readme.txt
├── platformio.ini
├── readme.md
├── src
│ └── main.cpp
└── test
└── test_desktop
└── test_avg.cpp
- The
lib/
directory contains our custom library. This is where we will build the business logic for our software. It will be thoroughly unit tested. - The
test/
directory contains the unit testing files that will be used to test the library code.
We will be executing our test suite from the terminal by invoking the pio
command together with the test
subcommand. Make sure you create an account and log in.
You should also ensure the gcc
toolchain is installed on your computer. Run the command gcc
. If this is not found you will need to install, the instructions differ depending on your operating system:
- MacOS:
brew install gcc
- Ubuntu:
apt install gcc
- Windows 10: install Cygwin and make sure you pick the gcc tools from the selection of software it can install.
$ pio test -v -e native
================================== [SUMMARY] ==================================
Environment nodemcuv2 [SKIP]
Environment native [SUCCESS]
========================= [SUCCESS] Took 0.32 seconds =========================
===================== [test/test_desktop] Testing... (2/2) =====================
/test_avg.cpp:11:test_add_single_val:FAIL: Expected 0 Was 42 [FAILED]
-----------------------
1 Tests 1 Failures 0 Ignored
FAIL
=============================== [TEST SUMMARY] ================================
test/test_desktop/env:nodemcuv2 [IGNORED]
test/test_desktop/env:native [FAILED]
========================== [FAILED] Took 0.33 seconds ==========================
Notice that the test suite fails!
You are now going to modify the code to ensure that the test passes. The test adds a single value to the library. It then reads the average value which should match the value we passed (but it doesn't). We are going to apply TDD principles.
For this first iteration, this has already been done, look in the test_avg.cpp
file.
We now need to write just enough code to pass the test. Edit the lib/avg/avg.cpp
file. Test your code by running the test suite. Stop as soon as the test passes.
The third and final step is to tidy up the code, making sure the tests still pass.
Now apply the TDD principles to fix the following issues, this will take 6 TDD iterations:
- If I send 2 values to the library, the average should be calculated based on the 2 values. Try adding 42 and 24, it should return 33.
- If the result is wrong perhaps you needed to call the
avg_clear()
function at the start of each test.
- If the result is wrong perhaps you needed to call the
- If the average is a floating point number it should round up if the average is 0.5 or above:
- If I pass values of 42 and 23 the average should be 33.
- If I pass values of 42, 11, 17, the average should be 23.
- The library should return the average of the last 5 values passed so if I add 6 values it should ignore the first one we added.
- If I pass the values 1, 2, 3, 4, 5 the average should be 3.
- If I pass the values 1, 2, 3, 4, 5, 6 the average should be 4.
- If I pass the values 1, 2, 3, 4, 5, 6, 7 the average should be 5.
For an extension task, use TDD to implement an average function that returns the mean reading since the device was powered up.
The final step is to use your custom library to create a complete temperature and humidity sensor.
- Connect a DHT11 sensor and capture the temperature and display in the serial monitor. 2. If you are using the Lolin32 board you should also display the temperature on the screen.
- Use your custom library to smooth the reading using a rolling average, display this next to each raw reading (including the screen if available).
- Capture the humidity as well as temperature and display this next to the temp values.
- How could you modify your library to smooth both the temperature and humidity?
- You may need to make changes to your library.
- Don't forget to apply the TDD methdology!