From ee8c1246516b97dbbaf30843770ab6e8c271cbd9 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Tue, 30 Nov 2021 23:45:27 -0800 Subject: [PATCH 1/4] initial pytest --- README.md | 8 +++++++- meshtastic/tests/test_int.py | 30 ++++++++++++++++++++++++++++++ pytest.ini | 4 ++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 meshtastic/tests/test_int.py create mode 100644 pytest.ini diff --git a/README.md b/README.md index 58681db..d220cfc 100644 --- a/README.md +++ b/README.md @@ -156,10 +156,16 @@ If you need to build a new release you'll need: ``` apt install pandoc -sudo pip3 install markdown pdoc3 webencodings pyparsing twine autopep8 pylint +sudo pip3 install markdown pdoc3 webencodings pyparsing twine autopep8 pylint pytest ``` To lint, run: ``` pylint meshtastic ``` + +To test, first install this code locally, then run pytest: +``` +pip3 install . +pytest +``` diff --git a/meshtastic/tests/test_int.py b/meshtastic/tests/test_int.py new file mode 100644 index 0000000..ed359d4 --- /dev/null +++ b/meshtastic/tests/test_int.py @@ -0,0 +1,30 @@ +"""Meshtastic integration tests""" +import re +import subprocess +import platform + +import pytest + + +@pytest.mark.int +def test_int_no_args(): + """Test without any args""" + return_value, out = subprocess.getstatusoutput('meshtastic') + assert re.match(r'usage: meshtastic', out) + assert return_value == 1 + + +@pytest.mark.int +def test_int_version(): + """Test '--version'.""" + return_value, out = subprocess.getstatusoutput('meshtastic --version') + assert re.match(r'[0-9]+\.[0-9]+\.[0-9]', out) + assert return_value == 0 + + +@pytest.mark.int +def test_int_help(): + """Test '--help'.""" + return_value, out = subprocess.getstatusoutput('meshtastic --help') + assert re.match(r'usage: meshtastic ', out) + assert return_value == 0 diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..35e4231 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] + +markers = + int: marks tests as integration From 0473820ef4f563793ca7c30f6c52e52e34caabb2 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Wed, 1 Dec 2021 00:13:57 -0800 Subject: [PATCH 2/4] add simple unit test --- README.md | 4 ++++ meshtastic/test/.test_node.py.swp | Bin 0 -> 12288 bytes meshtastic/{tests => test}/test_int.py | 0 meshtastic/test/test_node.py | 16 ++++++++++++++++ pytest.ini | 3 ++- 5 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 meshtastic/test/.test_node.py.swp rename meshtastic/{tests => test}/test_int.py (100%) create mode 100644 meshtastic/test/test_node.py diff --git a/README.md b/README.md index d220cfc..970e8bc 100644 --- a/README.md +++ b/README.md @@ -169,3 +169,7 @@ To test, first install this code locally, then run pytest: pip3 install . pytest ``` +Possible options for testing: +* for more verbosity, add "-v" or even "-vv" like this: pytest -vv +* to run just unit tests: pytest -munit +* to run just integrstion tests: pytest -mint diff --git a/meshtastic/test/.test_node.py.swp b/meshtastic/test/.test_node.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..1df257b1a9ba65e9dd488144ded33089ee9c5e5b GIT binary patch literal 12288 zcmeI&yGjE=6b9g z@C^iOAHzrRY|JKJh>D%$A2_@0%+Ac2ugm6C-TlpNvFenG+ceRfb0aUdUdAu(i2}c= zntm9nb~-q?nG>a~(~3OV8vGKEob?whHDpi<-WIQHE7P#jSl=tgZQF>#Qqb!uS!HbJ zQVFA)a^fFy`8ELzLSR@05?e2>PS8qu*-Z%+a`R$#W^Y(EaBc`d00Izz00bZa0SFAG zfQ?7!oKHU5Jw1N{=(Lef_wFNZ5P$##AOHafKmY;|fB*y_009X6LjhSM8k;0K;zcC7Zp$(>Y+*7x_B>1{~SSX0~b&)s9)5_-Q$9cUO zHC2~M>o-%}Y?CBZp{LuiRkeXT<@0X|eR(&R%k6Sa`upQ>7jYUQtyQnQKW1T#Hn^mn zyPZJlrjvxeC93Nv5d9K&CoOCjeh^0=k%+Rs&I&=a TvU-v3+sV!do+-MO^l93!j~vC= literal 0 HcmV?d00001 diff --git a/meshtastic/tests/test_int.py b/meshtastic/test/test_int.py similarity index 100% rename from meshtastic/tests/test_int.py rename to meshtastic/test/test_int.py diff --git a/meshtastic/test/test_node.py b/meshtastic/test/test_node.py new file mode 100644 index 0000000..0650aa5 --- /dev/null +++ b/meshtastic/test/test_node.py @@ -0,0 +1,16 @@ +"""Meshtastic unit tests for node.py""" +import re +import subprocess +import platform + +import pytest + +from meshtastic.node import pskToString + +@pytest.mark.unit +def test_pskToString(): + """Test pskToString""" + assert pskToString('') == 'unencrypted' + assert pskToString(bytes([0x00])) == 'unencrypted' + assert pskToString(bytes([0x01])) == 'default' + assert pskToString(bytes([0x02, 0x01])) == 'secret' diff --git a/pytest.ini b/pytest.ini index 35e4231..9c7d72e 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,4 +1,5 @@ [pytest] markers = - int: marks tests as integration + unit: marks tests as unit tests + int: marks tests as integration tests From 4def6438033dc3408507d6aad238170f071b03aa Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Wed, 1 Dec 2021 00:24:53 -0800 Subject: [PATCH 3/4] break out unit tests so there is one assert per test --- meshtastic/test/test_node.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/meshtastic/test/test_node.py b/meshtastic/test/test_node.py index 0650aa5..c04e49e 100644 --- a/meshtastic/test/test_node.py +++ b/meshtastic/test/test_node.py @@ -8,9 +8,30 @@ import pytest from meshtastic.node import pskToString @pytest.mark.unit -def test_pskToString(): - """Test pskToString""" +def test_pskToString_empty_string(): + """Test pskToString empty string""" assert pskToString('') == 'unencrypted' + + +@pytest.mark.unit +def test_pskToString_string(): + """Test pskToString string""" + assert pskToString('hunter123') == 'secret' + + +@pytest.mark.unit +def test_pskToString_one_byte_zero_value(): + """Test pskToString one byte that is value of 0""" assert pskToString(bytes([0x00])) == 'unencrypted' + + +@pytest.mark.unit +def test_pskToString_one_byte_non_zero_value(): + """Test pskToString one byte that is non-zero""" assert pskToString(bytes([0x01])) == 'default' + + +@pytest.mark.unit +def test_pskToString_many_bytes(): + """Test pskToString many bytes""" assert pskToString(bytes([0x02, 0x01])) == 'secret' From e5f0dc9c4bb7d16b9ecc2e10621c7aa5e071cd80 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Wed, 1 Dec 2021 00:26:57 -0800 Subject: [PATCH 4/4] fix typo and add more info to readme about adding other types of tests --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 970e8bc..3eee433 100644 --- a/README.md +++ b/README.md @@ -172,4 +172,5 @@ pytest Possible options for testing: * for more verbosity, add "-v" or even "-vv" like this: pytest -vv * to run just unit tests: pytest -munit -* to run just integrstion tests: pytest -mint +* to run just integration tests: pytest -mint +* if you want to add another classification of tests, then look in pytest.ini