Basic Settings
This section demonstrates how to test the following class (
Click here to download the accompanying code):
package tested;
/**
* @author Ploix, Forgeot d'Arc
*
*/
public class MyTested {
public char aMethodWhichIsOk (){
String s = "ABCD";
return s.charAt(2);
}
public char aMethodThatFails (){
String s = "ABCD";
return s.charAt(5);
}
}
First, build a test with JUnit and write it in Java. In Eclipse, create a new JUnit test case. If the JUnit jar file is not referenced in your project, Eclipse will propose to add it. Say yes!
Now, build a regular JUnit test case, derived from junit.framework.TestCase. You should get something like this:
package tests;
/**
* @author Ploix, Forgeot d'Arc
**/
import tested.MyTested;
import junit.framework.TestCase;
public class MyJavaTesting extends TestCase {
private MyTested tested = null;
public void setUp() {
tested = new MyTested();
}
public void testTheOkMethod() {
tested.aMethodWhichIsOk();
}
public void testTheFailMethod() {
tested.aMethodThatFails();
}
}
In your Eclipse project settings, point your continuous testing properties to the MyJavaTesting class (see Figure 1).
Eclipse immediately displays the result of your test in the JUnit view (see Figure 2).
Next, you can write other tests in Jython and integrate the Jython test results seamlessly inside the same view. To do that, first you need to have Jython installed. And you also need to add the Jython jar file to your Eclipse project. Find it in your project properties (see Figure 3).
 | |
| Figure 3. The Project Properties, Showing the Referenced Jar |
You can have a Jython plugin installed, which could be better. (I use the "Jython Development Tools" plugin.) Don't forget to set its properties in the main Eclipse Properties (see Figure 4).
Once everything is set, you should write your Jython test. Say, for instance, the following is your Jython class:
import junit, unittest
import tested
## @author Ploix, Forgeot d'Arc
## inherit from the TestCase class from Java
## and also from the one from unittest from python
class PythonTestCase (junit.framework.TestCase, unittest.TestCase):
"""Class designed to be played either by unittest from Python (Jython, in fact),
or from JUnit in Java. Please cut and paste this class, and create your own test
methods (the one beginning by "test". Then report to the samples in Java to
see how to reference them from JUnit them.
"""
def __init__(self, name):
"""
name is the name of the test (the method) to play
"""
junit.framework.TestCase.__init__(self,name)
self.theTestFunction = getattr(self,name)
def setUp(self):
"""called before each test."""
pass
def tearDown(self):
"""called after each test."""
pass
def runTest(self):
"""tells JUnit which test to run"""
self.theTestFunction()
def testOkInJython(self):
inst = tested.MyTested ()
inst.aMethodWhichIsOk ()
def testFailedInJython(self):
inst = tested.MyTested ()
inst.aMethodThatFails ()
You can recognize many things from JUnit: the inheritance, the testXXX method names, the setUp, the tearDown, and so on. You also can see that the setUp and tearDown methods are important here. Usually, you will use them as well.
Also, your Jython class uses a multiple inheritance, as one class comes from Java and the other comes from Python. This is useful only if you need to start your tests from Jython alone also. Here, you don't really need to inherit from unittest.TestCase.
The runTest (from JUnit) and __init__ (the constructor) are very important. The Java code uses them to run the test itself. Whenever you define your own Jython JUnit test case, you should have these methods implemented or inherited.
The problem becomes integrating this test into your JUnit tests.