{"id":2236,"date":"2016-11-17T19:34:52","date_gmt":"2016-11-18T03:34:52","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=2236"},"modified":"2016-11-17T19:34:52","modified_gmt":"2016-11-18T03:34:52","slug":"running-c-unit-tests-on-android-devices","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/android\/tests\/","title":{"rendered":"Running C++ Unit Tests on Android devices"},"content":{"rendered":"<p>This tutorial shows how to run C++ unit tests on your Android devices. We will create a basic unit test based on the CppUTest framework that will check that the device has an accelerometer.<\/p>\n<p>Before you begin, install VisualGDB 5.2 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Android Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2237\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname5.png\" alt=\"01-prjname\" width=\"862\" height=\"531\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname5.png 862w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname5-300x185.png 300w\" sizes=\"(max-width: 862px) 100vw, 862px\" \/><\/a><\/li>\n<li>On the first page of the wizard select &#8220;Create a unit test project&#8221; and pick a framework. VisualGDB supports CppUTest and Googletest out-of-the-box and provides an extensibility interface to support any other 3rd-party framework:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-unittest.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2238\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-unittest.png\" alt=\"02-unittest\" width=\"822\" height=\"642\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-unittest.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-unittest-300x234.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a>Normally VisualGDB will create\u00a0 Java app with C++ code that will run the unit tests inside a separate thread. If you don&#8217;t need the App context, you can select &#8220;Run tests inside a command-line executable&#8221; to create a stand-alone command-line Android binary.<\/li>\n<li>Proceed with the default settings on the &#8220;Project Source&#8221; page:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-activity.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2239\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-activity.png\" alt=\"03-activity\" width=\"822\" height=\"642\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-activity.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-activity-300x234.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>On the last page of the wizard select the platform you would like to target and the hardware platforms:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-platform.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2240\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-platform.png\" alt=\"04-platform\" width=\"822\" height=\"642\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-platform.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-platform-300x234.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate the project. Build it with Ctrl-Shift-B and open the Test Explorer to see the example unit tests detected by Visual Studio:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-tests.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2241\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-tests.png\" alt=\"05-tests\" width=\"1019\" height=\"644\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-tests.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-tests-300x190.png 300w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Select Test-&gt;Run All to launch them on your device. The FailingTest should immediately fail, while the others should succeed:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-testsdone.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2242\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-testsdone.png\" alt=\"06-testsdone\" width=\"1019\" height=\"644\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-testsdone.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-testsdone-300x190.png 300w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Now we will add a basic tests that ensures that your device has an accelerometer. Replace the code in the tests file with the following:\n<pre class=\"\">#include &lt;CppUTest\/CommandLineTestRunner.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;android\/sensor.h&gt;\r\n\r\nTEST_GROUP(HardwareTests)\r\n{\r\n};\r\n\r\nTEST(HardwareTests, AccelerometerTest)\r\n{\r\n\u00a0\u00a0\u00a0 ASensorList sensorList;\r\n\u00a0\u00a0\u00a0 ASensorManager* sensorManager = ASensorManager_getInstance();\r\n\u00a0\u00a0\u00a0 CHECK(sensorManager);\r\n\u00a0\u00a0\u00a0 int n = ASensorManager_getSensorList(sensorManager, &amp;sensorList);\r\n\u00a0\u00a0\u00a0 bool accelerometerFound = false;\r\n\u00a0\u00a0\u00a0 for (int i = 0; i &lt; n; i++)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 const ASensor* sensor = sensorList[i];\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (sensor == NULL)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (ASensor_getType(sensor) != ASENSOR_TYPE_ACCELEROMETER)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 const char* name = ASensor_getName(sensor);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 const char* vendor = ASensor_getVendor(sensor);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 printf(\"Found accelerometer: %s %s\\n\", vendor, name);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 accelerometerFound = true;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 CHECK(accelerometerFound);\r\n}<\/pre>\n<\/li>\n<li>Add &#8220;-landroid&#8221; to the System Library Names field in VisualGDB Project Properties so that your project can use the ASensor_xxx() functions:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-library.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2243\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-library.png\" alt=\"07-library\" width=\"899\" height=\"703\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-library.png 899w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-library-300x235.png 300w\" sizes=\"(max-width: 899px) 100vw, 899px\" \/><\/a><\/li>\n<li>Build the project. The Test Explorer window will show the newly added test:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-buildtests.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2244\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-buildtests.png\" alt=\"08-buildtests\" width=\"1019\" height=\"644\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-buildtests.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-buildtests-300x190.png 300w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Start it via Test-&gt;Run All Tests or by right-clicking on it and selecting &#8220;Run Selected Tests&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-pass.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2245\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-pass.png\" alt=\"09-pass\" width=\"1019\" height=\"644\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-pass.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-pass-300x190.png 300w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>If your device has an accelerometer, the test should succeed. If it does not, you can easily debug it by right-clicking on it in Test Explorer and selecting &#8220;Debug selected tests&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2246\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-debug.png\" alt=\"10-debug\" width=\"1019\" height=\"644\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-debug.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-debug-300x190.png 300w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>You can use the Call Stack window to understand the context in which the test is run. VisualGDB creates a separate thread using the <strong>pthread_create()<\/strong> function and passes control to the CppUTest framework. VisualGDB automatically selects the tests to run and interprets the information about successful and failed runs:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-shell.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2247\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-shell.png\" alt=\"11-shell\" width=\"1019\" height=\"644\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-shell.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-shell-300x190.png 300w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>VisualGDB also generates a very basic Java source file that creates a simple activity and invokes the StartTests() function so that the native tests can be started:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-javacode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2248\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-javacode.png\" alt=\"12-javacode\" width=\"1019\" height=\"644\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-javacode.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-javacode-300x190.png 300w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to run C++ unit tests on your Android devices. We will create a basic unit test<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[31,129],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2236"}],"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=2236"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2236\/revisions"}],"predecessor-version":[{"id":2249,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2236\/revisions\/2249"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=2236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=2236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=2236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}