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, the Unit Test is the file that is graded by the autograder or the teacher, and Scratchpad is ungraded.
Here's an example from our Introduction to Java (Latte) course, 3.3.4 Double Number:
What is a Unit Test?
A Unit Test is a type of exercise that is simpler than a full program and allows you to isolate and test a single concept. CodeHS Practice Assignments are good examples of Unit Tests. Any type of assignment that is designated Practice is technically a Unit Test.
Unit tests check the outputs of the student's function against the expected outputs of the solution using test cases. If the values don't match, an error message will be displayed, indicating that the function did not produce the expected results. When all test cases pass, the test cases will all turn green, signaling that the student has successfully completed the assignment.
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!
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?
The Scratchpad can help you create more robust tests by allowing you to give your tests meaningful names and messages. This can make it easier to understand what each test is checking for and what the expected outcome should be.
Here's an example of a more complex test for Double Number:
There's a lot that can be done in Scratchpad to test your Unit Tests.
Troubleshooting: Tips and Tricks
When copying and pasting code from another source (like from the Sandbox):
When copying and pasting code from another source (like from the Sandbox):
Be sure to copy the code exactly. Depending on if you are pasting into the Scratchpad or the Unit Test, the code may need to be modified to work within the parameters of the Test Cases problem.
If the program will not run:
If the program will not run:
Check the Scratchpad and ensure it contains all necessary code elements. Remember, the green Run button will only run code contained in the Scratchpad file; it will not run code from the Unit Test file.
Make sure that you are defining the function and calling the function (See the example below.) Functions need to display or print information in order for you to see it.
Double-check your code! If the program is freezing, there may be an infinite loop.
If the program appears to freeze after clicking Test Cases, Check Code, or when trying to go to the next assignment:
If the program appears to freeze after clicking Test Cases, Check Code, or when trying to go to the next assignment:
Check the Unit Test file. If the assignment instructions do not specifically request user input, do not use user input when defining the function.
Make sure your function returns a value!
Still have questions? Contact our team at hello@codehs.com to learn more!