【问题标题】:Run testng suite just using jar or war file仅使用 jar 或 war 文件运行 testng 套件
【发布时间】:2015-10-05 12:26:22
【问题描述】:

我使用 selenium webdriver 和 java 创建了一个 testng 测试。现在我不想将我的代码分享给不同的用户,但我希望我的代码由不同的用户使用 jar 或 war 文件运行。 谁能帮我解决这个问题。是否可以在不共享 testNG java 代码的情况下运行测试?

【问题讨论】:

  • 测试代码不属于 JAR 或 WAR 等可执行包。
  • Duffymo,所以我不能在没有代码的情况下运行我的测试?有没有办法,我不想分享我的完整框架。卢克,我知道 testng 测试用例,但想知道我是否可以使用 jar 或 war 文件运行我的测试。
  • 使用 ant 等构建工具编译您的 testng 测试,然后删除 src 文件(java 文件)并使用 ant 运行到编译期间生成的类文件。您现在可以将项目发送到客户端只有 .class 文件可用。如果您需要更多详细信息,请返回
  • Vicky 听起来很有趣,能详细介绍一下这个 ant 部分吗? maven也可以吗,如果可以,请提供更多详细信息?

标签: java selenium-webdriver testng


【解决方案1】:

我还没有机会与 maven 合作,但 maven 也应该可以工作

让我通过一个使用ant的例子告诉你这个想法

假设您正在使用带有以下 build.xml(根据您的需要更改示例文件)文件的 ant 运行测试

<project name="TestNG Demo" default="clean" basedir=".">  

<!-- ========== Initialize Properties =================================== -->
    <property environment="env"/>

    <property name="ws.home" value="${basedir}"/>
    <property name="ws.jars" value="${ws.home}/Jars"/>
    <property name="test.dest" value="${ws.home}/build"/>
    <property name="test.src" value="${ws.home}/src"/>
    <property name="ng.result" value="test-output"/>

    <target name="setClassPath" unless="test.classpath">
        <path id="classpath_jars">
            <fileset dir="${ws.jars}" includes="**/*.jar"/>
            <pathelement path="${ws.jars}"/>
        </path>
        <pathconvert pathsep=":" 
            property="test.classpath" 
            refid="classpath_jars"/>
    </target>

    <target name="init" depends="setClassPath">
        <tstamp>
            <format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
        </tstamp>
        <condition property="ANT" 
            value="${env.ANT_HOME}/bin/ant.bat" 
            else="${env.ANT_HOME}/bin/ant">
                    <os family="windows" />
        </condition>
        <taskdef name="testng" classpath="${test.classpath}"
               classname="org.testng.TestNGAntTask" />

    </target>

    <!-- all -->
    <target name="all">
    </target>

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

    <!-- compile -->
    <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="classpath------: ${test.classpath}"/>
        <echo message="compiling..."/>
        <javac 
            includeantruntime="false"
            debug="true" 
            destdir="${test.dest}" 
            srcdir="${test.src}" 
            target="1.6" 
            classpath="${test.classpath}"
        >
        </javac>
      </target>

    <!-- build -->
    <target name="build" depends="init">
    </target>

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

    <target name="usage">
        <echo>
            ant run will execute the test
        </echo>
    </target>

    <path id="test.c">
            <fileset dir="${ws.jars}" includes="*.jar"/>
    </path>

      <target name="makexsltreports">
            <mkdir dir="${ws.home}/XSLT_Reports/output"/>

            <xslt in="${ng.result}/testng-results.xml" style="src/demo/testng-results.xsl"
                  out="${ws.home}/XSLT_Reports/output/index.html" classpathref="test.c" processor="SaxonLiaison">
                <param name="testNgXslt.outputDir" expression="${ws.home}/XSLT_Reports/output/"/>
                <param name="testNgXslt.showRuntimeTotals" expression="true"/>
            </xslt>
        </target>

    <!-- ****************** targets not used ****************** -->

</project>

现在在测试编译之后(使用目标 ant compile 来编译你的测试),你将在你的项目文件夹中的 build 文件夹中获得类文件,现在你可以删除 src 文件夹(你的 java 文件)并使用 ant run 来执行测试(使用目标蚂蚁运行)来运行你的测试。如果你打算给它你的客户端,那么你可以制作一个简单的bat(windows)或shellscript(linux)来执行命令ant run,然后点击它测试将运行

希望对您有所帮助..如果您有任何疑问,请回复

【讨论】:

    【解决方案2】:

    正如@vicky 建议的那样,看看 Maven,因为它允许您将项目与生产 WAR/JAR 以及打包到测试 JAR 中的测试一起打包。特别是在test-jar JAR type,您可以标记 JAR。

    所以把它作为插件添加到 pom.xml:

    <build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-jar-plugin</artifactId>
               <version>2.2</version>
    
               <executions>
                   <execution>
                       <goals>
                           <goal>test-jar</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
        </plugins>
    </build>
    

    然后其他人可以将其作为依赖项使用:

     <dependency>
    <groupId>com.foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.2.3</version>
    <type>test-jar</type>
    <scope>test</scope>
    

    这里有更多信息:How do I install a test-jar in maven?

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 2020-08-30
      • 2019-06-12
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多