首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >仅使用jar或war文件即可运行testng套件

仅使用jar或war文件即可运行testng套件
EN

Stack Overflow用户
提问于 2015-10-05 20:26:22
回答 2查看 1.1K关注 0票数 0

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

EN

回答 2

Stack Overflow用户

发布于 2015-10-06 14:35:03

我还没有机会使用maven,但也应该可以使用maven

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

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

代码语言:javascript
复制
<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来执行测试(使用目标ant run)来运行您的tests.If您的计划将它交给您的客户端,然后您可以创建一个简单的bat(windows)或shellscript(linux)来执行ant run命令,单击它,测试就会运行。

如果你有任何疑问,希望它能帮助you..Kindly回复

票数 0
EN

Stack Overflow用户

发布于 2015-10-08 23:39:22

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

因此,将这个作为插件添加到pom.xml中:

代码语言:javascript
复制
<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>

然后其他人可以使用以下命令将其作为依赖项提取:

代码语言:javascript
复制
 <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?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32948599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档