【问题标题】:How to run custom testNG runner with maven by jenkinsjenkins 如何使用 maven 运行自定义 testNG runner
【发布时间】:2017-07-21 06:46:21
【问题描述】:

我是 Maven 和 Jenkins 的新手。请帮助我满足以下要求。

1.我有多个测试类,即 tc001、tc002、tc003、tc004 等。(使用 testng)

2.我正在从单个运行器类运行这些类(类似于 testNG.xml 或这个 testNG .XML 是由包含所有类的代码编写的),在这里我可以通过控制来管理哪些类要运行或哪些不运行来自外部 Excel 文件。

3.我的问题是我可以使用 maven 和 Jenkins 运行这个场景吗?

  1. 或者我可以使用 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 -->

]1]1

【问题讨论】:

  • 不需要测试运行器类?为什么使用 Excel 文件?只需运行它们,您就可以确定一切正常。此外,只需使用命名约定,具体取决于您在哪个阶段运行测试(集成测试、测试)? maven-surefire-plugin 或 maven-failsafe-plugin...
  • @khmarbaise 我想以受控方式运行测试用例,例如要执行哪些测试用例或不使用外部文件..

标签: java maven jenkins selenium-webdriver testng


【解决方案1】:

以下是两个插件,可以在pom.xml中使用,分别提到testng xml和runner/launcher类的路径。

包含“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>

Jenkins 配置可以设置为:

构建:

根 POM:pom.xml

目标和选项:全新安装

如果您遇到任何问题,请告诉我。谢谢!

【讨论】:

  • 非常感谢萨米尔!!在我的项目中实现这个插件配置后,我会告诉你的..
  • 嗨,Sameer,我已经尝试过了,但出现以下错误“java.lang.ClassNotFoundException: com.demo.toolsqa.runner.TestCaseRunnerTestNG”我在上面附加了 mt pom.xml。
  • @Afsar:你的TestCaseRunnerTestNG 类是否包含main 方法?你在 POM 中也提到过sourceDirectory 吗?您的TestCaseRunnerTestNG 课程是否存在于同一目录中?
  • 嗨Sameer,是的,它包含主要方法..附加代码......但我认为我没有在pom中包含类路径。 Xml .. 作为 maven 的新手.. 你能帮帮我吗
  • 您可以选择任一选项。要么按照#1 中的建议包含TestNG,您不需要主类,要么按照#2 中的建议使用包声明主类,其中主类将读取TestNG xml 并执行脚本。我建议您尝试使用mainClass 节点值为com.demo.toolsqa.runner.TestCaseRunnerTestNG 的选项#2。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 2012-09-23
  • 2017-05-01
相关资源
最近更新 更多