From 20c4eaca4c6f35d8ffc27e995c326ed2fac06f80 Mon Sep 17 00:00:00 2001 From: Siddharth Chandrasekaran Date: Sat, 7 Oct 2023 20:06:21 +0200 Subject: [PATCH] pytest: Add new test for CP/PD status reports Signed-off-by: Siddharth Chandrasekaran --- tests/pytest/README.md | 8 +++++ tests/pytest/test_status.py | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tests/pytest/README.md create mode 100644 tests/pytest/test_status.py diff --git a/tests/pytest/README.md b/tests/pytest/README.md new file mode 100644 index 00000000..27ba1675 --- /dev/null +++ b/tests/pytest/README.md @@ -0,0 +1,8 @@ +# Run Tests + +`make check` on cmake builds will invoke pytest correctly. During development, +it might be useful to run an individual test (instead of everything). To do so, + +``` +PYTHONPATH=../../build/python/ python3 -m pytest -vv -s test_events.py::test_event_input +``` diff --git a/tests/pytest/test_status.py b/tests/pytest/test_status.py new file mode 100644 index 00000000..3a8178cb --- /dev/null +++ b/tests/pytest/test_status.py @@ -0,0 +1,68 @@ +# +# Copyright (c) 2023 Siddharth Chandrasekaran +# +# SPDX-License-Identifier: Apache-2.0 +# + +import time +import pytest + +from testlib import * + +pd_cap = PDCapabilities([ + (Capability.OutputControl, 1, 1), + (Capability.LEDControl, 1, 1), + (Capability.AudibleControl, 1, 1), + (Capability.TextOutput, 1, 1), +]) + +pd_info = [ + PDInfo(101, scbk=KeyStore.gen_key(), name='chn-0'), +] + +# TODO remove this. +pd_addr = pd_info[0].address +pd = PeripheralDevice(pd_info[0], pd_cap, log_level=LogLevel.Debug) +cp = ControlPanel(pd_info) + +@pytest.fixture(scope='module', autouse=True) +def setup_test(): + pd.start() + cp.start() + cp.sc_wait_all() + yield + teardown_test() + +def teardown_test(): + cp.teardown() + pd.teardown() + +def test_cp_status(): + assert cp.online_wait(pd.address) + pd.stop() + assert cp.online_wait(pd.address) == False + pd.start() + assert cp.online_wait(pd.address) + +def test_cp_sc_status(): + assert cp.sc_wait(pd.address) + pd.stop() + assert cp.sc_wait(pd.address) == False + pd.start() + assert cp.sc_wait(pd.address) + +def test_pd_status(): + cp.stop() + time.sleep(1) + assert pd.is_online() == False + cp.start() + assert cp.sc_wait(pd.address) + assert pd.is_online() + +def test_pd_sc_status(): + cp.stop() + time.sleep(1) + assert pd.is_sc_active() == False + cp.start() + assert cp.sc_wait(pd.address) + assert pd.is_sc_active()