为什么我的测试用例总是运行两次?

时间:2013-11-26 10:35:34

标签: selenium ant webdriver selenium-webdriver

我尝试使用Ant配置webdriver + testNG。 这是一个简单的测试用例,启动FF并打开http://www.google.com。但是当我用ant执行build.xml时,测试用例执行了两次! 这对我来说很难过,我怀疑“Ant套件”中有什么问题,而且我不知道如何禁用它。我的代码就是这样。

Eclipse控制台输出:

    init:
    compile:
    [javac] D:\Workspace\HelloAnt\build.xml:23: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
run_tests:
     [echo] running tests
   [testng] ...
   [testng] ... TestNG 6.8beta 20120825_1010 by C?dric Beust (cedric@beust.com)
   [testng] ...
   [testng] [TestNG] Running:
   [testng]   D:\Workspace\HelloAnt\src\example\testng.xml
   [testng] ===============================================
   [testng] reportng demo
   [testng] Total tests run: 1, Failures: 0, Skips: 0
   [testng] ===============================================
   [testng] [TestNG] Running:
   [testng]   Ant suite
   [testng] ===============================================
   [testng] Ant suite
   [testng] Total tests run: 1, Failures: 0, Skips: 0
   [testng] ===============================================
     [echo] zip
      [zip] Building zip: D:\Workspace\HelloAnt\test-output\html.zip
BUILD SUCCESSFUL
Total time: 38 seconds

testng.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="reportng demo" verbose="2">
<test name="test">
        <classes>
            <class name="example.NewTest">
                <methods>
                    <include name="setup"/>
                    <include name="verifyTitle"/>
            <include name="clean"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

和build.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<project name="HelloReportng" default="run_tests" basedir=".">
    <property name="src" value="src" />
    <property name="dest" value="classes" />
    <property name="hello_jar" value="NewTest.jar" />
    <property name="lib.dir" value="lib" />
    <property name="output.dir" value="test-output"/>
    <property name="testng.dir" value="D:/Workspace/testng.jar"/>

    <!-- import testng jar package -->
    <taskdef resource="testngtasks" classpath="${testng.dir}"/>

    <path id="test.classpath">
        <fileset dir="${lib.dir}" includes="*.jar"/>
         <pathelement path="${dest}"/>
    </path>

    <target name="init">
       <mkdir dir="${dest}" />
    </target>

    <target name="compile" depends="init">    
       <javac srcdir="${src}" destdir="${dest}" >
           <classpath refid="test.classpath"/>           
       </javac>
    </target>

    <target name="compress" depends="compile">
       <jar jarfile="${hello_jar}" basedir="${dest}" />
    </target>

    <target name="run" depends="compress">
       <java classname="example.NewTest"   >
           <classpath refid="test.classpath"/>
       </java>      
    </target> 

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

    <target name="return" depends="clean">
        <ant target="clean" />
        <ant target="run" />
    </target>

    <!-- run tests -->
    <target name="run_tests" depends="compile">
        <echo>running tests</echo>
        <testng classpathref="test.classpath" outputdir="${output.dir}" haltonfailure="true" usedefaultlisteners="false"
         listeners="org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter"  failureproperty="test.failed" > 
        <classfileset dir="${dest}" includes="**/*.class" />
        <sysproperty key="org.uncommons.reportng.title" value="All Page Tests"></sysproperty>
        <sysproperty key="org.uncommons.reportng.escape-output" value="off"></sysproperty>
        <xmlfileset dir="${src}/example" >
            <include name="testng.xml" />
        </xmlfileset>
        </testng>
        <echo>zip</echo>
        <zip destfile="${output.dir}/html.zip" basedir="test-output/html" includes="**/*"></zip>
        <fail message="test failed.." if="test.failed" />
    </target>

</project>

1 个答案:

答案 0 :(得分:1)

为什么你有两个目标 - run_tests并运行?我认为这是运行目标,这是问题所在。删除该目标,看看它是否有效。