-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytest_generation.py
53 lines (48 loc) · 1.73 KB
/
pytest_generation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
from os.path import join, isfile
questions = [x for x in os.listdir() if not isfile(x) and "question" in x]
test_pattern = """{global_import}
{solution_import}
import pytest
@pytest.mark.timeout(5)
def test_{num}():
assert {action} == {result}
"""
id = 0
for question in questions:
test_path = join("base", question, "ans")
pairs = [
(join(test_path, x), join(test_path, x.replace("input", "output")))
for x in os.listdir(test_path)
if x.startswith("input")
]
for submission in os.listdir(question):
if isfile(join(question, submission)):
continue
global_import = ""
if os.path.exists(join(question, submission, "global.py")):
with open(join(question, submission, "global.py"), "r") as f:
global_import = f.read()
solution_import = "from {} import *".format(
[
x
for x in os.listdir(join(question, submission))
if x.startswith("wrong_")
][0][:-3]
)
for (input_file, output_file) in pairs:
with open(input_file, "r") as input, open(output_file) as output:
action = input.readlines()[0].strip()
result = output.readlines()[0].strip()
num = input_file.split("_")[-1][:-4]
test = test_pattern.format(
global_import=global_import,
solution_import=solution_import,
num=num,
action=action,
result=result,
)
with open(
join(question, submission, "test_{}.py".format(num)), "w"
) as f:
f.write(test)