{"id":8583,"date":"2023-10-09T15:31:18","date_gmt":"2023-10-09T22:31:18","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=8583"},"modified":"2023-10-09T15:31:18","modified_gmt":"2023-10-09T22:31:18","slug":"using-the-cpputest-framework-with-stm32-devices","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/tests\/cpputest\/embedded\/","title":{"rendered":"Using the CppUTest framework with STM32 devices"},"content":{"rendered":"<p>This tutorial shows how to use the CppUTest framework with the STM32 devices. We will create a basic test project, show how it runs on the target device, and will use the <a href=\"https:\/\/visualgdb.com\/tutorials\/tracing\/embedded\/\">Software Tracing<\/a> functionality to record the function calls inside the test program.<\/p>\n<ol>\n<li>Start Visual Studio and launch the VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/01-newprj-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8584\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/01-newprj-1.png\" alt=\"\" width=\"1014\" height=\"675\" \/><\/a><\/li>\n<li>Enter the name and location for the project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/02-path-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8585\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/02-path-1.png\" alt=\"\" width=\"890\" height=\"625\" \/><\/a><\/li>\n<li>Select &#8220;<strong>Create a New Project -&gt; Unit Test -&gt; Advanced CMake -&gt; CppUTest<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/03-test-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8586\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/03-test-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Select the ARM toolchain and choose the device you would like to target: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/04-device-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8587\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/04-device-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Proceed with the default project sample: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/05-demo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8588\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/05-demo.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Finally, choose the debugging settings that work with your setup and click &#8220;Finish&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/06-debug-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8589\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/06-debug-3.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>You can keep the FLASH patching and tracing enabled even if you are not planning to use them. Unless you actually create tracepoints, these settings will not add any extra overhead.<\/li>\n<li>Build the project. Note that the CppUTest framework has a non-trivial memory footprint (almost 75KB in this example). If this is too much for your device, consider using our <a href=\"https:\/\/visualgdb.com\/documentation\/tests\/TinyEmbeddedTest\/\">TinyEmbeddedTest<\/a> framework instead.<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/07-built-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8590\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/07-built-1.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a><\/li>\n<li>Open Test Explorer, right-click on the test group and select &#8220;Debug&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/08-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8591\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/08-debug.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a><\/li>\n<li>VisualGDB will automatically launch the tests and will stop on the first failure: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/09-fail.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8592\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/09-fail.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a>You can also use &#8220;Run&#8221; instead of &#8220;Debug&#8221; if you do not want to stop at test failures.<\/li>\n<li>Use the <strong>Call Stack<\/strong> window to see how the unit test function got reached. The <strong>main()<\/strong> function called <strong>CommandLineTestRunner::RunAllTests(),<\/strong> that in turn launched the tests your selected via Test Explorer. If you only selected a specific test or a test group, VisualGDB would automatically patch the test list, preventing other tests from running:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/10-stack.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8593\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/10-stack.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a>If you are planning to add tests to an existing embedded project by referencing the test framework, ensure your implementation of <strong>main()<\/strong> also calls <strong>CommandLineTestRunner::RunAllTests()<\/strong>.<\/li>\n<li>Open a command prompt window in the project directory and run the following command line:\n<pre class=\"\">\"%VISUALGDB_DIR%\\VisualGDB.exe\" \/runtests EmbeddedCppUTestDemo.vgdbcmake \/TargetPath:build\\VisualGDB\\Debug\\EmbeddedCppUTestDemo \/output:TestResults.xml<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/11-cmdline.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8594\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/11-cmdline.png\" alt=\"\" width=\"979\" height=\"512\" \/><\/a>It will automatically upload the test application into the device, run the tests there and save the report in the <strong>TestResults.xml<\/strong> file. See <a href=\"https:\/\/visualgdb.com\/documentation\/tests\/#cmdline\">this page<\/a> for more information about running tests via command line.<\/li>\n<li>Now we will show how to use Software Tracing to record all function calls in the test project. Ensure the Software Tracing is enabled via VisualGDB Project Properties:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/12-trace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8595\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/12-trace.png\" alt=\"\" width=\"1067\" height=\"668\" \/><\/a><\/li>\n<li><strong>Use the Debug-&gt;Windows-&gt;Live Tracing<\/strong> window to add tracepoints in every function that has &#8220;test&#8221; in the name: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/13-tracepoints.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8597\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/13-tracepoints.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a><\/li>\n<li>Explicitly configure the <strong>runOneTest()<\/strong> tracepoint to trace <strong>this-&gt;name_<\/strong> as a NULL-terminated string: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/13a-runone.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8596\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/13a-runone.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a><\/li>\n<li>If you debug your tests now, VisualGDB will record all selected function calls and show them in <strong>Debug-&gt;Windows-&gt;Live Tracing-&gt;Events<\/strong>, so you can see what exactly happened before the test failure:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/14-events.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8606\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/14-events.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a><\/li>\n<li>As we only configured the <strong>runOneTest()<\/strong> tracepoint to capture data (test name), other events will contain no data. Select an event from the <strong>runOneTest()<\/strong> tracepoint to see the actual test name recorded by it. If there are too many events to view each one separately, you can use the <a href=\"https:\/\/visualgdb.com\/documentation\/tracing\/#dataviews\">data views<\/a> in the Trace Data window to create a table where each event will represent a row, and each column will show a particular data item:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/15-data-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8607\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/15-data-1.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a><\/li>\n<li>Once you end the debug session, VisualGDB will create a tracing report containing all the recorded data: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/16-replay-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8608\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/16-replay-1.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a><\/li>\n<li>You can replay the trace reports at any time and use the navigation buttons to step between events and review the recorded data: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/17-nav-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8609\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/17-nav-1.png\" alt=\"\" width=\"1243\" height=\"762\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the CppUTest framework with the STM32 devices. We will create a basic test project,<\/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,248,61,129],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8583"}],"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=8583"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8583\/revisions"}],"predecessor-version":[{"id":8610,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8583\/revisions\/8610"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=8583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=8583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=8583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}