# 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.&#x20;

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.).

{% content-ref url="assertions-and-unit-testing" %}
[assertions-and-unit-testing](https://auto-grade.joemazzone.net/assertions-and-unit-testing)
{% endcontent-ref %}

{% content-ref url="python" %}
[python](https://auto-grade.joemazzone.net/python)
{% endcontent-ref %}

{% content-ref url="java" %}
[java](https://auto-grade.joemazzone.net/java)
{% endcontent-ref %}

{% content-ref url="ruby-examples" %}
[ruby-examples](https://auto-grade.joemazzone.net/ruby-examples)
{% endcontent-ref %}

## Webinar

{% embed url="<https://www.youtube.com/watch?v=IVV_Wm9nRVo>" %}

{% embed url="<https://www.youtube.com/watch?v=782_j-BSG3g>" %}

{% embed url="<https://www.youtube.com/watch?v=M2PwwtDCBC8>" %}

{% embed url="<https://www.youtube.com/watch?v=m43PxYYgZ6g>" %}

## Quick Start

New to Unit Testing?  Let's do a brief review.&#x20;

TODO

### Python - unittest

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

The simplest [`TestCase`](https://docs.python.org/3/library/unittest.html#unittest.TestCase) subclass will simply implement a test method (i.e. a method whose name starts with `test`) in order to perform specific testing code:

```python
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)
```

Note that in order to test something, we use one of the `assert*()` methods provided by the [`TestCase`](https://docs.python.org/3/library/unittest.html#unittest.TestCase) base class. If the test fails, an exception will be raised with an explanatory message, and [`unittest`](https://docs.python.org/3/library/unittest.html#module-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()`](https://docs.python.org/3/library/unittest.html#unittest.TestCase.setUp), which the testing framework will automatically call for every single test we run:

```python
def setUp(self):
    # Setup code here (if required, replace the 'pass')
    pass
```

&#x20;Similarly, we can provide a [`tearDown()`](https://docs.python.org/3/library/unittest.html#unittest.TestCase.tearDown) method that tidies up after the test method has been run:

```python
def tearDown(self):
    # Teardown code here (if required, replace the 'pass')
    pass
```

&#x20;If [`setUp()`](https://docs.python.org/3/library/unittest.html#unittest.TestCase.setUp) succeeded, [`tearDown()`](https://docs.python.org/3/library/unittest.html#unittest.TestCase.tearDown) will be run whether the test method succeeded or not.

### Java - JUnit

TODO

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

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

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

## Created and curated by Joe Mazzone

[Joe Mazzone](https://joemazzone.net/), former Computer and Software Engineering instructor at [William M. Davies, Jr. Career and Technical High School](https://www.daviestech.org/) in Lincoln, Rhode Island.

Joe is now a Senior Product Manager/Product Development Lead at John Wiley & Sons, Inc., working on [Coding Rooms](https://www.codingrooms.com/) and [zyBooks](https://www.zybooks.com/).

Like what Joe created?  ☕ Buy him a coffee: <https://www.buymeacoffee.com/JoeMazzone>
