1 of 1 people found this helpful
To ignore the test sources during compilation, you are right, you can make use of the CY_IGNORE make variable. When it comes to unit testing, there are a couple of popular unit testing frameworks available which you can use like Unity, OClint, Catch2, etc. Let me know which one you are using for writing the test cases.
When it comes to using the Makefile for conducting the unit test, you can make use of PHONY targets. A PHONY target is essentially a command to be executed when you make an explicit request. Let's say you have two test files and the way you would do it, edit the "Makefile" to add this piece of code (it can be anywhere in between) as follows:
# Example unit test TEST_DIR=-I./test \ -I./inc \ -I./src CPP_FLAGS=-std=c++11 -Wall TEST1_SRC=./test/test1.c TEST1_SRC=./test/test2.c CY_BUILD_test1: $(info ==============================================================================) $(info add your testing command here: For example: arm-none-eabi-g++ $(CPP_FLAGS) $(TEST_DIR) -o test_output1 $(TEST1_SRC) ) $(info = Testing1 complete =) $(info ==============================================================================) $(info ) CY_BUILD_test2: $(info ==============================================================================) $(info add your testing command here: For example: arm-none-eabi-g++ $(CPP_FLAGS) $(TEST_DIR) -o test_output2 $(TEST2_SRC) ) $(info = Testing2 complete =) $(info ==============================================================================) $(info ) test: CY_BUILD_test1 CY_BUILD_test2 .PHONY: test
This is just a simple example. Modify the command to be executed based on the test framework you are using.
Note that we have created a PHONY target called "test" which calls both the test cases in the line "test: CY_BUILD_test1 CY_BUILD_test2".
To invoke this command, run "Cygwin.bat" from this directory: "<modustoolbox_install_dir>\tools_2.0\modus-shell\".
Navigate to your application directory and then run "make test" as shown below:
This way the unit tests are only conducted when you invoke it using the command line and don't interfere in any way when you compile your project inside of ModusToolbox.
Let me know if you have further queries.
1 of 1 people found this helpful
This might be an alternate method of creating a unit test build WITHOUT changing the Makefile.
- Keep the CY_IGNORE command in the MakeFile. This is your standard build specification.
- Select "Build Targets\Create..." on your project.
- In the "Modify Build Target"
- You should now have a new Build Target in your project.
- Double-click on the "program Unit Tests" to build and program the target with unit testing enabled.
- If you want you can create another build target without the unit tests. See:
- Now you should see another Build Target.
- If you double-click on "program", then the Target will be programmed WITHOUT the unit test code because the Makefile has the CY_IGNORE=$(wildcard ./test/*.cpp) that you placed in it.
This method can also be used to substitute other Makefile commands such is the TARGET= and most if not all others. I use this regularly to substitute other targets for example:
Hi DheerajK and Len,
thank you for your recommendations. They will help me implementing the unit tests. I will try them out in the next days when I come to testing.
One problem I have to avoid is to have multiple main entry points when compiling for the tests as the build chain auto-detects all .c files in all subdirectories that are not CY_IGNOREd. I am not clear whether your suggestions will have the same problem. Eventually, I have to make more usage of CY_IGNORE to control which main() is included in the test build.