{"id":6084,"date":"2020-05-14T21:57:11","date_gmt":"2020-05-15T04:57:11","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6084"},"modified":"2023-09-20T21:53:45","modified_gmt":"2023-09-21T04:53:45","slug":"creating-unit-tests-for-keil-based-embedded-projects","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/keil\/tests\/","title":{"rendered":"Creating Unit Tests for Keil-Based Embedded Projects"},"content":{"rendered":"<p>This tutorial shows how to create unit tests running directly on the target for embedded projects built with the Keil ARMClang compiler. Unlike GCC-based unit test projects that can be created out-of-the-box, creating unit tests with Keil requires additional setup steps that will be shown in this tutorial.<\/p>\n<p>We will create a basic unit test project that will run on the STM32F4Discovery board and will show how to locate, run and debug individual unit tests.<\/p>\n<ol>\n<li>Start Visual Studio and locate the VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/01-newprj-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6085\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/01-newprj-3.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>Specify the name and location for your project, then click &#8220;Create&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/02-name-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6086\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/02-name-1.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>We will begin with creating a regular Keil project, and will then extend it with unit tests. Select &#8220;<strong>Create a new project -&gt; Embedded Binary -&gt; MSBuild<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/03-binary-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6098\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/03-binary-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>If you would like to extend an existing project with unit tests instead, you can skip to step 10.<\/li>\n<li>Select the <strong>ARMClang<\/strong> compiler and pick your target device in the list:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/04-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6088\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/04-device.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>If the toolchain or device list is not shown correctly, see <a href=\"https:\/\/visualgdb.com\/documentation\/toolchains\/keil\/\">this page<\/a> for troubleshooting instructions.<\/li>\n<li>Sample code for Keil projects is included with the Keil components, so the project sample page will be empty. Press &#8220;Next&#8221; to go to the debug page:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/05-nosample.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6089\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/05-nosample.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Select a debug method that is compatible with your setup and press &#8220;Finish&#8221; to generate a project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/06-stlink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6090\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/06-stlink.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>VisualGDB will generate an empty project. We will now use the Keil Components page of VisualGDB Project Properties to add sample code to it. First, locate and check the GPIO component. Confirm the additional dependencies required by it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/07-gpio-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6100\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/07-gpio-1.png\" alt=\"\" width=\"1177\" height=\"753\" \/><\/a><\/li>\n<li>Then check the &#8220;Startup&#8221; component:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/08-startup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6101\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/08-startup.png\" alt=\"\" width=\"1177\" height=\"753\" \/><\/a><\/li>\n<li>Press &#8220;OK&#8221;. When VisualGDB suggests copying sample files, check the <strong>main module for STM32Cube HAL<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/09-main-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6102\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/09-main-1.png\" alt=\"\" width=\"586\" height=\"243\" \/><\/a><\/li>\n<li>Now we have a basic Keil-based project that includes all the necessary components to run the <strong>main()<\/strong> function. Ensure that you can build it successfully:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/10-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6103\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/10-built.png\" alt=\"\" width=\"1183\" height=\"759\" \/><\/a><\/li>\n<li>Now we can proceed with adding unit tests to the project. Open VisualGDB Project Properties and reference the <strong>Fast Semihosting and Embedded Profiler<\/strong> framework. To minimize the chance of compatibility issues, make sure you disable the sampling profiler and RTOS support:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/11-fw.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6104\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/11-fw.png\" alt=\"\" width=\"919\" height=\"601\" \/><\/a><\/li>\n<li>Go to the <strong>Unit Tests<\/strong> page and reference the <strong>TinyEmbeddedTest<\/strong> framework:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/12-tests.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6105\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/12-tests.png\" alt=\"\" width=\"919\" height=\"601\" \/><\/a><\/li>\n<li>Rename the <strong>main.c<\/strong> file to <strong>main.cpp<\/strong> and replace its contents with a sample <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/blob\/master\/visualgdb\/keil\/KeilUnitTestDemo\/main.cpp\">unit test demo file<\/a>. Once you build the project, the Test Explorer will show 3 sample unit tests:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/12-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6095\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/12-test.png\" alt=\"\" width=\"1183\" height=\"759\" \/><\/a><\/li>\n<li>Use the <strong>Test-&gt;Run All<\/strong> command to quickly run all tests. VisualGDB will automatically load the program into the FLASH memory, will run the tests and collect and display the call stacks from the failed ones:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/13-stack.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6096\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/13-stack.png\" alt=\"\" width=\"1183\" height=\"759\" \/><\/a><\/li>\n<li>You can run any subset of the tests under a debugger either by right-clicking on them in Test Explorer, or using the <strong>Test-&gt;Debug All<\/strong> command. VisualGDB will automatically stop the program once any of the unit tests fails: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/14-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6097\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/05\/14-debug.png\" alt=\"\" width=\"1183\" height=\"759\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create unit tests running directly on the target for embedded projects built with the Keil<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[147,129],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6084"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=6084"}],"version-history":[{"count":3,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6084\/revisions"}],"predecessor-version":[{"id":8419,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6084\/revisions\/8419"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}