codeflash-internal/codeflash/verification/test_runner.py
2023-10-22 13:45:41 -07:00

35 lines
1.1 KiB
Python

import subprocess
def run_tests(
test_path,
test_framework: str,
cwd: str = None,
test_env=None,
pytest_timeout: int = None,
verbose: bool = False,
):
assert test_framework in ["pytest", "unittest"]
if test_framework == "pytest":
pytest_results = subprocess.run(
["pytest", test_path, "-q", f"--timeout={pytest_timeout}"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd,
env=test_env,
)
stdout = pytest_results.stdout.decode("utf-8")
stderr = pytest_results.stderr.decode("utf-8")
elif test_framework == "unittest":
unittest_results = subprocess.run(
["python", "-m", "unittest"] + (["-v"] if verbose else []) + [test_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd,
env=test_env,
)
stdout = unittest_results.stdout.decode("utf-8")
stderr = unittest_results.stderr.decode("utf-8")
else:
raise ValueError("Invalid test framework, we only support Pytest and Unittest currently.")
return stdout, stderr