forked from openhab-scripters/openhab-helper-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting_example.py
More file actions
39 lines (30 loc) · 1.13 KB
/
testing_example.py
File metadata and controls
39 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
This example demonstrates logging, item trigger decorator and the test runner.
Required Items:
Number TestNumber1
Number TestNumber2
"""
import unittest
import time
from openhab.log import logging
from openhab.testing import run_test
from openhab.triggers import item_triggered, ITEM_UPDATE
import openhab.items
@item_triggered("TestNumber1", ITEM_UPDATE)
def double_the_value():
events.postUpdate("TestNumber2", str(2 * items.TestNumber1.floatValue()))
class MyUnitTest(unittest.TestCase):
def setUp(self):
openhab.items.add("TestNumber1", "Number")
openhab.items.add("TestNumber2", "Number")
def tearDown(self):
openhab.items.remove("TestNumber1")
openhab.items.remove("TestNumber2")
def test_item(self):
events.postUpdate("TestNumber1", str(5))
time.sleep(1)
self.assertEqual(items.TestNumber2.floatValue(), 10)
# results are also logged to openHAB log file
# status can be used to take actions like sending notifications
# results is a JSON formatted string (will probably change to return Python dict instead)
print run_test(MyUnitTest)