Developers can choose to automate unit tests for multiple reasons. Many people even further exploit it to automate the positioning and execution of these tests. But what if I want to test the device module (test harness) to run as defined statically? Follow the developer Michael nadel to see how to use python to simulate the static JUnit testsuite class. The JUnit testing framework is used by more and more development teams. Thanks to a variety of Test Modules, you can now test any Java applicationProgramAlmost every component. In fact, almost the entire secondary market seems to be built around JUnit. Modules including cactus, jfcunit, xmlunit, dbunit, and httpunit can be used for testing applications for free. As the complexity of the system increases and so many tools are available, there is no reason not to rely on unit testing. However, developers are not just programmers. We interact with users to fix bugs and determine requirements. We attended the meeting and made a telemarketing call. We have completed some (sometimes all) quality assurance functions. Since there are so many responsibilities, it is natural to want to be as automated as possible. Because good teams (except for other things) perform a lot of tests, people who want to automate different development processes often perform detailed research on this field. Automated Unit Testing There are many ways to automate the positioning and execution of all project test cases. One solution is to combine ant's JUnit tasks with embedded fileset tasks. In this way, you can include and exclude files in a specific directory (based on the file name style ). Another option is to use eclipse, which can specify the directory where all tests are located and executed. The previous option provides the flexibility to filter running tests (and because it is a pure headless Java application that can run in almost all places ), the latter option allows debugging of the "dynamic" package. Can the two methods be combined with the power and flexibility? With PythonProgramming LanguageJava platform implementation-Jython, the answer is loud "Yes !" (If you are not familiar with Jython, you should add this knowledge before continuing this article. For more information, see references below ). With the strength and elegance of Jython, you can maintain a file system, search for classes that match a certain style, and dynamically compile scripts of the JUnit testsuite class. Like all other static-defined classes, this testsuite class can be easily debugged with a favorite debugging program. (The example used in this article assumes that Eclipse IDE is used. However, the technology I described here can be used in most other ides without many modifications .) When making any design decision, you must weigh the impact of the selection and decision. Here, in order to obtain the ability to debug dynamically generated test packages, additional complexity must be added. However, this complexity is mitigated by Jython itself: Jython has been well tested and well supported, and is openSource code. Moreover, python is increasingly becoming the de facto standard for object-oriented and platform-independent programming. For these two reasons, there are very few risks of using Jython, especially because it provides unparalleled flexibility in creating and debugging dynamically generated JUnit testsuite classes. If Jython is used or not, it can be used to solve the problem. If Jython is not used, you can use a Java property file to store a group of classes, directories, and packages to add or exclude tests in the packages. However, if you select Jython, you can use the entire Python language and runtime to solve the problem of which tests you choose to execute. Python scripts are much more flexible than Java property files, and they are limited by your imagination. Using the seamless integration of Jython and Java platform, you can create static-defined testsuite classes that are dynamically built. There are a lot of tutorials on JUnit, but let's look at the following two lines.CodeAs a review. Listing 1 is an example of Statically building the testsuite class (this example is taken from JUnit: A Cook's tour. for links to it and other JUnit resources, see references ): Listing 1. static definitionTestsuite Public Static Test Suite (){ Return new testsuite (moneytest. Class ); } Listing 1 shows that testsuite is composed of class instances of the test class. This assembly module fully utilizes this point. To analyze the code of this tool, you should download the sample jar file in this article from references. This document contains two files: dynamictestsuite. java and getalltests. py. The former is a Python script that uses the phthon script to dynamically generate the JUnit test fixture module of testsuite, and the latter is a Python script that searches for files matching a specific style. Dynamictestsuite. Java uses getalltests. py to build testsuite. You can modify getalltests. py to better suit the needs of your project. Understand the testing Module How does the Code work? First, assign getalltests. py to obtain a group of test classes to be executed. Then, use the Jython API to extract the list from the python runtime environment. Then, use Java reflection API to construct a class instance of the string object in the list that represents the test class name. Finally, use the JUnit API to add test to testsuite. The cooperation between the four databases can achieve your goal: dynamically built testsuite can run as defined statically. Take a look at the JUnit suite list in Listing 2. It is a testcase that discloses the public static testsuite Suite () method signature. The suite () method called by the JUnit Framework calls gettestsuite () and gettestsuite () calls getclassnamesviajython () to obtain a set of string objects, each object represents a testcase class as part of the package. List 2. Dynamic DefinitionTestsuite /** * @ Return testsuite a test suite containing all our tests (as found by Python script) */ Private testsuite gettestsuite (){ Testsuite suite = new testsuite (); // Get iterator to class names we're re going to add to our suite Iterator testclassnames = getclassnamesviajython (). iterator (); While (testclassnames. hasnext ()){ String classname = testclassnames. Next (). tostring (); Try { // Construct a class object given the test case class name Class testclass = Class. forname (classname ); // Add to our suite Suite. addtestsuite (testclass ); System. Out. println ("added:" + classname ); } Catch (classnotfoundexception e ){ Stringbuffer warning = new stringbuffer (); Warning. append ("Warning: Class '"). append (classname). append ("' Not found ."); System. Out. println (warning. tostring ()); } } Return suite; } Make sure that the system attributes are correctly set at the beginning. Internally, Jython uses the python. Home attribute to locate the files it needs. Finally, the getclassnamesviajython () method will be called, and some wonderful things will happen here, as will be seen in listing 3. Listing 3. Extracting Java objects from Python Runtime
/** * Get list of tests we're going to add to our suite * @ Return list a list of string objects, each representing class name of a testcase */ Private list getclassnamesviajython (){ // Run Python script Interpreter.exe cfile (getpathtoscript ()); // Extract out Python object named python_object_name Pyobject alltestsaspythonobject = interpreter. Get (python_object_name ); // Convert the python object to a string [] String [] alltests = (string []) alltestsaspythonobject. _ tojava _ (string []. Class ); // Add all elements of array to a list List testlist = new arraylist (); Testlist. addall (arrays. aslist (alltests )); Return testlist; } First, judge the python file. Then, extract a pyobject from the python runtime. This is the obtained object, which contains the class names of all test cases that will constitute the test package (remember-pyobject is the Java runtime counterpart of the python object ). Create a specific list and fill it with pyobject. Use _ tojava _ to indicate that pyobject converts its content into a Java string array. Finally, the control is returned to gettestsuite (), where the test cases of the Jython identity are loaded and added to the composite. Install the test fixture module in the Development Environment Now I have a good understanding of how to test the assembly module and may not be able to try it on my own. You need to complete the following steps to configure eclipse to run this installation module. (If you use different ides, you should be able to easily modify these steps for your environment .) Install Jython 2.1. If not. (For more information, see references ). Copy getalltests. py to the main directory. Edit getalltests. py row 25th to specify the root path of the source file. The file name that matches * text. Java in the org package in all directories under this location will be searched. If necessary, modify row 54th to change the root package name (for example, change to com ). Copy dynamictestsuite. Java to the source tree. Add the following jar to the Eclipse project: JUnit. Jar (the binary file of the JUnit framework. For download information, see the JUnit web site ). Jython. Jar (the Jython binary file, located in the Jython installation directory ). Load the dynamictestsuite class to the eclipse Java source file editor. Perform one of the following steps: Select dynamictestsuite in the package explorer view, or press Ctrl + Shift + T and enter the field dynamictestsuite in the choose type field. Select Run from the File menu bar, and then select debug .... Select JUnit configuration. Click new. A new JUnit target, dynamictestsui |