Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Write Eclipse JUnit Tests in Jython : Page 3

Eclipse integrates the JUnit framework to enable seamless test integration in Java, and Python is a great language for writing tests. Learn how to write Eclipse JUnit tests in Jython (Python for Java).


WEBINAR: On-Demand

Unleash Your DevOps Strategy by Synchronizing Application and Database Changes REGISTER >

Integrating the Jython Test
Integrating your Jython test in JUnit is very easy. It takes one line of Java code per Jython class. But to fully understand how to do it, consider the following steps you'll take in this section:
  1. You use the "Suite" way to discover the tests under JUnit. JUnit can use a suite() method to get the tests to run. You just have to build a Suite that gives JUnit enough information to run the Jython tests.
  2. This suite() method is not as simple to use as it seems, for it is static. So you have to discover the Jython tests to insert in the suite before they are instantiated. You will use static blocks to extract the Jython tests.
  3. You make some introspection inside Jython, then get the Jython Suite and cast it into the Java space through useful classes.

The first class to use is a Jython Suite Extractor (JythonTestSuiteExtractor.java). It basically extracts the proper methods from your Jython class and adds them to a suite. Then the suite, built in a Jython interpreter, is converted into Java. You should not have to modify anything in it.

The second class (JythonTestCaseExtractor.java) is a bit trickier. It uses static calls and structures to store the Jython test cases that will have to be integrated into the suite. You won't need to modify this class either.

The third class (MyJythonTestCase.java) just loads your Jython test case into the JythonTestCaseExtractor, in a static way. This class should be adapted to integrate your own Jython tests once you have new ones, as follows:

/* * Created on 1 Sept. 2004 * */ package tests; import junit.framework.Test; import junit.framework.TestCase; import fr.sgf.tools.pyjunit.JythonTestCaseExtractor; /** * To use a python test case inside JUnit, you need to define a class that * implements two (horrible) things: a static block and a suite method. The * reason comes from the way JUnit works--I'm innocent ;-). So define a class * that extends TestCase: the static block should add the * Python tests with directory, module name, and class name. The suite block * should call JythonTestCaseExtractor.suite(), that's all. God, it was a mess. * Oh, and if you want to integrate it inside another Suite, you should call * suite.addTest(MyJythonTestCase.suite()), and not * suite.addTestSuite(MyJythonTestCase.class), which doesn't work. * * @author Ploix, Forgeot d'Arc * * */ public class MyJythonTestCase extends TestCase { static { JythonTestCaseExtractor.addPythonTest("bin/tests", "JythonTestSuite", "PythonTestCase"); } public static Test suite() { return JythonTestCaseExtractor.suite(); } }

The static block adds the Jython class, considering the following factors:

  • The classes can be found in bin/tests directory of your project.
  • The class to add can be found in the JythonTestSuite module.
  • The Jython class itself is named PythonTestCase.

You could add as many "addPythonTest" calls to integrate as many test cases as you need. The previous snippet included only one. In any case, any method beginning with the word "test" in the Jython classes will be added.

Now, you have to tell JUnit that this Java class is the one that contains the JUnit tests. If you run this class as a JUnit test, the Jython tests will be used instead.

If you want to see your Java and your Jython tests in the same view, you have to integrate them in another suite. That's easy. Just write a JUnit test case:

/* * Created on 20 August 2004 * @author Ploix, Forgeot d'Arc * */ package tests; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * @author ploix * */ public class AllTests extends TestCase { public static Test suite() { TestSuite suite = new TestSuite("Tests in Java and in Jython"); suite.addTestSuite(MyJavaTesting.class); suite.addTest(MyJythonTestCase.suite()); return suite; } }

Figure 5. The Jython and Java Tests in the Same Window

The Jython wrapper (MyJythonTestCase) should be added with a call to the suite() method.

Your Eclipse JUnit plugin will see this class as if the Jython tests were written in Java (see Figure 5).

Once you have set it up, writing new Jython tests is very straightforward. You need to add one line of code to the MyJythonTestCase class and it works. New Jython methods integrate immediately and without any compilation steps. Jython and Java tests are started and their results are displayed in the same view.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date