JMU JMU - Department of Computer Science
Help Tools
Character-by-Character Testing


When testing console-based applications (i.e., applications that read their input from and write their output to) the console (i.e., System.in and System.out) it is almost impossible to verify the output "by eye". Instead, one should redirect input and output, and then use a diff utility (like diff, Meld, or Kdiff3).

1 Redirecting Input and Output

In principle, one could just redirect output and provide input from the keyboard, however, this is a little awkward because the prompts won't be visible. Hence, it is much more common to redirect both input and output.

For example, suppose the application is named Calculator, the input file is named test.in (for "input"), and you wan the output file to be named test.act (for "actual"). From the command line you would enter:

  java Calculator < test.in > test.out
  

2 Comparing the Expected and Actual Output

If you are lucky enough to have a file containing the expected (i.e., correct) output, you can then compare the expected output and the actual output character-by-character.

The classic command-line utility that provides this functionality is diff, however there many GUI tools that also provide this functionality (e.g., Meld and Kdiff3).

Continuing with the example above, suppose the expected output file is named test.exp (for "expected"), then you can compare the two using diff as follows:

  diff test.exp test.act
  

(You can do the same thing using Meld and Kdiff3, or you can load the files from the GUI.)

Copyright 2019