Skip to main content

Testing framework for Python students code.

Project description

Python Student Testing Framework

Contents

Introduction

Welcome to the Python Student Testing Framework! This README provides a brief overview of what the framework is and how to use it. The framework is designed for instructors and TAs to easily test and grade students Python code.

Features

  • Automated testing of students' code against a set of test cases
  • Support for multiple test cases and multiple functions
  • Ability to define custom tests and assertions
  • Clear and detailed feedback for students on test results
  • Display HTML report

Installation

To use the Python Student Testing Framework, you will need to have Python installed on your machine. Then, simply clone the repository or install with pip install school-grader

Contributing

The Python Student Testing Framework is an open-source project and contributions are welcome.

License

The Python Student Testing Framework is released under the MIT license. See the LICENSE file for more information.

Contact

If you have any questions or issues, please create a GitHub issue or reach out to the project maintainers at marcolivier.derouin@poulet-frit.com

How to use

Let's say you have given an assignment to your students.

assignment1.py

words = input("Please enter a list of words separated by spaces: ").split(" ")

palindrome_words = []
non_palindrome_words = []

for word in words:
    lowercase_word = word.lower()
    if lowercase_word == lowercase_word[::-1]:
        palindrome_words.append(word)
    else:
        non_palindrome_words.append(word)

print("Palindrome words:" + " ".join(palindrome_words))
print("Non-palindrome words:" + " ".join(non_palindrome_words))

You can write test case for this assignment using the following code.

from school_grader import FileTestCase, run_tests
FileTestCase('Test #1', # Test name
             'assignment1', # File name
             ['Palindrome words:kayak', 'Non-palindrome words:hi bonjour'], # Expected output, in a list of strings, each element is a printed line
             ['kayak hi bonjour'], # Mock input, in a list of strings, each element is a input line
            )

FileTestCase('Test #2',
             'assignment1',
             ['Palindrome words:level racecar madam level', 'Non-palindrome words:'],
             ['level racecar madam level'], # This parameter is optional, if not provided, the test will input nothing
            )

FileTestCase('Test #3',
             'assignment1',
             ['Palindrome words:', 'Non-palindrome words:hello world goodbye'],
             ['hello world goodbye'],
             timeout=4, # This parameter is optional, if not provided, the test will timeout after 1 second
            )

FileTestCase('Test #4',
             'assignment1',
             ['Palindrome words:KAyak', 'Non-palindrome words:hello world goodbye'],
             ['hello world goodbye KAyak'],
             fail_message='Did you think about case sensitivity?', # This parameter is optional, if not provided, the test will fail with a default message
            )

run_tests()

This will generate an HTML report that will automatically open in the browser.

HTML report

If there is an error in the student code, the report will look like this.

HTML report

Expected outputs

The expected_output list in FileTestCase can also contains an Equality class object in order to help with string comparison

Almost equal

  • AlmostEqualString: This class is used for almost equal string validation:

    • expected: The expected string value for comparison.
    • max_distance: The maximum Levenshtein distance allowed between the expected value and the value to test.
  • AlmostEqualNumber: This class is used for almost equal numerical validation:

    • expected: The expected string value for comparison.
    • precision: The number of decimal places to compare for floating-point numbers.

Equal

  • CaseInsensitiveStringEquality: This class is used for case-insensitive string equality validation:

    • expected: The expected string value for comparison.
  • WhiteSpaceInsensitiveEquality: This class is used for space-insensitive equality validation:

    • expected: The expected string value for comparison.
  • ContainsEquality: This class is used for contains equality validation:

    • expected: The expected string value to check if it is contained in the value to test.

Combinations of Equal classes

  • CombineEqualities(): This function combines multiple instances of Equal subclasses and returns a new subclass that combines all the provided equalities.

Here is an example using CaseInsentiveStringEquality

from school_grader import FileTestCase, run_tests, CaseInsensitiveStringEquality
FileTestCase('Test #1', # Test name
             'assignment1', # File name
             [CaseInsensitiveStringEquality('palindrome words:kayak'), CaseInsensitiveStringEquality('non-palindrome words:hi bonjour')], # Expected output
             ['kayak hi bonjour'], # Mock input, in a list of strings, each element is a input line
            )

run_tests() # Run all tests

Here is an example combining CaseInsentiveStringEquality and WhiteSpaceInsensitiveEquality

from school_grader import FileTestCase, run_tests, CaseInsensitiveStringEquality, WhiteSpaceInsensitiveEquality, CombineEqualities

Insentive = CombineEqualities(CaseInsensitiveStringEquality, WhiteSpaceInsensitiveEquality)

FileTestCase('Test #1', # Test name
             'assignment1', # File name
             [Insentive('palindromewords:kayak'), Insentive('non-palindromewords:hibonjour')], # Expected output
             ['kayak hi bonjour'], # Mock input, in a list of strings, each element is a input line
            )

run_tests() # Run all tests

Extension for Visual Studio Code

You can download a Visual Studio Code extension that will provide coloring when running tests.

print('Hi')

You can run the tests with these buttons.

Buttons

The result will look like this. When you hover on a test, you will see the Stack trace.

Result

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

school_grader-4.0.10.tar.gz (55.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

school_grader-4.0.10-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file school_grader-4.0.10.tar.gz.

File metadata

  • Download URL: school_grader-4.0.10.tar.gz
  • Upload date:
  • Size: 55.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for school_grader-4.0.10.tar.gz
Algorithm Hash digest
SHA256 c98ec6afab028d397b2ffd324e32f4081b9c49e03e2a5a19461a778ca8008a97
MD5 0d1fe93a762c05c7c9f24ebc4c94cc57
BLAKE2b-256 7ba1ea7b48f76a86ef492edc7febae8488c9527bfe52bce993270df18b57facc

See more details on using hashes here.

File details

Details for the file school_grader-4.0.10-py3-none-any.whl.

File metadata

  • Download URL: school_grader-4.0.10-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for school_grader-4.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 fc3ff62ad061c2fcfe9b2b152e876578d0dac81d236206ed147548b82ce3cbdb
MD5 88a28c88d455b577450e9b9ef3dfbc30
BLAKE2b-256 62ff0afe50795b33e0f3cfb4e023b780ae0c0eafbca885535a7fdeddb022d2ea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page