ArduinoUnit

From emboxit
Jump to: navigation, search

ArduinoUnit at code.google (version 1)

  • arduinounit Arduino Unit is an on-chip unit testing framework for Arduino libraries

Create a directory called ArduinoUnit under <arduino installation directory>\libraries.

Copy everything from the src directory to this new directory.

Open the Arduino software IDE and create a new, empty sketch (by clicking File | New or Ctrl+N). Next, import the ArduinoUnit library by clicking Sketch | Import Library | ArduinoUnit. This will insert the following line of code: <cpp>

  1. include <ArduinoUnit.h>

</cpp> Copy and paste the following simple unit testing sketch below the #include line:

<cpp> // Create test suite TestSuite suite;

void setup() {

 Serial.begin(9600);

}

// Create a test called 'addition' in the test suite test(addition) {

 assertEquals(3, 1 + 2);

}

void loop() {

 // Run test suite, printing results to the serial port
 suite.run();

} </cpp> Make sure that your Arduino is connected to your computer and that you have correctly configured the Board and Serial Port settings under the Tools menu.

Upload the sketch to the Arduino (using the 'Upload' button, File | Upload to I/O Board or Ctrl+U).

Turn on the Serial Monitor (using the 'Serial Monitor', Tools | Serial Monitor or Ctrl+Shift+M button) and if you've followed these steps correctly you should see the following output:

Running test suite... Tests run: 1 Successful: 1 Failed: 0


ArduinoUnit at github (version 2)

  • Readme.md is very comprehensive with simple example
Create a directory called ArduinoUnit under <arduino installation directory>\libraries.
Copy everything from the src directory to this new directory.
Open a sketch in arduino and click Sketch | Import Library... | ArduinoUnit to start unit testing.
For example, try this simple unit testing sketch:

<cpp>

  1. line 2 "sketch.ino"
  2. include <ArduinoUnit.h>

test(ok) {

 int x=3;
 int y=3;
 assertEqual(x,y);

}

test(bad) {

 int x=3;
 int y=3;
 assertNotEqual(x,y);

}

void setup() {

 Serial.begin(9600);

}

void loop() {

 Test::run();

} </cpp> Upload this sketch to the Arduino (using the 'Upload' button, File | Upload or Ctrl+U).

Turn on the Serial Monitor (using the 'Serial Monitor' button, Tools | Serial Monitor or Ctrl+Shift+M) and expect to see the following:

Assertion failed: (x=3) != (y=3), file sketch.ino, line 17.
Test bad failed.
Test ok passed.
Test summary: 1 passed, 1 failed, and 0 skipped, out of 2 test(s).