Skip to content
Michael Murphey edited this page Jan 18, 2016 · 2 revisions

To create tests for more assignments:

Step 1:

Create a directory with the name of the lab such as sum_digits

Step 2:

Option 1: Sample Input and Output:

Create a plain text file named input.txt containing the text that will be used as input and another plain text file named output.txt containing the correct output. In the example, sum_digits, our input.txt file might contain:
1337
and our output.txt file might contain:
Enter a number :: 14
It's important to remember to include the prompt that the user would receive before entering his or her input.

Option 2: Custom Test:

Create a script named test.sh which takes the username of the user who's lab is being tested as an argument and tests to see if that user's lab is correct. In the case of the sum_digits lab, test.sh might contain:

#!/bin/bash

# first program argument is username

# add digits in a pseudorandom number
input=$RANDOM
number=$input
sum=0
echo $number
while [[ $number > 0 ]]; do
  sum=$(($sum+$number % 10))
  number=$(($number / 10))
done
echo "Correct sum is " $sum

# check if program's output contains the sum
echo $input > input.txt
output=$(python3 /home/$1/sum_digits.py < input.txt)
echo $output
if [[ ${output//$(($sum))} != "$output" ]]; then
  # output contained sum
  echo "Correct output"
else
  echo "Incorrect output"
fi
rm input.txt

Step 3:

That's it! If you used option 1, run the test with ./tester.sh -u student_name sample-output-directory assignment-file or ./tester.sh --all student_name sample-output-directory assignment-file. In this example, we will test the lab for all the students. In the case of the sum_digits lab, we would would use ./tester.sh --all sum_digits sum_digits.py. If you used option 2, leave out the name of the assignment file since that should be in the test.sh file. In the case of the sum_digits lab, we would use ./tester.sh --all sum_digits.