Repl unit testing allows a repl author to create code-driven tests that compare actual function output with expected output.
Unit testing is great for more complicated testing scenarios. For example, when you need to test that functions return specific values based on their (dynamic) inputs.
Each test is itself a function that follows a pattern:
Here's an example using Java:
// invoke with 3.0, 4.0 as input double area = calculateRectArea(3.0, 4.0); // compare expected to actual assertEqual(12.0, actual);
Unit testing is not ideal for testing that involves using Standard In (
System.in) and Standard Out (
System.out). Input/Output testing is ideal for testing that relies on precise usage of
The testing pane can be found in your left-hand sidebar in your repl. It is your hub for creating tests. Read on to find out more about how to use this helpful feature.
Open the testing pane within a project.
If prompted, select "Unit tests".
Write a function within the main file that's easy to test: something which accepts parameters and returns a single result. Our example includes an
add function which simply returns the result of adding two numbers.
Click "+ Add test".
Providing a test will construct a unit test function for you. Only the body of the function is editable. Configure the test to invoke the
add function and compare the result to the expected value. If the actual value does not match the expected value, the assert method with throw an exception and cause the test to fail. Include a helpful failure message to explain the intent of the test.
Note: Python exposes its assert methods on the
self object. This behavior will be different depending on the language you use. See the "Assertion documentation" below to read about the invocation patterns for each unit testing framework.
Click "Run tests" to begin executing your test suite. Open the Console tab to monitor execution progress.
Test results will appear in the Console.
Imports can be configured in the "Setup" for the test suite, which is helpful if:
For example, you can import NumPy for all tests. Keep in mind that this will affect all test functions within your test suite (you will not need to import more than once):
Here is an example function using NumPy:
Every other test you write can also use NumPy:
If you would like to test multiple modules or files within your repl, you must manually import them in the "Setup".
Sometimes tests require specific setup and teardown steps to configure and destroy global state.
Consider a repl that relies on Repl Database and loads specific data by key.
Use import to include Repl Database.
Use the setup to add a database key with test data:
Use the teardown to delete the test data:
Read about the supported assert function for each unit testing library:
If you are new to projects, you can find more info on creating a project here.