Files and random numbers.

Re: Files and random numbers.

by Richard Lobb -
Number of replies: 0

The easiest ways to test a program with multiple different input files are:

  1. Have the program read from standard input and paste the different test data sets into the Input fields of the test cases in the author editing form. When CodeRunner runs questions that have non-empty input, it creates a data file for each of the input data sets and runs the program multiple times with standard input redirected to each of the data files in turn (equivalent to prog < data.txt at the command line)
  2. Get the students to read the name of the file to be processed from standard input, upload a set of data files to the Support Files, and in the various test cases set the Input for the test to be the name of one of the data files.
  3. Get the students to instead write a function that takes the name of the test file as a parameter.
If you've asked students to read from a filename like data.txt that is hard-coded into their program, you need to write your own template to set up the test data for each test case. For example, you might have support files test1.txt, test2.txt etc, and set the Extra field of each test case to the name of the file you wish to test with. The template code (which now needs to be a per-test-case template, not a combinator template) could then copy the required support file to data.txt, copy the students answer to a file prog.py (or whatever) and then compile (if necessary) and run the program with that data file. But that's far more complicated.