- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I am using ModusToolbox for my PSoC6 project and I want to add unit tests. Along with the folders ./src and ./inc I added a folder ./test for the unit tests. To prevent the target build flow from discovering the test sources, I excluded them from the target build by adding a line
CY_IGNORE=$(wildcard ./test/*.cpp)
into the Makefile. This works, the regular target build ignores the test files. Now, I want to establish the build flow for the unit tests. Is there any infrastructure to add this to the existing Makefiles? Or do I need a separate ModusToolbox project for the unit tests? Or should I establish a separate build flow for the unit tests outside ModusToolbox and the project Makefile?
What is the recommended way?
Regards
Manuel
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Regards,
Dheeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Regards,
Dheeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Manuel,
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"
- Give the build target a name in Target Name: "program Unit Tests"
- uncheck "Same as the target name"
- Type in Build Target: "program CY_IGNORE=". This should substitute a blank for your CY_IGNORE=$(wildcard ./test/*.cpp) in the Makefile.
- 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:
Len
"Engineering is an Art. The Art of Compromise."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Regards
Manuel