Skip to content

Instantly share code, notes, and snippets.

@samchouse
Last active September 27, 2024 12:26
Show Gist options
  • Select an option

  • Save samchouse/c3fbd6bbf3c0f12d0da48059c74d69e7 to your computer and use it in GitHub Desktop.

Select an option

Save samchouse/c3fbd6bbf3c0f12d0da48059c74d69e7 to your computer and use it in GitHub Desktop.
Grade calculation automated testing

Automated testing for Grade Calculation assignment

Made by me with test cases from Felix.

To run this script, follow these instructions:

  1. Create a file called test.py in the same directory as your calculation script and paste the contents of test.py from below.
  2. Create a file called tests.json in the same directory as your calculation script and paste the contents of tests.json from below.

Now to run the tests, go to the directory where all these files are located and open a terminal and run the following:

python tests.py <./path/to/script.py>
# or
python3 tests.py <./path/to/script.py>

For example, if my calculation script is named script.py I would run:

python tests.py script.py
import sys
import subprocess
import json
import re
args = sys.argv[1:]
if len(args) == 0:
print("Missing script argument")
sys.exit(1)
with open("tests.json", "r") as file:
cases = json.load(file)
for idx, case in enumerate(cases):
process = subprocess.run(
[sys.orig_argv[0], args[0]],
input="\n".join(case["inputs"]).encode(),
stdout=subprocess.PIPE,
)
grade = re.search(
r"\d+(\.\d+)?(?!.*\d)", process.stdout.decode().replace("\n", "")
).group(0)
if grade == case["output"]:
print(f"Passed test {idx + 1}/{len(cases)}")
else:
print(f"!!! Failed test {idx + 1}/{len(cases)}")
print(f"!!! Got: {grade}, Expected: {case['output']}")
if abs(float(grade) - float(case["output"])) <= 1:
print(
"!!! This may be a difference in rounding styles, please verify for yourself"
)
[
{
"inputs": [
"6",
"6",
"100",
"100",
"100",
"100",
"100",
"100",
"100",
"100"
],
"output": "100.0"
},
{
"inputs": ["3", "3", "50", "50", "50", "50", "50", "50", "50", "50"],
"output": "50.0"
},
{
"inputs": ["4", "3", "62", "54", "63", "66", "59", "62", "75", "79"],
"output": "64.0"
},
{
"inputs": ["3", "4", "89", "69", "50", "77", "88", "64", "77", "54"],
"output": "67.5"
},
{
"inputs": ["6", "6", "67", "90", "90", "51", "57", "87", "69", "79"],
"output": "82.08"
},
{
"inputs": ["8", "6", "67", "90", "90", "51", "57", "87", "69", "79"],
"output": "82.08"
},
{
"inputs": ["6", "8", "67", "90", "90", "51", "57", "87", "69", "79"],
"output": "82.08"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment