diff --git a/src/runner.py b/src/runner.py new file mode 100644 index 0000000..661db54 --- /dev/null +++ b/src/runner.py @@ -0,0 +1,45 @@ +from pathlib import Path +from typing import Callable, Optional, TypedDict + +import pytest +from icecream import ic + + +class SubTest(TypedDict): + condition: Callable[[Path], bool] + test_file: str + + +class Runner: + test_dir_name: Optional[Path] = None + main_test_name: Optional[str] = None + sub_tests: list[SubTest] = [] + + def __init__(self, dotenv_path: Path): + assert self.test_dir_name is not None + self.root_dir = Path(__file__).parent + self.dotenv_path = dotenv_path + + 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 / self.test_dir_name / test_name, + "--env_file_path", + self.dotenv_path, + ] + ) + + def show_files(self): + ic(list(self.root_dir.glob("*"))) + + def run_tests(self): + self._run_main_test() + for sub_test in self.sub_tests: + condition_function = sub_test["condition"] + if condition_function(self.dotenv_path): + test_name = sub_test["test_file"] + self._run_pytest(test_name) diff --git a/src/tests_wordpress/runner_wordpress.py b/src/tests_wordpress/runner_wordpress.py index b15db2a..75dc6b1 100644 --- a/src/tests_wordpress/runner_wordpress.py +++ b/src/tests_wordpress/runner_wordpress.py @@ -1,14 +1,6 @@ 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 +from runner import Runner, SubTest def condition_feature1(dotenv_path: Path) -> bool: @@ -19,43 +11,8 @@ def condition_always_true(dotenv_path: Path) -> bool: return True -class Runner: - test_dir: Optional[str] = None - main_test_name: Optional[str] = None - sub_tests: list[SubTest] = [] - - def __init__(self, dotenv_path: Path): - assert self.test_dir is not None - self.dotenv_path = dotenv_path - # self.root_dir = self.dotenv_path.parent - - 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 / self.test_dir / test_name, - "--env_file_path", - self.dotenv_path, - ] - ) - - def show_files(self): - ic(list(self.root_dir.glob("*"))) - - def run_tests(self): - self._run_main_test() - for sub_test in self.sub_tests: - condition_function = sub_test["condition"] - if condition_function(): - test_name = sub_test["test_file"] - self._run_pytest(test_name) - - class RunnerWordpress(Runner): - test_dir = "tests_wordpress" + test_dir_name = "tests_wordpress" main_test_name = "test_wordpress.py" sub_tests = [ SubTest(condition=condition_always_true, test_file="test_wordpress_feature1.py")