{"id":5368,"date":"2020-01-16T16:38:19","date_gmt":"2020-01-17T00:38:19","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=5368"},"modified":"2020-01-16T16:38:19","modified_gmt":"2020-01-17T00:38:19","slug":"creating-mbed-unit-tests-with-visualgdb","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/mbed\/tests\/","title":{"rendered":"Creating Mbed Unit Tests with VisualGDB"},"content":{"rendered":"<p>This tutorial shows how to create, build and debug unit test for Advanced Mbed Projects with Visual Studio and VisualGDB. We will create a basic project consisting of the main application and a library and will show how to use VisualGDB to create unit tests and selectively run subsets of them.<\/p>\n<p>Before you begin, install VisualGDB 5.5 or later.<\/p>\n<ol>\n<li>Start Visual Studio and launch the VisualGDB Mbed Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/01-prj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5369\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/01-prj.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>Enter the name and location for the created project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/02-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5370\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/02-name.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>On the first page of the wizard select &#8220;Create a new Blinking LED project&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/03-type.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5371\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/03-type.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>On the next page select the ARM toolchain and the targeted board:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/04-board.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5372\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/04-board.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><strong>Warning: mbed unit tests report the test results via an on-board serial port. If your target (e.g. STM32F4Discovery) does not have a primary serial port accessible from the computer, use an external USB-to-COM adapter and make sure you can see the serial port output from the board before proceeding with unit tests.<\/strong><\/li>\n<li>Connect your board to the USB port and let VisualGDB detect the debug configuration. You can use the &#8220;Test&#8221; button to automatically verify the debug settings:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/05-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5373\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/05-debug.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to create the project. VisualGDB will generate a very basic &#8220;Blinking LED&#8221; project consisting of one source file. Locate the &#8220;Test Cases&#8221; item in Solution Explorer, right-click on it and select &#8220;Enable Building of Unit Tests&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/06-tests.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5374\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/06-tests.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>This will switch the solution to a separate &#8220;Tests (Debug)&#8221; configuration and will allow managing mbed unit test binaries via Solution Explorer. Discovering and building mbed tests increases the build time of the project, so you can switch back to the regular &#8220;Debug&#8221; configuration when you are done running the tests. Right-click on the &#8220;Test Cases&#8221; item again and select &#8220;Add a new Mbed Test Binary&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/07-binary.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5375\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/07-binary.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>Mbed test binaries are organized in groups and are stored in the <strong>TESTS\\&lt;group&gt;\\&lt;binary&gt;<\/strong> folders. Furthermore, each binary can contain one or more unit tests. VisualGDB will ask for the binary, group and test names when creating a new test binary. Proceed with the default values:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/08-binname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5376\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/08-binname.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>Once the binary has been created, press Ctrl-Shift-B to build the solution. Initially it will fail due to a multiple definition of <strong>main()<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/09-multiple.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5377\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/09-multiple.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>In order to fix it, locate the normal <strong>main()<\/strong> function of your application and surround it with <strong>#ifndef MBED_TEST_MODE<\/strong>. This will make sure that it will not get included into the unit test binaries and the build will now succeed:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/10-succeeded.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5378\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/10-succeeded.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>\u00a0 Before you can run mbed tests with VisualGDB, you would need to select the COM port connected to the board. This can be done on the Unit Tests page of VisualGDB Project Properties:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/11-comport.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5379\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/11-comport.png\" alt=\"\" width=\"844\" height=\"553\" \/><\/a>Note that VisualGDB will automatically pick up the baud rate from the mbed settings on the Mbed Project page.<\/li>\n<li>Add another unit test to the test binary as shown below:\n<pre class=\"\">static control_t MbedTest1(const size_t call_count) \r\n{\r\n    \/\/TODO: Place your test logic here\r\n\r\n    TEST_FAIL_MESSAGE(\"This test has just failed\");\r\n    return CaseNext;\r\n}\r\n\r\nstatic control_t MbedTest2(const size_t call_count)\r\n{\r\n    \/\/TODO: Place your test logic here\r\n\r\n    TEST_ASSERT_EQUAL(4, 2 * 2);\r\n    return CaseNext;\r\n}\r\n\r\nstatic Case s_TestCases[] = \r\n{\r\n    Case(\"MbedTest1\", MbedTest1),\r\n    Case(\"MbedTest2\", MbedTest2),\r\n\t\/\/TODO: add more unit tests here\r\n};<\/pre>\n<\/li>\n<li>Build the project again and open the <strong>Test-&gt;Windows-&gt;Test Explorer <\/strong>window. VisualGDB will discover the tests inside the binary and will allow using the regular Visual Studio GUI to run them. Select any subset of tests, right-click on them and select &#8220;<strong>Run Selected Tests<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/12-run.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5380\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/12-run.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>VisualGDB will automatically program the necessary test binary, patch it to only run the selected tests and will display the test results in the Test Explorer window. If you have selected tests from multiple test binaries, VisualGDB will automatically program each of the binaries, run the selected tests from it and combine the results together. Unlike the regular mbed test runs, if one of the tests fails, VisualGDB will automatically reset the target and restart the remaining tests:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/13-failed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5381\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/13-failed.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>Now we will show how to create unit tests for an mbed library. Right-click on the <strong>Libraries<\/strong> item in Solution Explorer and select &#8220;<strong>Add-&gt;New Item<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/14-lib.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5382\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/14-lib.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>Enter the name of the library and click &#8220;OK&#8221;. Once the library has been added, right-click on it and select &#8220;<strong>Add a new Mbed Test Binary<\/strong>&#8221; again:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/15-newtest.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5383\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/15-newtest.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>Build the project one more time and see how VisualGDB now shows the tests from both binaries in the Test Explorer window:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/16-found.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5384\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/16-found.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>Set a breakpoint in the new test method, right-click on the test in Test Explorer and select &#8220;<strong>Debug Selected Tests<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/17-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5385\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/17-debug.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>VisualGDB will automatically launch the corresponding test binary in the debugger, patch it to only run the selected test(s) and the breakpoint will trigger: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/18-breakpoint.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5386\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/18-breakpoint.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a>If a test fails during a debug run, VisualGDB will automatically stop the target, letting you examine the context of the failure.<\/li>\n<li>Now we will show how to troubleshoot communication issues with mbed tests. Open the Raw Terminal page of VisualGDB Project Properties and enable the raw terminal on the mbed COM port. Ensure you specify the correct baud rate, as it won&#8217;t get automatically applied here:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/19-term.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5387\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/19-term.png\" alt=\"\" width=\"844\" height=\"553\" \/><\/a><\/li>\n<li>Right-click on the test binary in Solution Explorer and select &#8220;<strong>Debug Selected Test Case<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/20-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5388\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/20-debug.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>Once the debugging starts, use the <strong>Debug-&gt;Break All<\/strong> command to stop the target and switch to the Call Stack window to see what is going on:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/21-setup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5389\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/21-setup.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>The mbed&#8217;s <strong>Greentea<\/strong> test framework will be waiting for the &#8220;sync&#8221; token on the primary serial port. Copy the following text to the Raw Terminal window:\n<pre class=\"\">{{__sync;test123}}<\/pre>\n<p>Mbed will begin running tests, printing the test output to the serial port:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/22-rawdebug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5390\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/22-rawdebug.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>If this doesn&#8217;t happen, double-check that the COM port used by mbed (defined as the USBTX and USBRX pins) is connected to your computer and that you are using the correct baud rate:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/usbtx.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5394\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/usbtx.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>As building\/scanning each test binary takes additional time, you can disable individual test binaries via Solution Explorer to speed up the project rebuilds:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/23-disable.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5391\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/23-disable.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>Once a binary is disabled, it will not be built in response to the &#8220;Build Solution&#8221; command and tests from it will not appear in Test Explorer:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/24-disabled.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5392\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/24-disabled.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<li>When you are done testing, simply switch the configuration back to <strong>Debug<\/strong> or <strong>Release<\/strong> and VisualGDB will stop querying and building the unit tests for this project. Any changes to tests made via Solution Explorer window will be preserved and applied next time you switch to the <strong>Tests<\/strong> configuration:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/25-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5393\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/11\/25-debug.png\" alt=\"\" width=\"1227\" height=\"801\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create, build and debug unit test for Advanced Mbed Projects with Visual Studio and VisualGDB.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[53,93,115,129],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5368"}],"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=5368"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5368\/revisions"}],"predecessor-version":[{"id":5396,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5368\/revisions\/5396"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=5368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=5368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=5368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}