0

I have a simple Java console application and would like to test its input / output automatically. The input is always only one line, but the output is sometimes more than one line.

How can I do this? (with a Linux shell / Python / Eclipse / Java)

2
  • 1
    What do you mean by testing? Give us some example. Commented Feb 25, 2012 at 18:58
  • If the app is yours, you could (that is, should) separate the main() from the rest of the logic, so you can test your code with something like xUnit. Commented Feb 25, 2012 at 19:13

3 Answers 3

7

You could use pipes in Linux. For example, run your problem like this:

java myProgram < input_file > output_file

This will run myProgram and feed input from input_file. All output will be written to a file called output_file.

Now create another file called expected_file which you should handcreate to specify the exact output you expect on some input (specifically, the input you have in input_file).

Then you can use diff to compare the output_file and the expected_file:

diff output_file expected_file

This will output any differences between the two files. If there are no differences, nothing will be returned. Specifically, if something gets returned, your program does not work correctly (or your test is wrong).

The final step is to link all these commands in some scripting language like Ruby (:)) or Bash (:().

This is the most straight-forward way to do this sort of testing. If you need to write more tests, consider using some test frameworks like junit.

Sign up to request clarification or add additional context in comments.

1 Comment

You should note that the use of pipes is not specific to Linux. The same thing works on Windows and Mac OS X.
1

In eclipse you can log your console output to a physical file using the Run configuration settings. Run-> Run Configuration-> Select your application->go to common tab-> in 'Standard input and output' section specify physical file path.

Comments

0

You can execute any Unix command using watch command. Watch command will be executed until you terminate it either by CTRL+C or kill the process.

$ watch -n 5 ls

By default watch command uses 2 second interval, you can change it using -n option.

Or you could write a function like this in your .bashrc (from here)

function run() {
    number=$1
    shift
    for i in {1..$number}; do
      $@
    done
}

And use it like this

run 10 command

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.