2017-03-01 70 views
-2

我是新來的maven和詹金斯.. Plz幫助我滿足以下要求。如何用jenkins運行自定義的testNG亞軍運動員

1.I有多個測試類即TC001,TC002,TC003,TC004等。(用TestNG)

2.I正在運行來自單個轉輪類這些類(類似的testng.xml或此testNG .XML是由包含所有類的代碼編寫的),在這裏我可以通過從外部Excel文件進​​行控制來管理要運行哪些類或哪些類不能運行。

3.my問題是我可以使用maven和Jenkins運行這個場景嗎?

  1. 或者我可以使用Jenkins運行testNG runner類..在這裏我需要控制要執行哪些類或哪些不是來自外部文件。

請建議我這種情況的解決方案..

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
 
\t <modelVersion>4.0.0</modelVersion> 
 

 
\t <groupId>MobileTesting</groupId> 
 
\t <artifactId>com.maven</artifactId> 
 
\t <version>0.0.1-SNAPSHOT</version> 
 
\t <packaging>jar</packaging> 
 

 
\t <name>com.maven</name> 
 
\t <url>http://maven.apache.org</url> 
 

 
\t <properties> 
 
\t \t <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
 
\t </properties> 
 
\t <build> 
 
\t \t <plugins> 
 
\t \t \t <plugin> 
 
\t \t \t \t <groupId>org.apache.maven.plugins</groupId> 
 
\t \t \t \t <artifactId>maven-surefire-plugin</artifactId> 
 
\t \t \t \t <version>2.19.1</version> 
 
\t \t \t \t <configuration> 
 
\t \t \t \t \t <suiteXmlFiles> 
 
\t \t \t \t \t \t <suiteXmlFile>testng.xml</suiteXmlFile> 
 
\t \t \t \t \t </suiteXmlFiles> 
 
\t \t \t \t </configuration> 
 
\t \t \t </plugin> 
 
\t \t \t <plugin> 
 
\t \t \t \t <groupId>org.apache.maven.plugins</groupId> 
 
\t \t \t \t <artifactId>maven-compiler-plugin</artifactId> 
 
\t \t \t \t <version>3.6.1</version> 
 
\t \t \t \t <configuration> 
 
\t \t \t \t \t <fork>true</fork> 
 
\t \t \t \t \t <encoding>iso-8859-1</encoding> 
 
\t \t \t \t \t <source>1.8</source> 
 
\t \t \t \t \t <target>1.8</target> 
 
\t \t \t \t </configuration> 
 
\t \t \t </plugin> 
 
\t \t \t 
 
\t <plugin> 
 
       <groupId>org.codehaus.mojo</groupId> 
 
       <artifactId>exec-maven-plugin</artifactId> 
 
       <version>1.5.0</version> 
 
       <executions> 
 
        <execution> 
 
         <id>first-execution</id> 
 
         <phase>test</phase> 
 
         <goals> 
 
          <goal>java</goal> 
 
          <goal>exec</goal> 
 
         </goals> 
 
         <configuration> 
 
          <includePluginDependencies>true</includePluginDependencies> 
 
          <mainClass>com.demo.toolsqa.runner.TestCaseRunnerTestNG</mainClass> 
 
         </configuration> 
 
        </execution> 
 
       </executions> 
 
      </plugin> 
 
      \t \t 
 
\t \t </plugins> 
 
\t </build> 
 
\t <dependencies> 
 
\t \t <dependency> 
 
\t \t \t <groupId>junit</groupId> 
 
\t \t \t <artifactId>junit</artifactId> 
 
\t \t \t <version>3.8.1</version> 
 
\t \t \t <scope>test</scope> 
 
\t \t </dependency> 
 

 
\t \t <dependency> 
 
\t \t \t <groupId>com.relevantcodes</groupId> 
 
\t \t \t <artifactId>extentreports</artifactId> 
 
\t \t \t <version>2.41.1</version> 
 
\t \t </dependency> 
 
\t \t <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.seleniumhq.selenium</groupId> 
 
\t \t \t <artifactId>selenium-java</artifactId> 
 
\t \t \t <version>3.0.1</version> 
 
\t \t </dependency> 
 

 
\t \t <!-- https://mvnrepository.com/artifact/log4j/log4j --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>log4j</groupId> 
 
\t \t \t <artifactId>log4j</artifactId> 
 
\t \t \t <version>1.2.17</version> 
 
\t \t </dependency> 
 
    
 
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.apache.poi</groupId> 
 
\t \t \t <artifactId>poi</artifactId> 
 
\t \t \t <version>3.15</version> 
 
\t \t </dependency> 
 

 
\t \t <!-- https://mvnrepository.com/artifact/org.testng/testng --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.testng</groupId> 
 
\t \t \t <artifactId>testng</artifactId> 
 
\t \t \t <version>6.10</version> 
 
\t \t </dependency> 
 
\t </dependencies> 
 

 
</project> 
 

[![

my runner main class 
 

 
/** 
 
* 
 
*/ 
 
package com.demo.toolsqa.runner; 
 

 
import java.util.ArrayList; 
 
import java.util.List; 
 

 
import org.testng.Reporter; 
 
import org.testng.xml.XmlClass; 
 
import org.testng.xml.XmlSuite; 
 
import org.testng.xml.XmlTest; 
 

 
import com.beust.testng.TestNG; 
 
import com.demo.toolsqa.tests.TC_001; 
 
import com.demo.toolsqa.tests.TC_002; 
 
import com.demo.toolsqa.tests.ToolsQARegistration; 
 

 

 
/** 
 
* @author Afsar 
 
* 
 
*/ 
 
public class TestCaseRunnerTestNG { 
 

 
\t public static List<XmlClass> getclassList(String TCName) { 
 

 
\t \t XmlClass classlist = null; 
 
\t \t switch (TCName) { 
 

 
\t \t case "LTI_ENV": 
 
\t \t \t classlist = new XmlClass(TC_001.class); 
 
\t \t \t break; 
 
\t \t case "LTQ_ENV": 
 
\t \t \t classlist = new XmlClass(TC_002.class); 
 
\t \t \t break; 
 
\t \t case "INT_ENV": 
 
\t \t \t classlist = new XmlClass(ToolsQARegistration.class); 
 
\t \t \t break; 
 
\t \t } 
 

 
\t \t List<XmlClass> class_list = new ArrayList<XmlClass>(); 
 
\t \t class_list.add(classlist); 
 
\t \t return class_list; 
 

 
\t } 
 

 
\t public static void main(String[] args) throws Exception { 
 

 
\t \t XmlSuite mainsuite = new XmlSuite(); 
 
\t \t mainsuite.setName("SmokeTest Suite"); 
 
\t \t mainsuite.setFileName("SmokeTest.xml"); 
 
\t \t mainsuite.setParallel("false"); 
 
\t \t //mainsuite.addListener("utility.Listener"); 
 

 
\t \t XmlTest mainTest; 
 
\t \t String sTestName="LTI_ENV"; 
 

 
\t \t \t \t mainTest = new XmlTest(mainsuite); 
 
\t \t \t \t mainTest.setName("Test CaseName :" + sTestName); 
 
\t \t \t \t mainTest.setPreserveOrder("True"); 
 
\t \t \t \t mainTest.setXmlClasses(getclassList(sTestName)); 
 
\t \t 
 
\t \t //Reporter.log("total \t number of TC included in test :"+ic-1); 
 
\t \t 
 
\t \t 
 
\t \t TestNG runner=new TestNG(); 
 
\t \t List<XmlSuite>suites=new ArrayList<XmlSuite>(); 
 
\t \t suites.add(mainsuite); 
 
\t \t runner.setXmlSuites(suites); 
 
\t \t runner.run(); \t \t 
 
\t } 
 

 
} 
 

 
and testng.xml is 
 

 
<?xml version="1.0" encoding="UTF-8"?> 
 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
 
<suite name="Suite"> 
 
    <test name="Test"> 
 
    <classes> 
 
     <class name="com.demo.toolsqa.tests.TC_002"/> 
 
     <class name="com.demo.toolsqa.tests.TC_001"/> 
 
    </classes> 
 
    </test> <!-- Test --> 
 
</suite> <!-- Suite -->

] 1] 1

+0

否需要測試跑步者級別?爲什麼使用Excel文件?只需運行它們,你確定一切正常。此外,只要使用命名約定,取決於你正在運行測試的哪個階段(集成測試,測試)? maven-surefire-plugin或maven-failsafe-plugin ... – khmarbaise

+0

@khmarbaise我想以受控的方式運行測試用例,比如要執行哪個測試用例或者不用外部文件.. –

回答

0

以下是可以在pom.xml分別用於提TestNG的xml和澆道/發射類的路徑中的兩個插件。

插件包括 「的testng.xml」 文件: -

<build> 
    <plugins>  
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.5</version> 
      <configuration> 
       <suiteXmlFiles> 
        <suiteXmlFile>testng.xml</suiteXmlFile> 
       </suiteXmlFiles> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

插件包括 「主類」 的文件: -

 <build> 
     <sourceDirectory>src</sourceDirectory> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.6.0</version> 
       <configuration> 
        <fork>true</fork> 
        <encoding>iso-8859-1</encoding> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>exec-maven-plugin</artifactId> 
       <version>1.5.0</version> 
       <executions> 
        <execution> 
         <id>first-execution</id> 
         <phase>test</phase> 
         <goals> 
          <goal>java</goal> 
         </goals> 
         <configuration> 
          <includePluginDependencies>true</includePluginDependencies> 
          <mainClass>com.selenium.common.Launcher</mainClass> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

詹金斯配置可以被設置爲:

建立:

根POM:的pom.xml

目標和選項:全新安裝

讓我知道,如果你面對任何問題。謝謝 !

+0

非常感謝Sameer!在我的項目中實現這個插件配置後,我會讓你知道.. –

+0

嗨Sameer,我試過這個,但得到錯誤「java.lang.ClassNotFoundException:com.demo.toolsqa.runner.TestCaseRunnerTestNG」在上面附加mt pom.xml。 –

+0

@Afsar:你的'TestCaseRunnerTestNG'類是否包含'main'方法? 也有你在POM中提到'sourceDirectory'嗎?你的'TestCaseRunnerTestNG'類出現在同一個目錄中嗎? –