【发布时间】:2017-07-21 06:46:21
【问题描述】:
我是 Maven 和 Jenkins 的新手。请帮助我满足以下要求。
1.我有多个测试类,即 tc001、tc002、tc003、tc004 等。(使用 testng)
2.我正在从单个运行器类运行这些类(类似于 testNG.xml 或这个 testNG .XML 是由包含所有类的代码编写的),在这里我可以通过控制来管理哪些类要运行或哪些不运行来自外部 Excel 文件。
3.我的问题是我可以使用 maven 和 Jenkins 运行这个场景吗?
- 或者我可以使用 Jenkins 运行该 testNG 运行程序类吗?这里我需要控制要执行哪些类或哪些不是来自外部文件。
请建议我解决这个问题..
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MobileTesting</groupId>
<artifactId>com.maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com.maven</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</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>
<goal>exec</goal>
</goals>
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>com.demo.toolsqa.runner.TestCaseRunnerTestNG</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
</dependency>
</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 {
public static List<XmlClass> getclassList(String TCName) {
XmlClass classlist = null;
switch (TCName) {
case "LTI_ENV":
classlist = new XmlClass(TC_001.class);
break;
case "LTQ_ENV":
classlist = new XmlClass(TC_002.class);
break;
case "INT_ENV":
classlist = new XmlClass(ToolsQARegistration.class);
break;
}
List<XmlClass> class_list = new ArrayList<XmlClass>();
class_list.add(classlist);
return class_list;
}
public static void main(String[] args) throws Exception {
XmlSuite mainsuite = new XmlSuite();
mainsuite.setName("SmokeTest Suite");
mainsuite.setFileName("SmokeTest.xml");
mainsuite.setParallel("false");
//mainsuite.addListener("utility.Listener");
XmlTest mainTest;
String sTestName="LTI_ENV";
mainTest = new XmlTest(mainsuite);
mainTest.setName("Test CaseName :" + sTestName);
mainTest.setPreserveOrder("True");
mainTest.setXmlClasses(getclassList(sTestName));
//Reporter.log("total number of TC included in test :"+ic-1);
TestNG runner=new TestNG();
List<XmlSuite>suites=new ArrayList<XmlSuite>();
suites.add(mainsuite);
runner.setXmlSuites(suites);
runner.run();
}
}
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 -->
【问题讨论】:
-
不需要测试运行器类?为什么使用 Excel 文件?只需运行它们,您就可以确定一切正常。此外,只需使用命名约定,具体取决于您在哪个阶段运行测试(集成测试、测试)? maven-surefire-plugin 或 maven-failsafe-plugin...
-
@khmarbaise 我想以受控方式运行测试用例,例如要执行哪些测试用例或不使用外部文件..
标签: java maven jenkins selenium-webdriver testng