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

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