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:
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.
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!
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
Then, write the function and Run Scratchpad it to see if it works as expected:
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!
Still have questions? Reach out to your teacher!