Unit + Integration Tests
Projects Involved: OBC
Concepts: Unit and Integration Tests
Abstract:
This task introduces the basics of writing unit and integration tests in C++. Unit tests verify individual components in isolation, while integration tests check that combined components work together correctly. Specifically, you will be writing a unit test to validate that each public method in the mock camera works properly in isolation. You will also be writing a manual integration test to test the mock camera end-to-end with real files and timing. MAKE SURE you have completed your mock camera implementation first before proceeding to this step!
Goals:
- Learn the purpose and difference between unit and integration tests
Files Involved:
OBC
Reference files:
include/camera/mock.hpp
,src/camera/mock.cpp
include/camera/interface.hpp
,src/camera/interface.cpp
tests/unit/mission_state_test.cpp
(unit test reference)tests/integration/gcs_status_integration.cpp
(integration test reference)
Files to create and work on:
tests/unit/mock_camera_test.cpp
(unit test)tests/integration/take_pictures.cpp
(integration test)
Base Steps:
Basic Understanding
Let's first make sure you know what unit and integration tests are, as well as how to run them!
- If you are unfamiliar with testing in general, here is an article explaining what unit and integration tests are. In the context of TUAS, unit test generally refers to testing some component (usually a class) and their methods to make sure they behave as intended. Integration tests here are mostly tests that require manual verification to make sure some component works in the bigger system (across components)..
- To run a unit test in the onboarding OBC, you can run
make test
(run aftermake build
). This complies and runs all the tests in thetests/unit
folder. This folder is also where you will be placing your unit tests. - To run an integration test, you have to build it first (run
make build
), then change directories intobuild/
(cd build
), then run the test target, like./testname
, where "testname" is the file of your test, without the .cpp extension.
Unit Test
1. Ensure images_dir
points to a real folder with at least one image.
2. Create tests/unit/mock_camera_test.cpp
that:
- Constructs
MockCamera
. - Calls
takePicture()
. - Verifies the optional has a value and the
cv::Mat
is not empty; prints PASS/FAIL and return non-zero on failure.
3. Write unit tests for every remaining public methods in MockCamera
.
4. Build and make sure all tests pass.
Integration Test (requires manual verification)
1. Create tests/integration/take_pictures.cpp
that:
- Constructs
MockCamera
. - Calls
startTakingPictures()
. - Sleeps for a couple of seconds.
- Calls
stopTakingPictures()
. - Checks that the number of images has increased and prints the number of images before and after.
2. Build and make sure the test works as intended. Make sure to verify the results manually.
If you did not implement
startTakingPictures()
, that's okay. You should still write an integration test testing some function in the Mock Camera. (Ex. Take multiple pictures, make sure that they are different)