thing-it-test
v0.1.6
Published
Test and verification for all [thing-it-node] Plugins.
Downloads
4,880
Readme
thing-it-test
Test and verification utility for [thing-it-node] Plugin development.
Device, Sensor and Actor Test
Setting up a Test
Install the package via
npm install thing-it-test
in the directory of your [thing-it-node] Plugin.
In your test program load the [thing-it] Test Driver
var testDriver = require("thing-it-test").createTestDriver({logLevel: "debug"});
Load your Device, Actor and Sensor Plugins, e.g.
testDriver.registerDevicePlugin(__dirname + "/dummyDevice");
testDriver.registerUnitPlugin(__dirname + "/dummyActor");
testDriver.registerUnitPlugin(__dirname + "/dummySensor");
- note that you need to load Device Plugins before Unit Plugins - and create your configuration
var testConfiguration = {
label: "Room 22",
id: "room22",
devices: [{
id: "dummyDevice1",
label: "Dummy Device 1",
plugin: "dummy-device/dummyDevice",
logLevel: "debug",
configuration: {simulated: true, frequency: 5},
actors: [{
id: "dummyActor1",
label: "Dummy Actor 1",
type: "dummyActor",
logLevel: "debug",
configuration: {
}
}],
sensors: [{
id: "dummySensor1",
label: "Dummy Sensor 1",
type: "dummySensor",
logLevel: "debug",
configuration: {
frequency: 5
}
}]
}]
};
You can also load the configuration via require, e.g.
var testConfiguration = require("./dummyConfiguration")
Then run the test, e.g.
testDriver.start({
configuration: testConfiguration,
heartbeat: 10,
simulated: true
}).then(function () {
setTimeout(function () {
testDriver.dummyDevice1.dummyActor1.on();
testDriver.dummyDevice1.dummyActor1.off();
testDriver.dummyDevice1.dummyActor1.toggle();
testDriver.stop().then(function () {
}).fail(function (error) {
testDriver.logError("Could not stop Device: " + error);
});
}, 20000);
}).fail(function (error) {
testDriver.logError("Could not start Device: " + error);
});
Find a full example here.
Using mocha
If you have installed mocha via
npm install mocha -g
you can define tests like
describe('[thing-it] Philips Hue Plugin', function () {
var testDriver;
before(function () {
testDriver = require("thing-it-test").createTestDriver();
testDriver.registerDevicePlugin(__dirname + "/../hueBridge");
testDriver.registerUnitPlugin(__dirname + "/../default-units/lightBulb");
testDriver.registerUnitPlugin(__dirname + "/../default-units/livingColorLamp");
});
describe('Start Configuration', function () {
this.timeout(5000);
it('should complete without error', function () {
return testDriver.start({
configuration: require("../examples/configuration.js"),
heartbeat: 10,
logLevel: "error"
});
});
});
describe('Service calls', function () {
it('should complete without error', function (done) {
testDriver.philipsHueBridge.lightBulbBedroom.setBrightnessPercent({brightnessPercent: 100});
testDriver.philipsHueBridge.livingColorLampBar.setBrightnessPercent({brightnessPercent: 100});
testDriver.philipsHueBridge.lightBulbBedroom.setBrightnessPercent({brightnessPercent: 0});
testDriver.philipsHueBridge.livingColorLampBar.setRgbHex({rgbHex: "#FF0000"});
done();
});
});
});
and run with mocha.
Using Listeners
Many test cases for [thing-it-node] Plugins require listening to the Device's and Actor's reactions to Service Calls or just listening to Sensor Events.
To cover this in test suites you can register event listeners with the test driver e.g. as in the following mocha code
describe('Brightness = 100', function () {
this.timeout(5000);
before(function () {
testDriver.removeAllListeners();
});
it('should produce Actor State Change message', function (done) {
testDriver.addListener({
publishActorStateChange: function (device, actor, state) {
if (actor.id === "lightBulbBedroom" && device.id === "philipsHueBridge" && state.brightnessPercent === 100)
{
done();
}
else
{
done("Unexpected Actor State Change message.");
}
}
});
testDriver.philipsHueBridge.lightBulbBedroom.setBrightnessPercent({brightnessPercent: 100});
});
});
Listeners are available for
- publishMessage(message)
- publishEvent(event)
- publishDeviceStateChange(device, state)
- publishDeviceOperationalStateChange(device, state)
- publishDeviceStateChangeHistory(device, history)
- publishActorStateChange(device, actor, state)
- publishActorOperationalStateChange(device, actor, state)
- publishActorStateChangeHistory(device, actor, history)
- publishSensorStateChange(device, sensor, state)
- publishSensorOperationalStateChange(device, sensor, state)
- publishSensorStateChangeHistory(device, sensor, history)