What is a Unit Test?
A CodeHS Unit Test is designed to test one function at a time. Unit Tests are not full programs, so you can't print to the console or plug in numbers to test it out - you can only Check to see if your function passes all the test cases.
There are two files in this type of assignment: a Unit Test file and a Scratchpad file. A Unit Test requires a return
statement and does not ask for user input.
Check out our Interactive tutorial on using Unit Tests. This article will show you how to use the Scratchpad to make writing your Unit Tests easier.
In these program types, you can Run the Scratchpad file and Check the Unit Test file:
Basic Steps to Using the Scratchpad:
Copy & Paste any starter code from the Unit Test into your Scratchpad.
Code your function in the Scratchpad file. Because you can not Run the Unit Test file, it is much easier to write, test, and debug your function in the Scratchpad.
Copy & Paste your working function back into the Unit Test file to see if it passes all the test cases.
When it passes all the code checks, Submit your complete Unit Test for grading.
The Unit Test file:
is graded β
can not Run or print to the console π«
should contain a fully working function that works as expected π
β
What is the Scratchpad?
The Scratchpad is similar to how you would use scratch paper on a math or physics test. In the Scratchpad, you can work out the problem, plug in different inputs, print to the console, make mistakes, experiment, and debug.
Then, once your function works, you can copy and paste your final answer into the Unit Test file for your teacher to grade.
The Scratchpad file:
is not graded π«
can Run and print to the console β
allows you to plug in different inputs to test and/or debug your function π
A good way to use the Scratchpad is to test out different inputs to see if your 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!
β
Here's an example using Python, though this will be similar for JavaScript and Java.
This exercise is asking you to write a function to sum two numbers. If you try pressing Check Code, it fails all the test cases because the function doesn't do anything yet.
There is a skeleton of the function already in the Unit Test file's starter code - great!
Let's copy and paste the starter code into the Scratchpad:
Next, try writing a print
statement with a few example inputs (in this case, two integers to add together).
Then, write the function and Run Scratchpad to see if it works as expected:
Great! Our sum()
function sample output looks exactly as it should. You can now copy and paste it back into the Unit Test file to see if it passes the code checks:
It works! The function in the Unit Test file passes all the test cases in the code checker. Now the exercise is ready to Submit to your teacher for grading. Woohoo!
Troubleshooting: Tips and Tricks
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 won't 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.
If the program is freezing, there may be an infinite loop.
If the program appears to freeze after clicking Test Cases or Check Code, or when trying to go to the next assignment:
Check the Unit Test file.
If the assignment instructions don't specify using user input, do not use user input when defining your function.
Make sure the function returns a value.
Still have questions? Reach out to your teacher!