While working through CodeHS projects or practice problems, you might come across exercises that have two files: a Unit Test file and a Scratchpad file. 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.

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.

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 of code editor showing Unit Test not passing Test Cases

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 it 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!

Still have questions? Reach out to your teacher!

Did this answer your question?