-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added automatic check of homework #369
Open
Acepresso
wants to merge
2
commits into
RedHat-Israel:master
Choose a base branch
from
Acepresso:check-homework
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
docs/course_materials/exercises/03_Variables_and_datatypes/test_homework.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
""" Automatically check student's homework | ||
|
||
The tests will check: | ||
1. The content of the exercise file | ||
2. The execution and output of the exercise file | ||
3. The output with different inputs | ||
|
||
Guidelines: | ||
- Student's exercise files should be placed in the same dir as this test. | ||
- Test names are in the form: test_<topic>_<exc>.py | ||
|
||
Usage: | ||
|
||
# List all tests | ||
pytest -o addopts="" --collect-only | ||
|
||
# Run all tests: | ||
pytest -o addopts="" | ||
|
||
# Test a specific topic | ||
pytest -o addopts="" -k test_variables | ||
""" | ||
import os | ||
import re | ||
from subprocess import PIPE, STDOUT, Popen, TimeoutExpired | ||
|
||
|
||
def test_variables_exc1(): | ||
answer_file = 'variables.py' | ||
expected_pycode = [ | ||
r'^.*x *= *9.*', | ||
r'^.*y *= *7.*', | ||
r'^.*z *= *x *\+ *y.*', | ||
r'^.*print.*z.*', | ||
] | ||
expected_stdout = '16' | ||
|
||
check_list_of_answers_from_file(expected_pycode, answer_file) | ||
|
||
stdout = run_cmd(['python3', answer_file]) | ||
check_list_of_answers([expected_stdout], stdout.strip(), word_pattern=True) | ||
|
||
|
||
def test_variables_exc2(): | ||
answer_file = 'calculations.txt' | ||
expected_answers = [ | ||
# What is the result of 10 ** 3? | ||
'1000', | ||
# Given (x = 1), what will be the value of after we run (x += 2)? | ||
'3', | ||
# What is the result of float(1)? | ||
r'1\.0', | ||
# What is the result of 10 == “10”? | ||
'False', | ||
# Print the result of the following variable: | ||
# Number = ((((13 * 8 - 4) * 2 + 50) * 4 ) % 127 ) *5 | ||
'555', | ||
] | ||
check_list_of_answers_from_file(expected_answers, answer_file, word_pattern=True) | ||
|
||
|
||
def test_variables_exc3(): | ||
# ??? TODO: ASK SHIRA file names | ||
# Accept two numbers from the user (using input) and calculate multiplication (*) | ||
# Accept two numbers from the user (using input) and calculate Subtract (-) | ||
# Accept two numbers from the user (using input) and calculate Divide (/) | ||
# Accept two numbers from the user (using input) and calculate Modulus (%) | ||
pass | ||
|
||
|
||
def test_strings_names(): | ||
student_file = 'Names.py' | ||
answers = [ | ||
# Create a string variable with your name, call it my_name | ||
r'''\bmy_name\s*=\s*['"]\w+["']''', | ||
# Create a string variable with your family name, call it my_family_name | ||
r'''\bmy_family_name\s*=\s*['"]\w+["']''', | ||
# Create a string variable called my_full_name which is composed from the 2 variables my_name and my_family_name. | ||
r'''\bmy_full_name\s*=.*\bmy_name\b.*\bmy_family_name\b''', | ||
# Create a variable with your city name: call it my_city_name | ||
r'''\bmy_city_name\s*=\s*['"]\w+['"]''', | ||
# Create a variable msg with "My name is X and I’m from Y" using the variables you created above | ||
r'''\bmsg\s*=.*My name is .* and I'm from .*''', | ||
r'''\bmsg\s*=.*\bmy_(full_)?name\b.*\bmy_city_name\b''' | ||
] | ||
check_list_of_answers_from_file(answers, student_file) | ||
|
||
|
||
def test_strings_times(): | ||
student_file = 'Times.py' | ||
expected_msg = 'You have to spend {} minutes this week to complete ROSE homework' | ||
|
||
does_student_file_exist(student_file) | ||
for inputs, expected_output in [ | ||
[['2', '3'], expected_msg.format(6)], | ||
[['11', '6'], expected_msg.format(66)], | ||
[['10', '2'], expected_msg.format(20)], | ||
]: | ||
assert run_cmd(['python3', student_file], input_list=inputs) == expected_output | ||
|
||
|
||
def run_cmd(cmd_list, input_list=[], **kwargs): | ||
input_data = '\n'.join(input_list).encode('utf-8') if input_list else None | ||
|
||
p = Popen(cmd_list, stdout=PIPE, stdin=PIPE, stderr=STDOUT) | ||
try: | ||
stdout, stderr = p.communicate(input=input_data, timeout=20, **kwargs) | ||
except TimeoutExpired: | ||
p.kill() | ||
stdout, stderr = p.communicate() | ||
assert p.returncode == 0 | ||
|
||
return stdout.decode('utf-8') | ||
|
||
|
||
def check_list_of_answers_from_file(expected_answers_list, answer_file, word_pattern=False): | ||
with open(answer_file, 'r') as f: | ||
text = f.read() | ||
check_list_of_answers(expected_answers_list, text, word_pattern) | ||
|
||
|
||
def check_list_of_answers(expected_answers_list, text, word_pattern=False): | ||
for line in text.splitlines(): | ||
# print(f'line: |{line}|') | ||
for answer in expected_answers_list.copy(): | ||
pattern = f'\\b{answer}\\b' if word_pattern else answer | ||
# print(f'pattern: {pattern}') | ||
if re.match(pattern, line): | ||
# print("MATCHED") | ||
expected_answers_list.remove(answer) | ||
|
||
assert len(expected_answers_list) == 0, "Some expected answers were not found" | ||
|
||
|
||
def does_student_file_exist(filename): | ||
assert os.path.exists(filename), f'Student homework file not found: {filename}' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not Ideal,
I would change it so I could give a file path to this test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Will add that.