add Runner class, test infrastructure

This commit is contained in:
Daniel 2023-11-21 21:34:06 +01:00
parent 85200a6322
commit 2d9ca21fec
3 changed files with 92 additions and 50 deletions

View file

@ -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__":

View file

@ -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,
]
)

View file

@ -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,
]
)