11.2.5.1.8 Optional Steps
Separate Test and Production Code
To better organize your project, you should move unit support files and testrunners into a separate subfolder:
You can also add a separate test configuration to your project:
Because you have a test configuration that defines UNIT_TESTS (see picture above) and you moved test running to a separate subfolder, you can modify your main.c to something like this:
#include "mcc_generated_files/mcc.h"
#include <util/delay.h>
#ifdef UNIT_TESTS
#include "unit_tests/testrunner.h"
#else
int do_normal_stuff();
#endif
int main(void)
{
// Initialize drivers from MCC
SYSTEM_Initialize();
_delay_ms(1000);
#ifdef UNIT_TESTS
printf("Starting unit tests\n");
run_unit_tests();
#else
printf("Starting normal execution\n");
return do_normal_stuff();
#endif
}
int do_normal_stuff() {
return 0;
}
This way you can have one common project but one configuration that optimizes the build for production, and one test configuration that runs the test.
Add Code Coverage
If you want to enable code coverage, you can add the following lines to your
mdb.script file before the program
command:
# Generate simulator coverage data and write it to a file
set codecoverage.enabled Enabled_Reset_on_run
set codecoverage.enableoutputtofile true
set codecoverage.outputtofile simulator_coverage.txt
This will write coverage data to simulator_coverage.txt while running the mdb script.