Arduino Shared Project: PC Debugging and Automated Testing

About: News November 2019. Welcome to the place where we have started to build useful examples. Arduino for Visual Studio provides an Arduino compatible development environment in Microsoft Visual Studio. The tools...

Often there is code in your project which is independent of hardware and platform, for example to perform some standard mathematical operations, or a custom hash/encryption mechanism.

Testing this code on-chip takes a long time due to flashing, and can be difficult to debug and test fully, even with hardware debuggers.

This is where using Shared Projects in Visual Micro allows you to employ standard Unit Tests and debugger, which run on your PC, saving you a lot of time and frustration.

A video of all steps here is on step 6 and will take you to the end of this guide.

Supplies:

Visual Micro + Visual Studio

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Create Shared Project Code

Load VS > Create New Project > Arduino Shared Code Project

Here we can add the CPP and H files and any code we want to share between the PC and our Arduino projects.

Step 2: Create Unit Test Project

Right Click on Solution > Add > Project / File > New > Project

Add a "Native Unit Test Project"

Right click the new Project > Add > Reference

Shared Projects

Check the box by our Shared code project from the previous step

OK

Step 3: Create Arduino Project

Right Click on Solution > Add > Project / File > New > Project

Add an "Arduino Project"

Again include the Reference to our Shared Code Project in this Arduino Project

Now add the #include for the Shared Project into your ino file so it can be used...

Right click the Arduino Project again and ensure it is "Set as StartUp Project"

Step 4: Add Unit Tests

We will need to include the header file from our Shared Project into the Unit Test class.

Remove the header "pch.h" from the Unit Test Class, and right click the Shared Code Project > Properties > C++ > Precompiled Headers > Change to "Do Not Use Precompiled Headers"

Within the CPP file of your Unit Test project you can now define the test methods / classes to run.

Using the standard test structures with Assert and Logger allows you to quickly add in the tests you want to cover.

Navigate to Test > Test Explorer to load the test tool (and add the right click options to the project for Run/Debug)

If you want to debug these tests on your PC then you can right click on the Unit test Project and select "Debug Tests", and set breakpoints as normal in your code.

Step 5: Run Unit Tests

Now we have our Shared Project code and Unit Test written, we can try it out...

Right click on the Unit Test Project, or the Shared project, and click Run Tests.

This will load the test runner to manage and execute these tests on your PC against your shared Library code.

Step 6: Upload to Arduino

Now all our Unit Tests have passed, we can upload it to the Arduino to check it runs as we expect, but at least our issues should be confined to the Arduino Project code now at least.

Simply click the Build / Build & Upload vMicro buttons after selecting your COM port as normal.

Step 7: Find Out More

Be the First to Share

    Recommendations

    • Instrument Contest

      Instrument Contest
    • Make it Glow Contest

      Make it Glow Contest
    • STEM Contest

      STEM Contest

    Discussions