Repl input/output testing allows a teacher to create simple tests that automatically match input values to expected output in student projects. Students can also easily test their code before submitting projects, which improves persistence. You can even use regular expressions (regex) for complex, flexible pattern matching.
There is also a video explanation available here.
Teams for Education
Creating a Project
If you are new to projects, you can find more info on creating a project here.
Once your project is created, you'll find the "Input/Output Tests" window by clicking in the left side bar.
Creating a Project with Input/Output Tests
Ideally, after creating your project, you should:
- Add instructions for the student.
- Write some skeleton code for the student.
- Configure the Input/Output tests (we will explain how shortly).
- Publish the project for students to start.
Let's look at the different testing options, each following the above sequence.
Let's say we have a test on string formatting in Python.
We have created a README.md file with the student's instructions.
We have created some skeleton code for the student to start from.
Now we can create the test.
Open the "Input/Output Tests" pane from the left sidebar and click on "+ Create test".
A modal window will pop up where you can configure the input and output of the test.
Above, we create the test with the following steps:
- Give the test a name.
- Leave the input blank as it's not needed for this test.
- Specify "John Smith" for the output.
- Select "match" for the test type.
We are selecting "match" for this test because we don't want an exact match. Students can write their own welcome message, however, the full name "John Smith" must be part of the output string.
Once the test is created, it'll be listed under "Input/Output Tests" and you can delete or modify it from there. To edit the test, click on the pencil icon next to the test name.
We now have a complete project for students to work on. Let's publish the project and look at the testing from the student's perspective.
Once published, the students will get a notification that a new project has been published. Clicking on the notification opens the team's projects page where they can find the new project. They will click on "Start project" to open it.
The student will be greeted with the project instructions
README.md file added earlier.
main.py file has the skeleton code we added, and they can start working from there.
Let's open the input/output tests and run the String-Formatting test as a student. It will fail now, because we haven't added any code.
The student can check the results to see what the expected output should be.
Let's add some code to make the test pass. We'll change the code to print the full name and then run the test again.
Checking the "passed" results now, you'll see that the expected output only has the full name "John Smith", whereas the actual output has a string with some other words in it. With match tests, this will pass because the expected output is present within the actual output. If the name was incorrectly printed ie. "Smith John", then this test would fail. If you want an exact match, you can use "exact" for the input/output test type.
expectedOutput === actualOutput || expectedOutput + '\n' === actualOutput.
Creating an exact test is similar to the match test created above.
As an example, we'll create an assignment where students have to write the formula to calculate the area of a circle using the
math module. To test that the student uses
math.pi instead of some variable like
pi=3.14, we will use the exact input/output test.
We have already created the skeleton code and README.md file, so now we'll create the test.
Above, we create an exact test that will check for exactly the areas specified within the expected output. Follow the below steps to create an exact test.
- Open the "Input/Output Tests" pane.
- Click on "+ Create test".
- Name the test.
- Add the exact expected output.
- Choose "exact" as the test type and click the save button.
You can now publish the project. Students will get a notification that the project is published.
From the students' perspective, they'll have the skeleton code, and the README.md file with instructions to complete the project.
If they run the test, it will fail because we haven't added any code to the skeleton yet. Students can check the expected output by checking the test results.
The student can then add their code to the main.py file with the skeleton code and run the test again to see if they passed.
Below we have code that uses the incorrect representation of
pi and because we are using exact tests, the test is failing.
Then, when we import the math module and use
math.pi, we get the correct answer that matches exactly with the expected output, so our test passes, and it is safe for the student to submit their code.
When a student submits a project without running the tests first, they will get a notification asking them to run tests first or submit anyway. This is a reminder for students to test their work before submitting as it will give them a good indication whether the work they did is correct.
When a student tries to submit a project while tests are failing, they will also get a notification making them aware of the fact, with an option to "View tests" or "Submit anyway".
For more flexibility in defining the expected output, you can use regular expressions or "regex". The regex test is the third type of input/output test.
The test passes if the test matches the expected output compiled as a regex. This is equivalent to
As an example, we have a project where the student has to write code that will compile email addresses from the given variables.
For the test, we'll set up a regex test to check that the student's email address matches the required email format.
To create the test seen above:
- Open the "Input/Output Tests" pane.
- Create a new test.
- Give the test a name.
- Add the regex expression to the "Expected output".
- Choose "regex" as the test type and click save.
When we add the code to compile the email address and run the test, we get the following results.
If you don't want to be lenient of an extra newline and prefer to have a truly exact match with the expected output and actual output, you can use the
regex with a
^ at the start and
$ at the end. Keep in mind though, that you'll have to escape the other regex characters.
Test cases can be added, edited, and deleted at any time – even after the project has been published. This added flexibility allows you to get started with testing right away.
While I/O tests work with most languages on Replit, there are just a few exceptions:
- HTML, CSS, JS
- Python with turtle