【问题标题】:why my test case under ant always runs twice?为什么我在ant下的测试用例总是运行两次?
【发布时间】:2013-11-26 10:35:33
【问题描述】:

我正在尝试配置 webdriver+testNG 与 Ant 一起使用。 这是一个简单的测试用例,启动FF并打开http://www.google.com。但是当我用ant执行build.xml时,测试用例执行了两次! 我很困惑,我怀疑“Ant suite”有问题,我不知道如何禁用它。这是我的代码。

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>

【问题讨论】:

    标签: selenium ant webdriver selenium-webdriver


    【解决方案1】:

    为什么你有两个目标 - run_tests 和 run?我认为这里的问题是运行目标。移除那个目标,看看它是否有效。

    【讨论】:

    • 感谢您的回复。删除目标“run”后,仍然执行了两次。
    • 已解决。从目标“run_tests”中删除了代码“',它按预期工作:) 但我仍然不清楚为什么这段代码会导致这个问题发生。
    • 好的,我完全理解这个问题。标签'classfileset'仅在文件testng.xml不存在时使用。它与标签'xmlfileset'重复功能,所以它运行两次!
    猜你喜欢
    • 2021-09-10
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2012-09-12
    • 2016-07-13
    • 1970-01-01
    相关资源
    最近更新 更多