Unit Tests in CodeHS Courses

When working through our curriculum, students might come across exercises that have two files: a Unit Test file and a Scratchpad file. In these assignments, Unit Test is the file which is graded by the autograder or the teacher, and Scratchpad is ungraded. Here's an example from our Java course, 3.3.4 Double Number:

Code editor shows scratchpad file

What is a Unit Test?

A Unit Test is a type of exercise which is simpler than a full program and allows you to test a single concept much easier. CodeHS Practice Assignments are good examples of Unit Tests. Any type of assignment that is designated Practice, is technically a Unit Test.

Unit Tests have test cases which pass values through the student's function and compare these outputs to the outputs when these values are passed through the solution to the exercise. If the values don't match, the student will see a message showing that their function did not generate the expected result. When the student passes all test cases, they will turn green signaling that the student completed the assignment successfully!

Code editor shows error messages on right hand side

How to use the Scratchpad

The scratchpad is available to help students with debugging. The Scratchpad file is a special file that only exists for Unit Test programs. Students can experiment with the code they write here and use the console to test outputs before submitting their Unit Test solution.

A good way for students to use the Scratchpad is to test out different inputs to see if their code is working properly. It may work fine using positive integers, but what happens if you pass a negative integer? Or a zero? Let's go to the Scratchpad to find out!

Scratchpad Example:

We've written some code that could be a solution into Scratchpad.java for the assignment 3.3.4 Double Number and added some primitive tests. We hit Run Scratchpad to see if the code behaves how we expect!

Scratch pad has sample code using tests

Green Run Code button now say Run Scratchpad
Sample scratchpad out put in the Run Scratchpad Console

Our output looks like what we'd expect, so we can copy the code into the Unit Test file and submit our assignment confidently.

What else can I do?

You can create more robust tests with good names and messages using the Scratchpad. Here's an example of a more complex test for Double Number:

Shows sample tests running in the Run Scratchpad console

There's a lot that can be done in Scratchpad to test your Unit Tests.

Still have questions? Contact our team at hello@codehs.com to learn more!

Did this answer your question?