3 Replies Latest reply on Mar 25, 2020 12:50 AM by user_4289666

    Unit Tests in ModusToolbox

    user_4289666

      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

        • 1. Re: Unit Tests in ModusToolbox
          DheerajK_81

          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

          1 of 1 people found this helpful
          • 2. Re: Unit Tests in ModusToolbox
            LePo_1062026

            Manuel,

             

            This might be an alternate method of creating a unit test build WITHOUT changing the Makefile.

             

            1. Keep the CY_IGNORE command in the MakeFile.  This is your standard build specification.
            2. Select "Build Targets\Create..." on your project.
            3. In the "Modify Build Target"
              1. Give the build target a name in Target Name: "program Unit Tests"
              2. uncheck "Same as the target name"
              3. Type in Build Target: "program CY_IGNORE=".  This should substitute a blank for your CY_IGNORE=$(wildcard ./test/*.cpp) in the Makefile.
            4. You should now have a new Build Target in your project.
            5. Double-click on the "program Unit Tests" to build and program the target with unit testing enabled.
            6. If you want you can create another build target without the unit tests.  See:
            7. Now you should see another Build Target.
            8. 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

            1 of 1 people found this helpful
            • 3. Re: Unit Tests in ModusToolbox
              user_4289666

              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