Skip to content

Commit b36f735

Browse files
ligurioTotktonada
authored andcommitted
test: add unit tests
Tests written using unittest [1] and hypothesis [2] modules. We use hypothesis v4.57.0 because it is a latest version with Python 2.7 support [3]. How-to run: $ make test_unittest 1. https://docs.python.org/3/library/unittest.html 2. https://hypothesis.readthedocs.io/en/latest/ 3. https://hypothesis.readthedocs.io/en/latest/changes.html#v4-57-0
1 parent 54783c2 commit b36f735

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ htmlcov/
4141
.coverage
4242
.coverage.*
4343
.cache
44+
.hypothesis/
4445
nosetests.xml
4546
coverage.xml
4647
*,cover

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ luacheck:
1515
test_integration:
1616
$(PYTHON) test/test-run.py --force $(TEST_RUN_EXTRA_PARAMS)
1717

18-
test: test_integration
18+
test_unittest:
19+
$(PYTHON) -m unittest discover test/unittest/
1920

20-
.PHONY: lint flake8 luacheck test test_integration
21+
test: test_unittest test_integration
22+
23+
.PHONY: lint flake8 luacheck test test_integration test_unittest

requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
flake8==3.7.9
2+
hypothesis==4.*
5.93 KB
Binary file not shown.

test/unittest/test_lib_utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import socket
2+
import unittest
3+
4+
from hypothesis import given, settings
5+
from hypothesis.strategies import integers
6+
7+
import lib.utils as utils
8+
9+
class TestUtils(unittest.TestCase):
10+
def test_extract_schema_from_snapshot(self):
11+
snapshot_path = 'test/unittest/00000000000000000003.snap'
12+
v = utils.extract_schema_from_snapshot(snapshot_path)
13+
self.assertEqual(v, (2, 3, 1))
14+
15+
@settings(max_examples=5)
16+
@given(port=integers(65100, 65535))
17+
def test_check_port(self, port):
18+
def open_socket(p):
19+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
20+
s.bind(('localhost', p))
21+
s.listen(0)
22+
return s
23+
status = utils.check_port(port, rais=False, ipv4=True, ipv6=False)
24+
self.assertEqual(status, True)
25+
s = open_socket(port)
26+
status = utils.check_port(port, rais=False, ipv4=True, ipv6=False)
27+
s.close()
28+
self.assertEqual(status, False)
29+
30+
31+
if __name__ == "__main__":
32+
unittest.main()

0 commit comments

Comments
 (0)