The following xml file is the ant execution file that successfully runs the JUnit test and generates the test report after I modify it. First, list the code as a reference, and then introduce some problems that I encountered during the file writing process and make some analysis, hoping to help the learners later.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Project name = "MathTest" default = "test" basedir = ".">
<Property name = "src" value = "src"/>
<Property name = "dest" value = "bin"/>
<Property name = "lib" value = "lib"/>
<Property name = "report" value = "report"/>
<Path id = "class. path">
<Fileset dir = "$ {lib}">
<Include name = "**/*. jar"/>
</Fileset>
<Pathelement path = "$ {dest}"/>
</Path>
<Target name = "init">
<Mkdir dir = "$ {dest}"/>
<Mkdir dir = "$ {report}"/>
</Target>
<Target name = "compile" depends = "init">
<Javac srcdir = "$ {src}" destdir = "$ {dest}">
<Classpath refid = "class. path"/>
</Javac>
</Target>
<Target name = "test" depends = "compile">
<Junit printsummary = "true" haltonfailure = "no" showoutput = "true">
<Classpath refid = "class. path">
</Classpath>
<Formatter type = "xml" usefile = "true"/>
<Batchtest fork = "yes" todir = "$ {report}">
<Fileset dir = "$ {dest}">
<Include name = "**/* Test. *"/>
</Fileset>
</Batchtest>
</Junit>
<Junitreport todir = "$ {report}">
<Fileset dir = "$ {report}">
<Include name = "TEST-*. xml"/>
</Fileset>
<Report format = "frames" todir = "$ {report}/html"/>
</Junitreport>
</Target>
</Project>
1. What should I do if I want to reference an external jar package?
An external package is definitely required during the test. For JUnit testing, the jar package of JUnit is required. For selenium testing, selenium is required. You can put the required package in the specified folder, and then define the path to include the jar file. When compiling, running, or testing, you need to reference these packages by defining the environment variable attribute classpath and referencing the defined path. Multiple jar packages can be referenced in regular expressions for simplicity. If there are some files or packages to be referenced in other places, you can also specify the pathelement separately.
2. How are output logs displayed during the test?
You can use the showoutput attribute of junit to set it.
3. How to perform a junit test on multiple test classes?
You can use regular expressions to match the files to be tested. For example, the code above tests all the names in the compiled executable file path including * Test. **/** Test. * is used to execute all the files that meet the conditions in the dir directory, including the files in the subdirectories.
4. What should I do if I want to organize the source files and compiled files in the form of packages so that the files are scattered in different folders?
You only need to specify the source file and the root directory of the compiled target file. ant will automatically compile all the files in the source file directory and generate the class file in the same way in the target file. Therefore, src and dest do not need to specify the detailed relative location of a specific file. For example, in the above Code, the source file is in src/org/twenz, And it will automatically generate the class file in dest/org/twenz.
5. Why does the following error occur after ant in Eclipse in junitreport?
[Plain]
[Junitreport] Loading stylesheet jar: file:/E:/Program % 20 Files/eclipse/plugins/org. apache. ant_1.8.3.v20120321-1730/lib/ant-junit.jar! /Org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl.
[Junitreport]: Error! The first independent variable of the non-static Java function "replace" is not a valid object reference.
[Junitreport]: Error! You cannot convert the Data Type "void" to "reference ".
[Junitreport]: Fatal Error! Unable to compile style sheet www.2cto.com
[Junitreport] Failed to process E: \ users \ zhatan \ workspace \ JunitTestProject \ report \ TESTS-TestSuites.xml
You can directly use the command line ant to avoid this error.
6. Why is Test-*. xml in two lines after the junitreport command, and the number of cases in the generated html file is 0?
Probably because the junit Command did not export the results, for example, the statement <batchtest fork = "yes" todir = "$ {report}"> in the preceding file generates the result to the specified directory through todir. Set usefile = "true" to obtain the Test-*. xml file. If these files are not correctly generated, the use case in the html file generated in the subsequent report is 0.