Learn How to Auto-Grade
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.
The simplest TestCase subclass will simply implement a test method (i.e. a method whose name starts with test) in order to perform specific testing code:
1
def test_default_case(self):
2
# Your test case logic here (replace the example assertion below)
3
# You may also rename this to any function in the form of 'test_your_test_name(self):'
4
self.assertTrue(True)
Copied!
Note that in order to test something, we use one of the assert*() methods provided by the TestCase base class. If the test fails, an exception will be raised with an explanatory message, and unittest 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 setUp(), which the testing framework will automatically call for every single test we run:
1
def setUp(self):
2
# Setup code here (if required, replace the 'pass')
3
pass
Copied!
Similarly, we can provide a tearDown() method that tidies up after the test method has been run:
1
def tearDown(self):
2
# Teardown code here (if required, replace the 'pass')
3
pass
Copied!
If setUp() succeeded, tearDown() will be run whether the test method succeeded or not.
​

Java - JUnit

TODO
​
1
@Test
2
public void testDefaultCase() {
3
// You may rename this method to better suit the purpose of your test case
4
// Your test case logic here
5
}
Copied!
​
1
@Before
2
public void setUp() {
3
// Setup code here (if required)
4
}
Copied!
​
1
@After
2
public void tearDown() {
3
// Teardown code here (if required)
4
}
Copied!
​

Created and curated by Joe Mazzone

​Joe Mazzone, Computer and Software Engineering instructor at William M. Davies, Jr. Career and Technical High School in Lincoln, Rhode Island
Like what Joe created? β˜• Buy him a coffee: https://www.buymeacoffee.com/JoeMazzone​
​
Last modified 1mo ago