Let compilation and test process automation

xiaoxiao2021-03-06  69

Ant and Junit let you be more close to XP dreams

Erik Hatcher (Erik@hatcher.net) EBLOX Senior Designer August 2001

Gradually increment testing and continuous compilation are two kinds of extreme programming methods. Make both into a separate, automated process-plus automatically generated email reports - you will take a solid step on the road to XP dreams. Please follow Erik Hatcher, he showed you how he modified the popular ANT 1.3 and JUnit test framework to achieve complete, customized automation.

One of the main principles of extreme programming (XP) is that programmers must perform regular unit testing, but also integrate changes to the environment of similar products. In addition, XP recommends automating this process as much as possible. After all, if the developer creates a test case like production code, the process will relatively relatively relatively relatively relatively.

If the unit test is good, you should satisfy each of the code (especially by individual classes). Perform continuous or unstable compilation allows you to understand how it takes effect when the code is integrated into a production environment. In the development cycle, integration unit testing and regular automatic compilation, to ensure that the code is reliable when the code is published.

In this article, I will take a practical method for the automatic compilation and testing process. Using the ANT 1.3 and JUnit test frameworks will be displayed like you to automate a process, this process captures related information that each test suite to run, generate attractive reports and send this report with email. Although these properties have many hidden in Ant, I still have modified many core tasks to better meet the requirements. These modifications are the center of this article, which will all incorporate all into Ant's next release. All patchs can be found in the source file included in this article (see References). You may need to review the code when reading. In addition, if you are not familiar with Ant 1.3, you may need to learn the official ANT documentation when you continue this article (see Resources).

Why use ANT and JUNIT? ANT 1.3 is the actual standard in the compilation tool. Due to the Java language, Ant is open source, can run on a variety of platforms and bring great flexibility for the compilation process. The JUnit test framework is also open source, it is widely used and integrated with Ant's compilation process (see more about Ant and Junit, see Resources).

Ant 1.3 plus optional and tasks, you can start basic compilation and test automatic processes without modification. This process is as follows:

Run the JUnit test utility capture test results Create an attractive HTML summary report

Once the capture result, you can use the XML formatter to run the number of faults and errors for each test suite, along with the execution time of the test suite with the package and class name. For each test suite, the following information is captured:

Test case's name execution duration failure or error type (if applicable) any fault or error details

There are fewer features in this idea to function perfect, but the automation described above is neither ideal. By modifying a few in the JUnit task, we can create the automated procedure running as follows.

Executing the JUnit Test to record the results to XML files or other formats to translate the results to the XSL format test report transforms this report to HTML format with email send reports

Please give me more data, in addition to extension of Ant and Junit to the extent of automatic compilation and testing, I also added standard data captured during testing. That is, I need to know what operating system, the test date / time, support the JVM version of the test run, and ClassPath. To capture this information, I have a simple modification to the four classes in the JUnit related class of Ant: JUnittask, JunitTest, JunitTestrunner, and XmljunitResultFormatter make simple modifications. You will find these changes in the included source file.

As an attached advantage, when the captured data is expanded, the final capture is not only information on the specific state of the test suite, but also the operation attribute set of the entire Ant. It contains system properties and internal ANT properties (such as user-defined properties).

Let data report automation extends to Ant's task, so that it can provide additional status information, the next step is to enable automated data reports.

One of the biggest advantages using Ant is its scalability. An XML file is created for each test class running using the task with an XML format (

Listing 1. Overview_summary.xsl code segment to display newly added properties

Once these files are modified, you can run compilation. The build.xml code segment responsible for running the JUnit test and creates an HTML report as shown in Listing 2.

Listing 2. BUILD.XML to run the JUnit test and create a part of the HTML report

Description = "Execute Unit Tests">

Regarding this compile code segment, the main thing you should pay attention to is the styleDir property of element in ; it specifies the directory where the user-defined XSLT file is located. To capture the date / timestamp, you need to introduce tasks before the task at build.xml.

Let the email task automation Ant has a built-in task, but unfortunately this task is only the performance of text messages, and cannot handle the file attachment. But recently, a task is submitted to an Ant development group that implements the performance of MIME mail.

I work with the other two active ANT developers to improve this recent submitted task so that it can support the fireset feature of Ant and the mail features of Cc and Dark delivery. I also added another very good feature - the ability to attach the file to the body of the email message.

Modern email readers can display the included HTML content as MIME, but also interpret HTML code as the browser. Figure 1 shows what the JUnit sent by ANT is displayed using the task.

Figure 1. JUnit report sample sent with ANT

The code for sending an email is shown in Listing 3.

Listing 3. BUILD.XML used to send email code segments

ClassName = "org.apache.tools.ant.taskdefs.optional.mail.mimemail" />

Messagefile = "overview-summary.html"

Tolist = "erik@eblox.com"

Mailhost = "mail.eblox.com"

Subject = "JUnit Test Results: $ {Today}"

From = "erik@hatcher.net">

Code Note:

is used to define the task because the ANT does not provide this feature. In the future, this task may become one of the optional tasks of Ant; then will become extra. is used to create Today properties in the email topic. In the "Preparation" or "Initialization" phase of Build.xml, it contains tasks, so it may be redundant according to your situation. has a clear self-explanatory syntax. The available code contains the Ant-style help file for this task (see Resources). Not only the HTML report is sent (using the messagefile property to attach the body part of the message), and the original XML result file is also sent to as a standard MIME attachment is sent. creates a file called Overview-Summary.html (because "NOFRAMES" option is specified). Dissu, test and deployment, I have provided all code amendments just discussed for Ant development groups, they have already approved it to contain it in the subsequent release. However, because the official ANT release has not included the skills used in this article, some measures need to be taken before letting these techniques for your service.

Let the code take effect first, the correction of these code is based on the ANT 1.3 release; this is the unique Ant version I will recommend for this code. The code provided contains JUnitmail.jar and source code for generating JAR files. This JAR file must exist in the ClassPath in the launch Ant environment - before the Optional.jar of Ant.

If you are a typical Ant user, you run Ant through one of the package scripts in $ ANT_HOME / BIN. These packaging scripts put all the JAR files in all $ Ant_home / lib in the ClassPath before starting ANT. Under the Windows platform, just put junitmail.jar in the $ ANT_HOME / LIB directory, you can handle the ClassPath problem with Ant.bat.

If you have encountered a quirky behavior (for example, the extra parameters that may be captured from are not appearing in the XML file), then debug the classpath used in the package script (for example,% localclasspath% in Ant.bat) And modify the package script to ensure that junitmail.jar in ClassPath is before Optional.jar.

After the test code integrates this new code into the ClassPath, test a simple JUnit test example with an XML formatter (as shown in Listing 2) to ensure that additional ANT properties are captured.

Additional properties are captured in the XML formatter as the element. If, due to some reason, the attribute element does not appear, it may be a problem with ClassPath.

Make sure that the corrected JUnit task is being used, an insurance approach is to use the JUnitTask.class, JunitTest.class, JunitTrunner.class, JUnitTrunner.class, and XmljunitResultFormatter.class provided in my JunitMail.jar version instead of the corresponding file in Optional.jar.

Task has two dependent conditions - JavaMail and Java activity framework (see Resources). When starting ANT, these two JAR files must be placed in the classpath to use . Custom layout You can customize the HTML layout of the JUnit results by adjusting the XSLT file. By adding additional attributes to each test suite in hidden

, I have modified the standard layout of Ant by adding some JavaScript for hidden and displaying the additional information. These JavaScript tips can take effect in Internet Explorer, but does not work in Netscape (it shows all properties, not hidden).

The primary purpose of adding new capture properties is just to let those information available, but these attributes are combined into the HTML report, and there is a big improvement. (Remember, if you have made any improvements, please submit it to the ANT developer and user group, let us share it!)

Let XP come true once you use my patch, let the compilation and test processes are only a problem that the ANT execution in the CRON job (or the AT job on the Windows platform).

The idealized idea is that the automatic process will extract the code from your source management system, complete the compilation, execute unit test, and then use email to send results per night. There is also the entire compilation log to send or post it or post to the Intranet site, so you will be close to "Sustained Integration" and XP Dreams.

Reference

Please participate in the discussion forum on this article. You will find all Ant's JUnit tasks and patchs for the new MIMEMAIL task in this compressed file, and enhanced XSL files. Please download Ant 1.3 from Jakarta Project. If you still don't have (using JUnit) test code today, it is now coming. To start merge Ant / Junit, please read Malcolm Davis's "Incremental Development with Ant and Junit" (DeveloperWorks, November 2000). Excellent articles, "Ant In Anger" describes the use of Ant in a typical product development environment. Try extreme programming sites to get information about this topic. The JavaMail and Java activity framework is the task. Ant and Junit are the perfect testing of the WebSphere application development environment; version 4.0 provides enhanced extensions, such as Java business rules, allowing you to change the Java app without having to rewrite the code. Requires an enterprise-level system testing, please see what IBM Performance Management, Testing and Scalability Services provides something (including a library, PDF format) about the company. Please find more Java reference materials in the developerWorks Java Technology Zone.

About the author Erik Hatcher is crazy in Dot-COM this year, but he discovered his home in Eblox, develop J2EE solutions in the XP method. Please contact him through erik@hatcher.net.

转载请注明原文地址:https://www.9cbs.com/read-92033.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.035, SQL: 9