How to - Arduino unit testing in Visual Studio 2019 with VS Micro

Hello all,

TLDR; Create and run unit tests for your Arduino code in VS2019 but only if you don't solely use sketch files.

As a long-time user of these forums, I have yet to contribute something back, so here goes...

I was keen to improve the quality of my code and avoid having to conduct endless flashes to the MCU in debugging, so I investigated Arduino code-friendly unit testing using my IDE of choice, Microsoft Visual Studio 2019 Community edition coupled with VisualMicro. VisualMicro is a specific Arduino extention to VS2019 that simplifies uploading to MCUs, library mangement etc.

These steps work for me on VS2019 16.8.6 and VM v21.106.0, with Arduino IDE 1.8.13. You will need to ensure that you have installed the Microsoft Native Unit C++ Testing options and obviously C++ language support as part of VS2019

Note that I do most of my programming using standard C classes and tie these in with the Arduino sketches (*.ino files). This method will not work if you only use the sketch file.
I tried adding Arduino references to a testing project and testing references to an Arduino project but I had a never-ending nightmare trying to resolve myriad issues this seems to generate.
It took me a while to find a combination that worked but eventually I found that removing Arduino-specific code from my libraries, I could use the enhanced and convenient testing functions within the IDE (such as test explorer) with no compile errors etc. This is necessary because the testing project has no idea about Arduino core API functions (all the stuff like millis(), digitalWrite() etc.).

This is quite annoying, so I found it quite workable to write a test 'harness' containing the minimal Arduino functions I needed and including this in the test project. Note that this means changes in Arduino platform will require review of the test harness. I also used compiler directives (#ifdef etc.) to omit/include portions of testing-specific code and prevent duplicate definitions of these functions in the normal Arduino project.

I have included an illustrative screenshot of my IDE with a working solution open, along with a simple diagram to explain how the components fit together.

A lot of the technicalities will depend on your project structure and how familiar you are with C and the VS2019 IDE; an overview of the steps can be found below to allow you to replicate this, noting you may have to do some debugging to get it just-right for your setup:

(From within VS2019, empty environment/no solution or project loaded):

  • Create new Arduino project (creates an *.ino sketch and associated VS2019 solution and vcproj files)
  • Add a new Native Unit C++ Testing project to the solution
  • Add the Arduino project as a dependency to the testing project
  • Add/create your C/C++ classes and other files, DO NOT PUT Arduino API code from your classes etc. (this must only reside in the sketch which isn't tested), ensuring they are referenced in the testing project too
  • Ensure the test project is set to Debug x86 profile (I needed to do this anyway to avoid weird compile errors)
  • Ensure the Arduino project has an appropriate Platform Toolset specified within VS2019 Project Properties (Configuration Properties->General)
  • Check your Arduino project compiles (right click the project [NOT THE SOLUTION] and Build) - regularly checking this will ensure you will actually be able to upload your code to the Arduino!
  • Check your testing project compiles (right click the project [NOT THE SOLUTION] and Build), you should see the template tests appear in Test Explorer window (Tests->Test Exporer) - this code never gets uploaded to the Arduino but is compiled and executed on your PC; much faster and convenient and saves previous flash write cycles on your Arduino hardware
  • ---Don't proceed until you can build both projects separately---
  • #include your code to be tested in the automatically generated test cpp file - this normally means some static functions and instances of classes I am working with
  • #include your test harness code if you are using this (this should include the CPP Testing Framework namespace provided in the generated test classes)
  • Create a simple test class to check something mega easy in your class to prove the testing setup works

Et voila! You can now quickly and easily test and debug your code, improving the quality, allowing regression testing and greatly increasing testing code coverage.

Reference for the MS CPP Testing Framework:

Hopefully this helps someone.

Hi, I found this way of debugging the code interesting, but I think that the person who starts with Arduino, may prefer only to have better performance in the compilation, since the Arduino Flash memory usually supports thousands of rewrites, so my tip is:

To use Arduino IDE in Linux, is much faster than using the IDE on Windows.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.