📋
Auto-Grading for Teachers
  • Learn How to Auto-Grade
  • Assertions and Unit Testing
  • Request / Submit Examples
  • 🐍Python Examples
    • Check Variable Values
    • Output Tests
    • Input / Output Tests
    • Print Function Tests
    • Return Function Tests
    • Random Number Tests
    • Parse Student Code
  • ☕Java Examples
    • Check Variable Values
    • Output Tests
    • Input / Output Tests
    • Return Method Tests
    • Class - .toString() and Void Method Tests
    • Random Numbers Tests
    • Array and ArrayList Tests
    • Parse Student Code
  • 💎Ruby Examples
    • Output Tests
    • Input / Output Tests
    • Print Function Tests
    • Return Function Tests
    • Parse Student Code
Powered by GitBook
On this page
  • Webinar
  • Quick Start
  • Python - unittest
  • Java - JUnit
  • Created and curated by Joe Mazzone

Was this helpful?

Export as PDF

Learn How to Auto-Grade

NextAssertions and Unit Testing

Last updated 2 years ago

Was this helpful?

Auto-grading is a powerful tool that saves teachers time and provides students with feedback regarding the correctness of their solution before they submit it for a grade.

All of this auto-grading magic can be accomplished with unit testing -- test whether parts (units) of a students code work properly by asserting that they meet certain conditions (equal to a predetermined value, produce a result in a given range, etc.).

Webinar

Quick Start

New to Unit Testing? Let's do a brief review.

TODO

Python - unittest

The basic building blocks of unit testing are test cases — single scenarios that must be set up and checked for correctness.

def test_default_case(self):
    # Your test case logic here (replace the example assertion below)
    # You may also rename this to any function in the form of 'test_your_test_name(self):'
    self.assertTrue(True)
def setUp(self):
    # Setup code here (if required, replace the 'pass')
    pass
def tearDown(self):
    # Teardown code here (if required, replace the 'pass')
    pass

Java - JUnit

TODO

@Test
public void testDefaultCase() {
    // You may rename this method to better suit the purpose of your test case
    // Your test case logic here
}

@Before
public void setUp() {
    // Setup code here (if required)
}

@After
public void tearDown() {
    // Teardown code here (if required)
}

Created and curated by Joe Mazzone

The simplest subclass will simply implement a test method (i.e. a method whose name starts with test) in order to perform specific testing code:

Note that in order to test something, we use one of the assert*() methods provided by the base class. If the test fails, an exception will be raised with an explanatory message, and will identify the test case as a failure. Any other exceptions will be treated as errors.

Tests can be numerous, and their set-up can be repetitive. Luckily, we can factor out set-up code by implementing a method called , which the testing framework will automatically call for every single test we run:

Similarly, we can provide a method that tidies up after the test method has been run:

If succeeded, will be run whether the test method succeeded or not.

, former Computer and Software Engineering instructor at in Lincoln, Rhode Island.

Joe is now a Senior Product Manager/Product Development Lead at John Wiley & Sons, Inc., working on and .

Like what Joe created? ☕ Buy him a coffee:

TestCase
TestCase
unittest
setUp()
tearDown()
setUp()
tearDown()
Joe Mazzone
William M. Davies, Jr. Career and Technical High School
Coding Rooms
zyBooks
https://www.buymeacoffee.com/JoeMazzone
Assertions and Unit Testing
Python Examples
Java Examples
Ruby Examples