All Collections
Resources for Students
Using the CodeHS Code Editor
For Students: Using the Scratchpad and Unit Test Files
For Students: Using the Scratchpad and Unit Test Files

How to use the Scratchpad to help you write Unit Test files

Lea Sloan avatar
Written by Lea Sloan
Updated over a week ago

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:

Image highlighting scratchpad and Unit Test files and options to Run Scratchpad and check code


Basic Steps to Using the Scratchpad:

  1. Copy & Paste any starter code from the Unit Test into your Scratchpad.

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

  3. Copy & Paste your working function back into the Unit Test file to see if it passes all the test cases.

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

Image showing code not passing checks

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:

Gif showing user copying and pasting code from Unit Test into scratchpad file

Next, try writing a print statement with a few example inputs (in this case, two integers to add together).

Image of adding code into scratchpad file

Then, write the function and Run Scratchpad to see if it works as expected:

Image of code in scratchpad file being run

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:

Gif showing user copying the defined function from the scratchpad to the Unit Test

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.

Image showing defining and calling a function in the Scratchpad file.

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!

Did this answer your question?