Skip to content

Commit

Permalink
feat: support concurrently executing tests (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreoxmt authored Dec 4, 2023
1 parent 4a231f3 commit 55a2d02
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os
import time
import tomllib

from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from typing import Dict, List

from tqdm import tqdm

from test_util import DocSitePreviewTest

ENV_FILE: str = ".env"
Expand Down Expand Up @@ -39,9 +37,7 @@ class TestConfig:
class TestRunner:
def __init__(self):
self.tests = self._load_config()
self.report = TestReport(
start_time=time.time(), end_time=time.time(),
success_tests=[], failed_tests=[])
self.report = TestReport(start_time=time.time(), end_time=time.time(), success_tests=[], failed_tests=[])
self._env = self._load_env()

@staticmethod
Expand All @@ -54,11 +50,8 @@ def _load_config() -> List[TestConfig]:
config = []
for _, test in data.items():
test_cases = [TestCase(**case) for case in test["test_cases"]]
config.append(TestConfig(
diff_command=test["diff_command"],
test_target=test["test_target"],
test_dependencies=test.get("test_dependencies"),
test_cases=test_cases))
config.append(TestConfig(diff_command=test["diff_command"], test_target=test["test_target"],
test_dependencies=test.get("test_dependencies"), test_cases=test_cases))
return config

@staticmethod
Expand All @@ -73,29 +66,36 @@ def _load_env() -> Dict[str, str]:
env[key] = value
return env

def run_test_case(self, config, case) -> None:
script_name = config.test_target
diff_command = config.diff_command
case_name = case.name
feature_dir = os.path.dirname(case_name)
test_dir = os.path.abspath(case.directory)
script_args = case.args

test = DocSitePreviewTest(test_dir, feature_dir, script_name, config.test_dependencies)
print(f"Running Test {case_name}...")
if test.execute(args=script_args, env=self._env) and test.verify(diff_command):
self.report.success_tests.append(case_name)
else:
self.report.failed_tests.append(case_name)

def run(self) -> None:
"""
Run test cases based on given configuration and environment variables.
"""
print(f"Running Tests...")

for config in self.tests:
script_name = config.test_target
diff_command = config.diff_command

for case in tqdm(config.test_cases):
case_name = case.name
feature_dir = os.path.dirname(case_name)
test_dir = os.path.abspath(case.directory)
script_args = case.args

test = DocSitePreviewTest(test_dir, feature_dir, script_name, config.test_dependencies)

if test.execute(args=script_args, env=self._env) and test.verify(diff_command):
self.report.success_tests.append(case_name)
else:
self.report.failed_tests.append(case_name)

with ThreadPoolExecutor() as executor:
futures = []
for config in self.tests:
for case in config.test_cases:
future = executor.submit(self.run_test_case, config, case)
futures.append(future)
# Wait for all futures to complete
for future in futures:
future.result()
self.report.end_time = time.time()

def analyze(self) -> str:
Expand Down

0 comments on commit 55a2d02

Please sign in to comment.