From 55a2d0206ba5ca6557734d6cd5bdb5193a52e04b Mon Sep 17 00:00:00 2001 From: Aolin Date: Mon, 4 Dec 2023 11:49:57 +0800 Subject: [PATCH] feat: support concurrently executing tests (#25) --- test/test.py | 56 ++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/test/test.py b/test/test.py index 43ac5ff0..dd4a63b7 100644 --- a/test/test.py +++ b/test/test.py @@ -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" @@ -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 @@ -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 @@ -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: