Skip to content

Instantly share code, notes, and snippets.

@seefalert
Last active September 30, 2024 08:28
Show Gist options
  • Save seefalert/d32adc794a8a7f4bf9a2702d6acf4b96 to your computer and use it in GitHub Desktop.
Save seefalert/d32adc794a8a7f4bf9a2702d6acf4b96 to your computer and use it in GitHub Desktop.
Тестовый скрипт для проверки вашего кода. Поместите тестовые файлы в папку "tests", а ваш код в файл "my_code.py". Запустите скрипт для выполнения тестов и проверки результатов.
import os
import pathlib
import subprocess
import sys
import time
def load_test_file(file_path):
with open(file_path, encoding='utf-8') as file:
return file.read().strip().splitlines()
DIR = pathlib.Path(__file__).parent.resolve()
tests = os.path.join(DIR, 'tests')
try:
n_tests = len(os.listdir(tests)) // 2
open('my_code.py', encoding='utf-8')
except FileNotFoundError:
print('-' * 69, '\nОШИБКА 404')
print('Папка с тестами должна называться - tests, а файл с кодом - my_code.py', '-' * 69, sep='\n')
raise
python_version = 'python3' if sys.platform in {'linux', 'linux2', 'darwin'} else 'py'
for i in range(1, n_tests + 1):
start_time = time.perf_counter()
with open(os.path.join(tests, str(i)), encoding='utf-8') as test_file, open(os.path.join(tests, f'{i}.clue'),
encoding='utf-8') as clue_file:
my_code = open('my_code.py', encoding='utf-8').read().strip()
code = test_file.read().strip()
combined_code = my_code + '\n' + code
result_bytes = subprocess.run([python_version, "-c", combined_code], capture_output=True, text=True,
input=combined_code, encoding='utf-8')
result = result_bytes.stdout.strip()
correct = clue_file.read().strip()
assert result == correct, f"Test#{i}\n{'-' * 69}\nexpect:{repr(correct)}\nresult:{repr(result)}\n"
end_time = time.perf_counter()
print(f'Тест №{i} пройден(✓), время выполнения: {end_time - start_time:.2f} секунд')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment