|
From: | Sriram Karra |
Subject: | Re: [DotGNU]pnetlib::csunit |
Date: | 12 Apr 2002 18:48:50 +0530 |
User-agent: | Gnus/5.09 (Gnus v5.9.0) Emacs/21.1 |
Rhys Weatherley <address@hidden> writes: > S11001001 wrote: > > > Could Rhys or someone please post a quick HOWTO for writing > > pnetlib unit tests? > > The "csunit" framework is a stripped-down version of JUnit/NUnit, > designed for automated tests within an automake framework, but not > much else. I have not really looked at csunit... But if it is really very similar to JUnit, maybe someone can make some simple mods to the following document: a for-dummies' like howto on JUnit I wrote a little while back. I did that because I wanted something smaller / simpler than what was available on junit.org
Date: March 2,
2002
JUnit For Dummies
This document is a brief introduction to Junit, a “Unit
Tester” for Java programs, written in Java. About JUnit
JUnit is an open source tool that can be
used to perform “Unit Testing” of Java programs. The latest version, as of this writing, is JUnit 3.7. It can be downloaded from:
http://prdownloads.sourceforge.net/junit/junit3.7.zip
What is Unit Testing? A “Unit Test” is a simple piece of test code that tests one
teeny-weeny bit of your program. A Unit
Test does not test application level
functionality. A Unit Testing tool is a
framework that makes it Easy™ to write, maintain, execute, and collect &
present results from, a number of unit tests. With JUnit, we can do all of the above. JUnit comes with a graphical interface as
well as a command line interface. It
also interacts with sophisticated development environments like IBMÂ’s VisualAge
for Java Using JUnit
Using the JUnit testing framework will involve the following
steps:
Writing tests
You should first write a class that extends
junit.framework.TestCase like this:
import junit.framework.*; public class MyTest extends TestCase
{ Each unit test you write will be a method of this
class. It is a popular convention to
give the tests a name that starts with `testÂ’. The code below shows a class with two tests: public class MyArithTests
extends TestCase { public MyArithTests (String name) { super(name); } public void test_one() { int a = 1, b =
2, c; c = a + b; assert( 3 == c); } public void test_two() { int a = 1, b =
2, c; c = a * b; assert( 2 == c); } } assert() is a function that is available via the TestCase
class. The assert succeeds if the
condition passed to it is true, and it fails otherwise. test_one wants to check
if Java’s implementation of the “+” operator is right or wrong!! When an assert fails, the JUnit makes a note
that the test has failed, and if a test routine returns without any
error/failure, JUnit makes a note that the test has succeeded. Add as many unit tests as you like, and compile the java
file(s). Executing the Unit Tests
The JUnit framework can present the test results in three
different interfaces – a command-line interface, and two graphical interfaces,
one each based on java.awt and java.swing APIs. The invocation of the test execution command depends on which
interface you want the results in. If you want a vanilla awt-based output, issue the following
command: java
junit.awtui.TestRunner MyArithTests If you are in the mood for a swing-enhanced user experience,
fire the following command: java
junit.swingui.TestRunner MyArithTests If you are in a graphics-impaired state, the following might
be the recipe for you: java
junit.textui.TestRunner MyArithTests What happens, really, is
quite simple. The main function in
TestRunner extracts all the methods from the MyArithTests class that start with
“test” and runs them, keeping track of any errors that occur while that
happens. Errors include failed
`assert()Â’s, exceptions and all that stuff.
Screenshots and the like are beyond the scope of this document writerÂ’s
abilities. Readers are encouraged to
experience the feeling themselves. Expert JUnit!! What we have seen is the simplest way to
invoke and use JUnit. It is more than
likely that testing non-trivial code will need non-trivial testing. There are some additional features in JUnit
that we will touch upon here.
setUp() and tearDown() In the example above, both
test_one() and test_two() declared and used the same variables a, b, c, and
even assigned the same values to a, b.
It would be great if we can simply set them up once and reuse them. This will be a recurring pattern. We will want to setup a set of objects and
use them in different tests. In a
normal class, we could have made a, b, c as private members and written a
constructor to initialize the variables of our choice. But overloading constructors like this will
not work with the JUnit framework, and so there is a method called setUp()
which can be overridden to initialize our variables. The following code snippet shows how we could use the setUp
method: public class MyArithTests
extends TestCase { private int a, b, c; void setUp() { a = 1; b = 2; } // Â… Snipped } As you would have guessed, tearDown() is run at windup
time. You can use it, perhaps, to do
some reporting of your own or whatever! Conclusion
JUnit is a very simple
tool that can be used to organize and easily execute a number of test cases,
which can be built incrementally. This
document did not cover all features of JUnit but is only meant to be an
introduction with which you can get right into using JUnit beneficially. |
-- "Have you ever fought an IDEA, Picard?" -- Gowron, Star Trek, TNG
[Prev in Thread] | Current Thread | [Next in Thread] |