From 4f1df7ac428bbe2de7917872fd6441410102a011 Mon Sep 17 00:00:00 2001 From: kdmukai Date: Sun, 5 Mar 2023 13:06:41 -0600 Subject: [PATCH] Slight BaseTest reorg; more comments --- tests/base.py | 37 +++++++++++++++++++++++++++++++------ tests/test_controller.py | 2 +- tests/test_flows_seed.py | 4 ++-- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/tests/base.py b/tests/base.py index 0a08823a..0010cef7 100644 --- a/tests/base.py +++ b/tests/base.py @@ -30,6 +30,17 @@ class BaseTest: @classmethod def teardown_class(cls): + pass + + + @classmethod + def reset_settings(cls): + """ Wipe and re-initialize the Settings singleton """ + Settings._instance = None + + + @classmethod + def remove_settings(cls): """ If settings were written to disk, delete """ import os try: @@ -38,21 +49,29 @@ class BaseTest: print(f"{Settings.SETTINGS_FILENAME} not found to be removed") - def reset_controller(self): - """ Wipe and re-initialize the Controller singleton for each test run """ + @classmethod + def reset_controller(cls): + """ Wipe and re-initialize the Controller singleton """ Controller._instance = None Controller.configure_instance() def setup_method(self): - self.reset_controller() + """ Guarantee a clean/default Controller and Settings state for each test case """ + BaseTest.reset_controller() + BaseTest.reset_settings() self.controller = Controller.get_instance() self.settings = Settings.get_instance() + + + def teardown_method(self): + BaseTest.remove_settings() @dataclass class FlowStep: + """ Trivial helper class to express FlowTest sequences below """ expected_view: Type[View] = None run_before: Callable = None screen_return_value: Union[int, str] = None @@ -60,11 +79,17 @@ class FlowStep: class FlowTest(BaseTest): + """ Base class for any tests that do flow-based testing """ + def run_sequence(self, initial_destination: Destination, sequence: List[FlowStep]) -> Destination: """ - Runs the given sequence from the initial_destination, mocking out the associated - Screen and the scenario's return_value, receiving the resulting new Destination, - and repeating until the sequence is complete. + Runs the given sequence of FlowSteps starting from the initial_destination: + * verifies that we landed on the expected_view (if provided). + * mocks out the `View.run_screen()` to prevent the associated Screen class from instantiating. + * patches in the FlowStep's screen_return_value (as if it came from user interaction). + * optional `run_before` method modifies the View when necessary to be compatible w/test suite limitations. + * Runs the View and receives the resulting next Destination. + * then repeats the process on the next Destination until the sequence is complete. Returns the final Destination. """ diff --git a/tests/test_controller.py b/tests/test_controller.py index fb76b533..df6c37ec 100644 --- a/tests/test_controller.py +++ b/tests/test_controller.py @@ -15,7 +15,7 @@ class TestController(BaseTest): controller = Controller.get_instance() controller.address_explorer_data = "foo" - self.reset_controller() + BaseTest.reset_controller() controller = Controller.get_instance() assert controller.address_explorer_data is None diff --git a/tests/test_flows_seed.py b/tests/test_flows_seed.py index 220e384b..942c9710 100644 --- a/tests/test_flows_seed.py +++ b/tests/test_flows_seed.py @@ -1,6 +1,6 @@ # Must import this before the Controller -from base import FlowTest, FlowStep +from base import BaseTest, FlowTest, FlowStep from seedsigner.views.view import Destination, MainMenuView from seedsigner.views import seed_views, scan_views @@ -83,7 +83,7 @@ class TestSeedFlows(FlowTest): # Test data from iancoleman.io; 12- and 24-word mnemonic test_with_mnemonic("tone flat shed cool census soul paddle boy flight fantasy stem social".split()) - self.reset_controller() + BaseTest.reset_controller() test_with_mnemonic("cotton artefact spy mind wing there echo steak child oak awful host despair online bicycle divorce middle firm diamond rare execute chimney almost hollow".split())