Trước tiên ta tải JUnit tại đây và lưu nó trong thư mục lib.
Thư mục dự án (project) trong Eclipse có cấu trúc sau:
Nội dung tập tin Ant testBuild.xml :
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <project basedir="." default="build" name="Test"> <property name="src.dir" value="${basedir}/src"/> <property name="build.dir" value="${basedir}/bin"/> <property name="lib.dir" value="${basedir}/lib"/> <property name="report.dir" value="${basedir}/report"/> <path id="test.classpath"> <fileset dir="${lib.dir}" includes="*.jar" /> </path> <target name="init" description="create report folder and copy all non-java code to bin folder"> <mkdir dir="${report.dir}"/> <copy includeemptydirs="false" todir="${build.dir}"> <fileset dir="${src.dir}"> <exclude name="**/*.java"/> </fileset> </copy> </target> <target name="clean" description="clean generated folder"> <delete dir="${report.dir}"/> </target> <target name="compile" depends="clean, init" description="compile sources"> <javac srcdir="${src.dir}" destdir="${build.dir}" encoding="ISO-8859-1"> <classpath refid="test.classpath"/> </javac> </target> <target name="test" depends="compile"> <junit printsummary="true" haltonfailure="false" showoutput="yes" logfailedtests="true" failureproperty="tests.failed"> <classpath> <pathelement path="${build.dir}"/> </classpath> <classpath refid="test.classpath"/> <!-- <formatter type="plain"/> --> <formatter type="xml"/> <batchtest todir="${report.dir}"> <formatter type="xml"/> <fileset dir="${build.dir}" includes="**/*Test.class"/> </batchtest> </junit> <junitreport todir="${report.dir}"> <fileset dir="${report.dir}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${report.dir}"/> </junitreport> <fail if="tests.failed"> There are failed test(s) </fail> </target> <target depends="test" name="build"/> </project>
Giải thích :
- Trong thẻ
project
, ta định nghĩa mục tiêu (target) mặc định là build
.- Các thẻ
property
dùng tạo các biến ta dùng.- Thẻ
path
định nghĩa đường dẫn đến các tập jar trong thư mục bin
mà ta cần dùng.- Các thẻ
target
định nghĩa các mục tiêu.- Mục tiêu "init" nhằm : tạo thư mục
report
để lưu báo cáo phát sinh, chép các tập tin không java (nếu có) trong thư mục src
vào thư mục bin
(thư mục chứa mã java đã được biên dịch sang bytecode).- Mục tiêu "clean" nhằm xoá thư mục
report
.- Mục tiêu "compile" để biên dịch mã nguồn trong thư mục
src
vào thư mục bin
. Thuộc tính depends
của thẻ này nhằm định nghĩa các mục tiêu phụ thuộc: khi thực hiện mục tiêu "compile" thì hai mục tiêu "clean" và "init" sẽ được thực thi trước.- Mục tiêu "test" : thẻ
junit
nhằm thực thi các kiểm thử đơn vị, nó gọi các lớp được biên dịch trong thư mục bin
có tên tận cùng là Test (**/*Test.class)
và lưu kết quả chạy kiểm thử này dưới dạng tập tin xml trong thư mục report
. Thẻ junitreport
nhằm phát sinh báo cáo cho toàn bộ những kiểm thử vừa được thực hiện dưới dạng html và lưu trong thư mục report
. Thẻ fail
nhằm in ra thông báo trong trường hợp có lỗi khi thực thi các kiểm thử.- Mục tiêu "build" nhằm gọi tới mục tiêu "test".
Lưu ý : nếu chạy ant với tập tin testBuild.xml trên (nhấn chuột phải trên tập tin, chọn Run As> Ant Build) trong Eclipse mà bị lỗi sau (do sự không tương hợp giữa ant và jdk từ jdk6_32 tới jdk7_*) :
[junitreport] Processing D:\Eclipse\Workspace\XmlParse\report\TESTS-TestSuites.xml to C:\DOCUME~1\m.chu\LOCALS~1\Temp\null257800068 [junitreport] Loading stylesheet jar:file:/D:/Eclipse/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! Le premier argument pour la fonction Java 'replace' non static n'est pas une référence d'objet valide. [junitreport] : Error! Impossible de convertir le type de données 'void' en 'reference'. [junitreport] : Fatal Error! Impossible de compiler la feuille de style [junitreport] Failed to process D:\Eclipse\Workspace\XmlParse\report\TESTS-TestSuites.xml BUILD FAILED D:\Eclipse\Workspace\XmlParse\testBuild.xml:40: Errors while applying transformations: Fatal error during transformation
Ta có thể lách lỗi này trong khi chờ nó được sửa bằng cách tải về hai plug-in org.apache.xalan và org.apache.xml.serializer từ Eclipse Orbit project. Rồi lưu chúng vào thư mục ext của Java, cụ thể trong Windows là thư mục C:\Program Files\Java\jre7\lib\ext
Không có nhận xét nào:
Đăng nhận xét