User Tools

Site Tools


junit-test-example

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
junit-test-example [2008/10/12 07:37] jtkorbjunit-test-example [2008/10/25 08:58] (current) jtkorb
Line 1: Line 1:
 +**Updates**
 +  * Project 3 neither reads from standard input nor writes gradeable text to standard output, so the mechanism below to redirect these two streams to files is not necessary for this project.  Instead, your JUnit test routines can just call ''main'' directly with different versions of the ''String[] args'' array to indicate different input test files.
 +  * For command-line testing in the UNIX/Linux environment, see the new section [[#Command-Line JUnit Testing in UNIX|below]].
 +
 ====== Testing Your Project ====== ====== Testing Your Project ======
  
 //Test early, test often.// //Test early, test often.//
- 
-As the project specifications point out, it is difficult to redirect standard input from within DrJava, forcing you to constantly flip back and forth between the IDE and the command line.  Here's an approach that gets around this problem and also makes it easy for you to add additional tests as you develop your code. 
  
 Use a [[http://www.junit.org|JUnit]] test case.  JUnit is a Java package that facilitates "unit testing": creation, execution, and evaluation of repeatable tests on your code.  It is designed to support incremental testing ([[http://en.wikipedia.org/wiki/Test-driven_development|test-driven development]]), but can also be used in a variety of ways including "whole program" testing.   Use a [[http://www.junit.org|JUnit]] test case.  JUnit is a Java package that facilitates "unit testing": creation, execution, and evaluation of repeatable tests on your code.  It is designed to support incremental testing ([[http://en.wikipedia.org/wiki/Test-driven_development|test-driven development]]), but can also be used in a variety of ways including "whole program" testing.  
Line 9: Line 11:
 Both DrJava and Eclipse include JUnit support.  ([[http://www.drjava.org/userdocs/junit.html|Chapter 7]] in the DrJava help documentation has details.) Both DrJava and Eclipse include JUnit support.  ([[http://www.drjava.org/userdocs/junit.html|Chapter 7]] in the DrJava help documentation has details.)
  
-To use JUnit to test your main method, you need to make a minor structural change.  The idea is this: Use the main method to gather parameters and setup input and output streams, then call a second main method, ''mainWork(...)'', to do the actual work of your program.  This arrangement allows you to programmatically set up for and call ''mainWork(...)'' from your JUnit test class.+ 
 +====== Testing Programs that Read from Standard In and Write to Standard Out ====== 
 + 
 +For projects that take input from the command line (or redirected from a file) and produce gradable results to standard out, automated testing requires that these streams be redirected in the JUnit test class.  This section explains how to do that. 
 + 
 +To use JUnit to test your main method with different input streams, you need to make a minor structural change.  The idea is this: Use the main method to gather parameters and setup input and output streams, then call a second main method, ''mainWork(...)'', to do the actual work of your program.  This arrangement allows you to programmatically set up for and call ''mainWork(...)'' from your JUnit test class.
  
 Your ''MainClass'' methods looks like this... Your ''MainClass'' methods looks like this...
Line 58: Line 65:
  
  
 +====== Command-Line JUnit Testing in UNIX ======
  
- +Here is a minimalist way to do command-line JUnit testing in the UNIX/Linux environment.
-====== Command Line Testing on UNIX ====== +
- +
-Here is a minimalist way to do command line JUnit testing in the UNIX/Linux environment.+
  
   - Download the latest JUnit jar file from http://www.junit.org (as of this writing, ''junit-4.5.jar'').   - Download the latest JUnit jar file from http://www.junit.org (as of this writing, ''junit-4.5.jar'').
   - Put this jar file into the same directory as your test class and other project files.   - Put this jar file into the same directory as your test class and other project files.
-  - Compile your test class, adding junit-4.5.jar to your classpath (either using the ''-cp'' option to ''javac'' or with the sample script below.)+  - Compile your test class, adding junit-4.5.jar to your classpath (either using the ''-cp'' option to ''javac'' or with the sample script below).
   - Run your test class using the text-based JUnit runner, ''junit.textui.TestRunner''.   - Run your test class using the text-based JUnit runner, ''junit.textui.TestRunner''.
  
-Here is a script, named ''tester'', that compiles and runs a test class given as the command line argument.+Here is a script, named ''javat'', that compiles and runs a test class given as the command line argument.
  
   #!/bin/sh   #!/bin/sh
Line 78: Line 83:
 If your test class is named, say, ''SpellCheckerTester'', you would type... If your test class is named, say, ''SpellCheckerTester'', you would type...
  
-  $ tester SpellCheckerTester+  $ ./tester SpellCheckerTester
  
 +For more general-purpose use, put the ''jar'' file in your ''~/lib'' directory, add the correct ''CLASSPATH'' variable to your startup file (e.g., to ''~/.bashrc'') or edit it into the tester script, and move the ''tester'' script to your personal ''~/bin'' directory (adding that directory to your ''PATH'', if necessary).
junit-test-example.1223822233.txt.gz · Last modified: 2008/10/12 07:37 by jtkorb