2015-10-05 59 views
0

我使用selenium webdriver和java創建了一個testng測試。現在我不想將我的代碼分享給不同的用戶,但我希望我的代碼可以由使用jar或war文件的不同用戶運行。 任何人都可以幫我解決這個問題。是否有可能在不共享testNG java代碼的情況下運行測試?使用jar或war文件運行testng套件

+0

測試代碼不屬於像JAR或WAR這樣的可執行程序包。 – duffymo

+0

這是有用的:http://www.toolsqa.com/selenium-webdriver/testng-testcase/? –

+0

Duffymo,所以我無法運行沒有代碼的測試?有什麼出路,我不想分享我的完整框架。 Luke,我知道testng測試用例,但想知道我是否可以使用jar或war文件運行我的測試。 –

回答

0

我沒有得到機會與Maven的工作,但它也有可能需要maven

讓我告訴你用螞蟻

採取一個例子想法假設你正在使用運行測試螞蟻與下面的build.xml(根據您的需求樣本文件更改)文件

<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編譯來編譯你的測試),你會在你的項目文件夾內的build文件夾中獲得類文件,現在你可以刪除src文件夾(你的java文件)並使用ant run來執行測試(使用目標螞蟻運行)來運行你的測試。如果你計劃給它的客戶端,那麼你可以做一個簡單的蝙蝠(Windows)或貝殼(Linux)執行命令螞蟻運行和點擊它,測試將運行

希望它可以幫助你。如果你有任何疑問,請回復

+0

@ Md.Zishan Paya上述解決方案是否適合您? – Vicky

0

正如@vicky建議的,看看Maven,因爲它可以讓你用生產WAR/JAR和測試打包成測試JAR。特別是在test-jar JAR type上,您可以用JAR標記。使用

<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> 

更多的信息

所以添加爲一個插件的pom.xml :How do I install a test-jar in maven?