The JUnit of ANT task

Source: Internet
Author: User

The JUnit of ant task:

  Learning ant is primarily about learning Ant's task,ant There is a testing tasks in many tasks, which have two missions: JUnit and Junitreport, which are used primarily for unit testing and generating unit test reports.

Testing Tasks  
Task Name Description
Junit

Runs tests from the Junit testing framework. This task had been tested with JUnit 3.0 up to JUnit 3.7; It won ' t work with versions prior to JUnit 3.0.

Junitreport

Merges the individual XML files generated by the Junit task and applies a stylesheet on the resulting merged document to P Rovide a browsable report of the testcases results.

Official website:http://ant.apache.org/manual/index.html

This article mainly introduces JUnit task,<junit>, which can contain other elements, such as:

1, <test>: Running a single testcase

2, <batchtest>: Running multiple TestCase

3, <formatter>: Define test result output format

There's a lot more to see in the official documentation.

Ii. Examples of projects:

This article does not explain how to build an ant environment because ant installation is relatively simple, with a lot of online searches and now ecplise basically with Ant.

In addition, you can bring up the Ant view in Eclipse by: Window->show view

1. The directory structure is as follows:

2, the Simplecalculation class code is as follows:

 1  package   com.glen.he;  2   Public  class   simplecalculation { 4  public  int  ADD (int  a,int   b) { 5  return  (A+B);  6  }   8 } 
simplecalculation

3. The test class Simplecalculationtest code is as follows:

1  Packagecom.glen.he;2 3 Importcom.glen.he.SimpleCalculation;4 5 Import Staticorg.junit.assert.*;6 Importorg.junit.Test;7 8  Public classSimplecalculationtest {9 TenSimplecalculation sc =Newsimplecalculation (); One      A @Test -      Public voidaddtest () { -          the         intc = SC. ADD (3, 5);  -          -Assertequals (8, c);  -     } +}
simplecalculationtest

4, in the project to the directory to add the Build.xml file, the contents are as follows:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Projectname= "Antdemo"default= "JUnit"Basedir=".">3     <!-- ===================================================================  -4     <!--Variable Settings -5     <!-- ===================================================================  -6 7     <!--Source code src path -8     < Propertyname= "Src.path"value= "Src/java"/>9     <!--compile file class path -Ten     < Propertyname= "Build.path"value= "Build"/> One     <!--Unit Test Code path - A     < Propertyname= "Test.path"value= "Src/test"/> -     <!--lib Package path - -     < Propertyname= "Lib.path"value= "Lib"/>          the          -     <!-- ===================================================================  - -     <!--set Classpath - -     <!-- ===================================================================  - +     <PathID= "Compile.path">         -         <Filesetdir= "${lib.path}"> +             <includename= "**/*.jar"/> A         </Fileset> at          -         <pathelementPath= "${build.path}"/> -     </Path>      -  -     <!-- ===================================================================  - -     <!--Clear History Compilation class - in     <!-- ===================================================================  - -     <Targetname= "clean"Description= "clean"> to         <Deletedir= "${build.path}"/> +     </Target> -  the     <!-- ===================================================================  - *     <!--compiling the test file, initializing the directory - $     <!-- ===================================================================  -Panax Notoginseng     <Targetname= "Compile"Description= "Compile"> -         <mkdirdir= "${build.path}"/>                         the         <JavacSrcdir= "${src.path}"Destdir= "${build.path}"Classpathref= "Compile.path"/> +         <JavacSrcdir= "${test.path}"Destdir= "${build.path}"Classpathref= "Compile.path"/> A     </Target>       the           +     <!-- ===================================================================  - -     <!--execute a test case - $     <!-- ===================================================================  - $     <Targetname= "JUnit"depends= "Clean,compile"> -         <JUnitprintsummary= "true"> -              <ClasspathrefID= "Compile.path"/>                   the                        -              <Testname= "Com.glen.he.SimpleCalculationTest"/>Wuyi          </JUnit> the      </Target> -  Wu </Project>
View Code

Description

<junit printsummary= "true" >
<classpath refid= "compile.path"/>
<test name= "Com.glen.he.SimpleCalculationTest"/>
</junit>

<path id= "compile.path" >
<fileset dir= "${lib.path}" >
<include name= "**/*.jar"/>
</fileset>
  <pathelement path= "${build.path}"/>
</path

under the <junit〉 task, we used the compiled. class file's directory and the directory where the jar package needed to compile it. because the JUnit task is actually running the test class for us, not just javac compiling as we publish the ant file, we just need to compile the required jar packages. We also need to ship the. class file as a Java task, so you must include the compiled. class file.

5. Then drag the Build.xml file into the ant view, for example, double-click JUnit execution.

6. Execution Result:

1 buildfile:d:\anttest\build.xml 2 clean:3    [delete] Deleting directory D:\AntTest\build 4 compile:5     [mkdir] Cre Ated dir:d:\anttest\build   6     [Javac] compiling 1 source file to D:\AntTest\build 7     8     [Javac] compiling 1 s Ource file to D:\AntTest\build 9 junit:10     [JUnit] Running com.glen.he.SimpleCalculationTest11     [JUnit] Tests Run : 1, failures:0, errors:0, time elapsed:0.016 sec12 BUILD SUCCESSFUL13 Total time:1 Second

Reference:

http://blog.csdn.net/shendl/article/details/532587

http://blog.csdn.net/tochal/article/details/12560151

The JUnit of ANT task

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.