{"id":5559,"date":"2020-02-05T15:41:15","date_gmt":"2020-02-05T23:41:15","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=5559"},"modified":"2020-07-15T08:16:25","modified_gmt":"2020-07-15T15:16:25","slug":"creating-unit-tests-for-iar-based-projects","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/iar\/tests\/","title":{"rendered":"Creating Unit Tests for IAR-based Projects"},"content":{"rendered":"<p>This tutorial shows how to create, manage and debug unit tests for projects built with the IAR compiler for ARM with Visual Studio and VisualGDB. We will create a basic project containing multiple tests and will show how to run them individually and observe the results via the Visual Studio test GUI.<\/p>\n<p>Before you begin, install VisualGDB 5.5 Preview 3 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/01-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5560\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/01-newprj.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>Specify the name and location for the project, then press &#8220;Create&#8221; to launch the VisualGDB&#8217;s portion of the wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/02-location.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5561\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/02-location.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>Unlike GCC-based test projects that can be created directly based on templates shipped with VisualGDB, IAR-based projects are created empty. Hence, instead of creating a unit test project, we will create a regular project by selecting &#8220;Create a New Project -&gt; Embedded Binary -&gt; MSBuild&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/03-binary.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5562\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/03-binary.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>We will add the unit test support to this project after we complete the wizard.<\/li>\n<li>On the next page select the IAR toolchain and pick your device from the list:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/04-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5563\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/04-device.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Next&#8221; on the Sample Selection page to skip over it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/05-sample.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5564\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/05-sample.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the Debug Method page specify the settings that work with your setup and press &#8220;Finish&#8221; to complete the wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/06-stlink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5565\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/06-stlink.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>VisualGDB will create an empty project based on the IAR compiler. Add an empty C++ source file to the project and copy <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/blob\/master\/visualgdb\/ARM\/IAR\/IARTestDemo\/Source.cpp\">this sample code<\/a> into it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/07-newsrc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5566\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/07-newsrc.png\" alt=\"\" width=\"1395\" height=\"914\" \/><\/a><\/li>\n<li>The sample code used in the previous step is based on the TinyEmbeddedTest framework &#8211; a very lightweight unit test framework for embedded devices. In order to include it in the project, open VisualGDB Project Properties on the Unit Tests page and select <strong>TinyEmbeddedTest<\/strong> from the list:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/08-framework.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5567\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/08-framework.png\" alt=\"\" width=\"1284\" height=\"856\" \/><\/a>VisualGDB also supports the <strong>CppUTest<\/strong> and <strong>GoogleTest<\/strong> frameworks, however they have never been tested with IAR and may require porting before you can use them.<\/li>\n<li>Go to the Embedded Frameworks page of VisualGDB Project Properties and reference the <strong>Fast Semihosting and Embedded Profiler<\/strong> framework. Make sure you check the &#8220;Exclude Sampling Profiler Code&#8221; checkbox:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/09-semihost.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5568\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/09-semihost.png\" alt=\"\" width=\"1284\" height=\"856\" \/><\/a><\/li>\n<li>Now you can build the project by pressing Ctrl-Shift-B:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/10-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5569\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/10-built.png\" alt=\"\" width=\"1392\" height=\"912\" \/><\/a><\/li>\n<li>Once the project is built, open the Test Explorer window in Visual Studio. It will show all test methods defined in the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/11-found.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5570\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/11-found.png\" alt=\"\" width=\"1392\" height=\"912\" \/><\/a><\/li>\n<li>You can run either all tests using the <strong>Test-&gt;Run -&gt; All Tests<\/strong> command, or select individual tests in the Test Explorer, right-click on them and click &#8220;Run&#8221;. VisualGDB will automatically program the test firmware into the FLASH memory, will run the tests and display the results in the Test Explorer window:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/12-trace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5571\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/12-trace.png\" alt=\"\" width=\"1392\" height=\"912\" \/><\/a>Note that VisualGDB uses fast semihosting (reading of the target&#8217;s memory while the target is running) in order to report the test results to Visual Studio. This works very fast compared to using a COM port and does not require any additional hardware other than a JTAG\/SWD debugger.<\/li>\n<li>You can debug all tests, or individual tests by selecting &#8220;Debug&#8221; instead of &#8220;Run&#8221; in the test GUI. VisualGDB will automatically stop the debugger once any of the tests reports a failure:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/13-break.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5572\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/13-break.png\" alt=\"\" width=\"1392\" height=\"912\" \/><\/a><\/li>\n<li>Note that the code in the <strong>main()<\/strong> function calls <strong>IsRunningUnitTests()<\/strong> to distinguish between the regular debug sessions and unit test runs. This allows embedding unit tests directly into your binary. Unless you specifically choose to run one or more tests via the Visual Studio GUI, <strong>IsRunningUnitTests()<\/strong> will return <strong>false<\/strong> and your program can run its regular logic:\n<pre class=\"\">int main()\r\n{\r\n\tif (IsRunningUnitTests())\r\n\t{\r\n\t\tRunAllTests();\r\n\t}\r\n\treturn 0;\r\n}<\/pre>\n<\/li>\n<li>In order to run the tests from command line, locate\u00a0 the <strong>.vgdbtestcontainer<\/strong> file inside the <strong>VisualGDB<\/strong> subdirectory. It is an XML file containing sufficient information to run tests outside of Visual Studio. You can launch a test session using the following command line:\n<pre class=\"\">%VISUALGDB_DIR%\\VisualGDB \/runtests &lt;file&gt;.vgdbtestcontainer \/vsoutput:results.trx<\/pre>\n<p>The <strong>results.trx<\/strong> file produced by VisualGDB will be compatible with <a href=\"https:\/\/nunit.org\/\">NUnit<\/a>, so you can integrate them into your continuous integration workflow: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/14-trx.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5573\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/01\/14-trx.png\" alt=\"\" width=\"979\" height=\"512\" \/><\/a><\/li>\n<\/ol>\n<p>You can find the source code for the project shown in this tutorial in our <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/tree\/master\/visualgdb\/ARM\/IAR\/IARTestDemo\">GitHub repository<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create, manage and debug unit tests for projects built with the IAR compiler for ARM<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[128],"tags":[53,157,129],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5559"}],"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=5559"}],"version-history":[{"count":3,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5559\/revisions"}],"predecessor-version":[{"id":6447,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5559\/revisions\/6447"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=5559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=5559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=5559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}