Skip to content

Commit 8bc4f9c

Browse files
committed
Add some unittests
1 parent 6176637 commit 8bc4f9c

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

.github/workflows/unittest.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ jobs:
1818
- name: Lint
1919
run: |
2020
make lint
21+
- name: Test
22+
run: |
23+
make coverage

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
.PHONY: lint
1+
.PHONY: lint test coverage
22

33
lint:
44
python -m pylint check_sensorProbe2plus.py
5+
test:
6+
python -m unittest -v test_check_sensorProbe2plus.py
7+
coverage:
8+
python -m coverage run -m unittest -b test_check_sensorProbe2plus.py
9+
python -m coverage report -m --include check_sensorProbe2plus.py

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pylint==3.3.7
2+
coverage==7.9.0

test_check_sensorProbe2plus.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
3+
import unittest
4+
import unittest.mock as mock
5+
import sys
6+
7+
sys.path.append('..')
8+
9+
from check_sensorProbe2plus import convert_state_to_nagios
10+
from check_sensorProbe2plus import print_status_message
11+
12+
from check_sensorProbe2plus import NagiosState
13+
14+
class UtilTesting(unittest.TestCase):
15+
16+
def test_state(self):
17+
actual = convert_state_to_nagios(1)
18+
expected = NagiosState.UNKNOWN
19+
20+
self.assertEqual(actual, expected)
21+
22+
@mock.patch('builtins.print')
23+
def test_status_message(self, mock_print):
24+
sensor_states = {
25+
"OK": [],
26+
"WARNING": [],
27+
"CRITICAL": [],
28+
"UNKNOWN": [],
29+
}
30+
31+
print_status_message(sensor_states, "perfdata")
32+
33+
calls = [mock.call('OK sensorProbe2plus: Sensor reports that everything is fine|p e r f d a t a ')]
34+
35+
mock_print.assert_has_calls(calls)
36+

0 commit comments

Comments
 (0)