Driver Testing

For driver development, please refer to Getting Started With Driver and Driver Programming Model

The old-school approach

In traditional embedded development, the testing procedure involves burning the dev image onto the hardware. Since the image file is usually quite large, the burning process may take from a few minutes to an hour.

When an application requires numerous logic testings, the traditional testing procedure can become extremely time-consuming.

Ruff testing for drivers

Ruff has a driver runner to provide an infrastructure for driver testing. This allows testings to run directly on the dev machine without deploying onto the hardware.

Let’s examine an example from Getting Started With Driver:

var runner = require('ruff-driver-runner').runner;
var when = require('ruff-mock').when;

export['test should work well'] = function() {
runner.run(driverPath, function(device, context) {
var gpio = context.arg('gpio');
when(gpio.read()).thenReturn(1);
assertOk(dev.readValue() == 1);
}
}

require('test').run(exports);

(test/driver-test.js)

Use the following command to test script:

ruff test/driver-test.js

Notes:

  • ruff-driver-runner: driver runner provided by ruff. All devices are simulated to mock objects in order to run on the dev machine.
  • ruff-mock: a mock framwork powered by Ruff that supports mock objects.
  • Our test framework complies with CommonJS Unit Testing Standards.

Now let’s take a look at Driver Runner and Mock Interface, the core components in Ruff driver testing.

Driver Runner

Driver Runner is a simulated test environment to execute application without hardware, as follows:

var runner = require('ruff-driver-runner');

runner.run(driverPath, function(device, context) {
// your test code when your driver is ready

});

Remeber to include ruff-driver-runner in the beginning:

var runner = require('ruff-driver-runner');

Driver Runner has a run function with two parameters. The first, driverPath, is the path of the driver folder. The second parameter is a function with two parameters:

  • device: device object to be tested.
  • context: contains config information (in the above example, we might get gpio).

Mock Interface

Mock Interface allows you to test driver API on your dev machine. You may:

  • Set return values (e.g., GPIO) to 1.
  • Verify hardware behaviors on dev machine.
  • Trigger events (e.g., giving GPIO an interruption).

ruff-mock sets return values and behaviors while event triggers events.

run has a callback function with a context parameter that supports mock interface via the following command:

var gpio = context.arg('gpio');

Set return value

The following example shows how to set GPIO’s value to 1:

when(gpio).read().thenReturn(1);

Verify hardware behavior

To test whether a method is invoked, use verify, as in the following example:

verify(gpio).write(1);

Trigger events

EventEmitter allows you to use emit to trigger an event, as in the following example:

gpio.emit('interrupt');