diff --git a/src/test_wrapper.py b/src/test_wrapper.py index 74afda3..aa713c5 100644 --- a/src/test_wrapper.py +++ b/src/test_wrapper.py @@ -3,7 +3,7 @@ from typing import Protocol from dotenv import dotenv_values from icecream import ic -from tests_wordpress.testrunner_wordpress import TestRunnerWordpress +from tests_wordpress.runner_wordpress import RunnerWordpress class TestRunner(Protocol): @@ -11,7 +11,7 @@ class TestRunner(Protocol): pass -WRAPPER_DICT: dict[str, type[TestRunner]] = {"wordpress": TestRunnerWordpress} +RUNNER_DICT: dict[str, type[TestRunner]] = {"wordpress": RunnerWordpress} class TestWrapper: @@ -32,9 +32,9 @@ class TestWrapper: def load_test_wrapper(self): config_type = self.config["TYPE"] ic(config_type) - WrapperClass = WRAPPER_DICT[config_type] - wrapper = WrapperClass(self.dotenv_path) - wrapper.run_tests() + RunnerClass = RUNNER_DICT[config_type] + runner = RunnerClass(self.dotenv_path) + runner.run_tests() if __name__ == "__main__": diff --git a/src/tests_wordpress/runner_wordpress.py b/src/tests_wordpress/runner_wordpress.py new file mode 100644 index 0000000..13b0f00 --- /dev/null +++ b/src/tests_wordpress/runner_wordpress.py @@ -0,0 +1,87 @@ +from pathlib import Path +from typing import Callable, Optional, TypedDict + +import pytest +from dotenv import dotenv_values +from icecream import ic + + +class SubTest(TypedDict): + condition: Callable[[Path], bool] + test_file: str + + +def condition_feature1(dotenv_path: Path) -> bool: + """returns true if feature 1 should be executed based on the dotenv file given as a Path""" + + +def condition_always_true(dotenv_path: Path) -> bool: + return True + + +class Runner: + main_test_name: Optional[str] = None + sub_tests: list[SubTest] = [] + + def __init__(self, dotenv_path: Path): + self.dotenv_path = dotenv_path + self.root_dir = self.dotenv_path.parent + self.show_files() + self.load_dotenv() + + def _run_main_test(self): + if isinstance(self.main_test_name, str): + self._run_pytest(self.main_test_name) + + def _run_pytest(self, test_name: str): + pytest.main( + [ + self.root_dir / "tests_wordpress" / test_name, + "--env_file_path", + self.dotenv_path, + ] + ) + + def run_tests(self): + self._run_main_test() + for c in self.sub_tests: + condition_function = c["condition"] + if condition_function(): + test_name = c["test_file"] + self._run_pytest(test_name) + + +class RunnerWordpress(Runner): + main_test_name = "test_wordpress.py" + sub_tests = [ + SubTest(condition=condition_always_true, test_file="test_wordpress_feature1.py") + ] + + def show_files(self): + ic(list(self.root_dir.glob("*"))) + + def load_dotenv(self): + assert self.dotenv_path.is_file() + config = dotenv_values(self.dotenv_path) + ic(config) + + def run_pytest_dir_wp(self): + pytest.main([self.root_dir / "tests_wordpress"]) + + def run_pytest_single_wp(self): + pytest.main( + [ + self.root_dir / "tests_wordpress" / "test_wordpress.py", + "--env_file_path", + self.dotenv_path, + ] + ) + + def run_pytest_single_wp_feature1(self): + pytest.main( + [ + self.root_dir / "tests_wordpress" / "test_wordpress_feature1.py", + "--env_file_path", + self.dotenv_path, + ] + ) diff --git a/src/tests_wordpress/testrunner_wordpress.py b/src/tests_wordpress/testrunner_wordpress.py deleted file mode 100644 index d375588..0000000 --- a/src/tests_wordpress/testrunner_wordpress.py +++ /dev/null @@ -1,45 +0,0 @@ -from pathlib import Path - -import pytest -from dotenv import dotenv_values -from icecream import ic - - -class TestRunnerWordpress: - def __init__(self, dotenv_path: Path): - self.dotenv_path = dotenv_path - self.show_files() - self.load_dotenv() - # self.run_pytest_dir_wp() - # self.run_pytest_single_wp() - self.run_pytest_single_wp_feature1() - - def show_files(self): - ic(list(self.root_dir.glob("*"))) - - def load_dotenv(self): - dotenv_path = self.root_dir / "./blog.dev.local-it.cloud.env" - assert dotenv_path.is_file() - config = dotenv_values(dotenv_path) - ic(config) - - def run_pytest_dir_wp(self): - pytest.main([self.root_dir / "tests_wordpress"]) - - def run_pytest_single_wp(self): - pytest.main( - [ - self.root_dir / "tests_wordpress" / "test_wordpress.py", - "--env_file_path", - self.dotenv_path, - ] - ) - - def run_pytest_single_wp_feature1(self): - pytest.main( - [ - self.root_dir / "tests_wordpress" / "test_wordpress_feature1.py", - "--env_file_path", - self.dotenv_path, - ] - )