Unit test idea

Just a thought on how to be able to unit test rules. For example give this rule:

var lightsOn = 0

rule "Lights on with start of Dusk"
when
    Channel 'astro_sun_local_civilDusk#event' triggered START 
then
  zwave_device_03f9975a_node18_switch_dimmer.sendCommand(25)
  // zwave_device_03f9975a_node17_switch_binary.sendCommand('ON')
  zwave_device_03f9975a_node2_switch_binary.sendCommand('ON')
  lightsOn = 1
end

We then could add a trigger to the when, like so:

var lightsOn = 0

rule "Lights on with start of Dusk"
when
       Item test_lightsOnWithStartOfDusk received command ON
    or Channel 'astro_sun_local_civilDusk#event' triggered START 
then
  zwave_device_03f9975a_node18_switch_dimmer.sendCommand(25)
  // zwave_device_03f9975a_node17_switch_binary.sendCommand('ON')
  zwave_device_03f9975a_node2_switch_binary.sendCommand('ON')
  lightsOn = 1
  // Turn off the testing state
  test_lightsOnWithStartOfDusk.sendCommand(OFF)
end

I als have create a testable.items file containing

Switch test_lightsOnWithStartOfDusk "test lights on with start of dusk [%s]" <switch> 

This would allow to create a panel with buttons to trigger these rules.

The question is whether this would actually work or has someone already walked this path and has some hints?

Yes, I do
I have several items that I use only to trigger rules when I am testing
A Switch with a mqtt binding topic ‘test’
A dimmer with a mqtt binding topic ‘test’
A contact with a mqtt binding topic ‘test’
That covers most simple test
Sometimes I create an item just for the purpose of testing something and once done I remove it

I do this on an ad hoc basis as I’m developing a rule but I tend to only do it for Channel triggered rules. And once it works I’ll often eliminate the test Item.

In all other cases, I’ll either change the state of Items through the Karaf console or the REST API docs to change or command Items to simulate events to trigger rules I want to test. This lets me avoid adding lots of new Items and adding lots of new controls to my sitemap.

Can you point me to the documentation on how to simulate an event like astro:sun:local:set#event?
I would like to test my config with real events, rather than a workaround.

Install the REST API Documentation (Misc tab in paper UI). Bring up the “UI” and, iirc (I don’t have access to my system at the moment) you can send the event under the Things entry.

I now had the time to do install the Rest API and now I also have installed Postman from https://www.getpostman.com/ and tested the Rest API successfully.

Time to remove all the unnecessary variables :slight_smile:

@rlkoshak Is there a means to trigger channel based events using the REST API?

I believe so. Look in the REST docs under Thing it the related entries. If you don’t find anything then the answer is no.

I’ve never done it myself. I usually have Items linked to the channel so have never needed to try.