0

我已经安装了 ant,我已经在系统变量中设置了 ANT_HOME。在系统环境变量中设置 JAVA_HOME。将 tools.jar 文件复制到“JRE/lib”文件夹中。现在我应该在哪里创建 build.xml,在哪个文件夹中。我有基本的 build.xml。在这个 build.xml 中,我在哪里提到要运行的脚本。例如,我的测试脚本在 google 上搜索了一些东西然后退出。我正在使用 java,testng 在 windows7 上编写我的脚本

<?xml version="1.0"?>
<project name="sampleProject" basedir="." default="jar">
    <property name="src" value="ant-source"/>
    <property name="output" value="bin"/>

    <target name="compile" depends="create">
        <javac destdir="bin">
            <src path="${src}"/>
            <classpath refid="java"/>
        </javac>
    </target>

    <target name="jar" depends="compile">
        <jar destfile="test.jar">
            <fileset dir="bin"/>
        </jar>
    </target>


    <target name="clean">
        <delete dir="${output}"/>
    </target>

    <target name="create" depends="clean">
        <mkdir dir="${output}"/>
    </target>

    <path id="java">
        <fileset dir="D:JarsHibernate">
            <include name="*.jar"/>
        </fileset>
    </path>
</project>
4

2 回答 2

0

这是我为从命令行执行自动化测试而创建的 build.xml 文件。希望它可以帮助你。

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>

<project name="WebTestProject" default="start" basedir=".">  

<!-- ========== Initialize Properties =================================== -->
    <property environment="env"/>
    <property file="./environmentConfig.properties"/>

    <property name="ws.home" value="${basedir}"/>
    <property name="test.dest" value="${ws.home}/build"/>
    <property name="test.src" value="${ws.home}/src"/>
    <property name="browser" value="/usr/bin/google-chrome"/>
    <property name="mail_body_file" value="${basedir}/email_body.txt"/>
    <property name="buildID" value="build123"/>
    <property name="sendmailscript_path" value="${basedir}/sendmail.sh"/>
    <property name="mail_subject" value="Automated_test_execution_of_${buildID}"/>
    <property name="mail_recipient" value="username@domain.com"/>


    <!-- ====== For setting the classpath ====  -->
    <target name="setClassPath" unless="test.classpath">
        <path id="classpath_jars">
            <fileset dir="${ws.home}/lib" includes="*.jar"/>
        </path>
         <pathconvert pathsep=":" property="test.classpath" refid="classpath_jars"/>
    </target>




    <!-- ============ Initializing other stuff ===========  -->
    <target name="init" depends="setClassPath">

    <taskdef name="testng" classpath="${test.classpath}" classname="org.testng.TestNGAntTask" />

    </target>



    <!-- cleaning the destination folders -->
    <target name="clean">
        <delete dir="${test.dest}"/>
    </target>


    <!-- target for compiling the java files -->
    <target name="compile" depends="init, clean" > 
        <delete includeemptydirs="true" quiet="true">
            <fileset dir="${test.dest}" includes="**/*"/>
        </delete>
        <echo message="making directory..."/>
        <mkdir dir="${test.dest}"/>

        <echo message="compiling..."/>
        <javac 
            debug="true" 
            destdir="${test.dest}" 
            srcdir="${test.src}" 
            target="1.6" 
            classpath="${test.classpath}"
            includeantruntime="true"
        >
        </javac>
      </target>


    <!-- run -->
    <target name="run" depends="compile">
        <testng classpath="${test.classpath}:${test.dest}" suitename="InitialTestSuite">
           <xmlfileset dir="${ws.home}" includes="testng.xml"/> 
        </testng>

    </target>



    <!--  ========== Generating reports using XSLT utility ==============    -->
    <target name="testng-xslt-report">
                <delete dir="${basedir}/testng-xslt">
                </delete>
                <mkdir dir="${basedir}/testng-xslt">
                </mkdir>
                <echo message="Generating XSLT report..."/>
                <xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${basedir}/testng-xslt/index.html">
                    <param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir" />
                    <param expression="true" name="testNgXslt.sortTestCaseLinks" />
                    <param expression="FAIL,SKIP,PASS,CONF,BY_CLASS" name="testNgXslt.testDetailsFilter" />
                    <param expression="true" name="testNgXslt.showRuntimeTotals" />
                    <classpath refid="classpath_jars">
                    </classpath>
                </xslt>
                <echo message="XSLT report generated"/>
    </target>


    <!-- ====== Fetch the generated report's index.html, open a chrome browser and show the report.========= -->
     <target name="runAndViewReport" depends="run, testng-xslt-report"> 
            <exec executable="${browser}" spawn="yes"> 
           <arg line="'${basedir}/testng-xslt/index.html'" /> 
          </exec>
        <echo message="Opening up the report in a browser..."/>
     </target>


    <!-- Starting point of the execution, should be dependent on target "run".

    Target sequence will be:
    start (not_execute) ==> run (not_execute) ==> compile (not_execute) ==> init (execute) ==> clean (execute)
    start (execute) <== testng-xslt-report (execute) <== run (execute) <== compile (execute) <==

    Suitable for ANT 1.7. Currently using this ====================== -->


    <target name="start" depends="run, testng-xslt-report">
        <tstamp prefix="getTime">
                <format property="TODAY" pattern="MM-dd-yyyyhhmmaa"/>
        </tstamp>
        <echo message="sending report as mail...."/>
        <property name="execution_time" value="${buildID}_${getTime.TODAY}"/>
        <property name="dest_file" value="/home/Reports/${execution_time}.zip"/>
        <zip destfile="/home/${execution_time}.zip" 
            basedir="${basedir}/testng-xslt"/>
        <property name="report_attachment_file" value="${dest_file}"/>
        <exec executable="${sendmailscript_path}" newenvironment="false"> 
                    <arg value="${mail_subject}"/>
                    <arg value="${mail_recipient}"/>
                    <arg value="${report_attachment_file}"/>
                    <arg value="${mail_body_file}"/> 
        </exec>

    </target>


</project>
于 2013-04-04T06:08:33.317 回答
0

您应该考虑任何来自 ant的“执行任务”任务。

于 2013-04-03T16:36:22.390 回答