The old-school approach
In traditional embedded development, the testing procedure involves burning the device 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 procedure
Ruff has an app runner and a simulator to provide an infrastructure for application testing. This allows testings to run directly on the dev machine without deploying onto the hardware.
In our getting started guide, we provide an example using a built-in driver. Notice that all we need to do is invoke the right functions, and we can leave the rest of the work to the driver:var runner = require('ruff-app-runner');
var verify = require('ruff-mock').verify;
exports['test should call turn on while application is ready'] = function() {
runner.run(appPath, function() {
verify($('led-0')).turnOn();
});
};
require('test').run(exports);
(test/app-test.js)
We then use the following command to run a test script:ruff test/app-test.js
Notes:
ruff-app-runner
is the application runner provided by Ruff. All the devices are simulated to mock objects in order to run on the dev machine.ruff-mock
is 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 App Runner and Mock Objects, the core components in Ruff application testing.
App Runner
App Runner is a simulated test environment to execute an application without hardware, as follows:var runner = require('ruff-app-runner');
runner.run(appPath, function() {
// your test code when your application is running
}).end(function() {
// your test code after your application has ended
});
Remember to include ruff-app-runner
to use functions in App Runner.
Our test environment includes two parts, one for when the app is running and one for after the app has ended:
run
: app is ready, and the function within ready() in app code has been invoked. Therun
function has two parameters.appPath
is the path to the application for App Runner to load settings. The other parameter is a function that runs our test code.end
: app has ended, and the function within end() in app code has been invoked.
For more information about ready
and end
in app code please refer to Programming Model。
Mock objects
The Mock Objects component is used to substitute for real devices in the test environment. You may:
- Set mock data values (e.g., data from a temperature sensor).
- Verify hardware behaviors (e.g., LED is on/off).
- Trigger events (e.g., light a fire to the fire detector).
Mock data and hardware behaviors are supported by ruff-mock
, and event triggers are supported by event
.
Mock data
Mock Objects can set a data value to be returned, as below:when($('temperature')).getTemperature().thenReturn(24);
Hardware behaviors
Use verify
to verify a behavior.verify($('led')).turnOn();
Trigger events
Use emit
to trigger an event:$('ir').emit('away');