import os
import sys
import subprocess
def run_tests(directory):
test_files = sorted(
f for f in os.listdir(directory) if f.endswith(".py") and f != "tutorial_template.py"
)
for test_file in test_files:
print(f"Running {test_file}...", flush=True)
test_path = os.path.join(directory, test_file)
env = os.environ.copy()
env["PYTHONUNBUFFERED"] = "1"
process = subprocess.Popen(
[sys.executable, test_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
env=env,
)
for line in process.stdout:
print(line, end='', flush=True)
for line in process.stderr:
print(line, end='', flush=True)
process.wait()
if process.returncode != 0:
print(f"Error in {test_file}. Test failed.", flush=True)
print("--------------------------------------------------", flush=True)
return
else:
print("Test passed!", flush=True)
print("--------------------------------------------------", flush=True)
print("All tests completed successfully!", flush=True)
if __name__ == "__main__":
run_tests("../tutorials")