Rule unit tests


I’m new to openhab. Currently I’m trying to do a rollershutter setup. It becomes complicated pretty quickly. Can I write unit tests to proof that my code works? If not, is there something similar to unit tests (I read about scripts, but I don’t know how to use that).

Somewhere I read that I could use python to write rules (and then I could use the python do the unit tests), but I don’t know if this is a good path to go (is it really well supported)?


The short answer is no, there is no direct way to unit tests rules.

The JSR233 plug in let’s you will you’re rules using Jython, JavaScript, or Groovy and there may be a way to create unit tests for rules written in those languages. But you cannot write unit tests for the default rules language using JSR233.

You might be able to set something up in your rules with proxy items and such but I can’t see how that would would be any better than and certainly more work and more complex than going back to good old debugging with log statements, which is about all we get.

There is a new experimental rules engine in the works the might have the structure and language hooks to support unit tests. It will support libraries. You might open a discussion on the ESH forums to discuss it and if warranted open a feature request.

Hopefully someone who knows JSR233 will comment on whether unit tests are supported.

Thanks Rich. Will give JSR233 a closer look. The fear I have is that this is less well supported than the plain Xtend language, and it might easily get deprecated in a couple of years. I’m looking for the longer term solution here.

If anything, the default Rules DSL is what will be replaced in the future. There is a whole new rules engine being developed and the JSR233 add-on is unlikely to go away.

Unfortunately, I think with OH 2 that the default Rules DSL is the most completely supported and documented for use in OH 2, but if you want longest term support you probably want to look at the Experimental Rules Engine or JSR233.

Interesting. Does anybody have a Jython setup working and can share some example rules?

Let me try if the instructions at work, then I’ll ask again.