testng教程之testng.xml的配置和使用,以及参数传递

testng.xml

testng.xml是为了更方便的管理和执行测试用例,同时也可以结合其他工具

You can invoke TestNG in several different ways: 你可以用以下三种方式执行测试

  • With a testng.xml file           直接run as test suite
  • With ant                            使用ant
  • From the command line      从命令行
  • eclipse                              直接在eclipse执行


textng.xml 基本格式如下:

<test name="Regression1"><groups><run><exclude name="brokenTests"  /><include name="checkinTests"  /></run></groups>

<classes><class name="test.IndividualMethodsTest"><methods><include name="testMethod" /></methods></class></classes></test>

suite:定义一个测试套件,可包含多个测试用例或测试group

suite 里可以设置是否使用多线程:

  

parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.

parallel="tests": TestNG will run all
the methods in the same <test> tag in the same thread, but each
<test> tag will be in a separate thread. This allows you to group
all your classes that are not thread safe in the same <test> and
guarantee they will all run in the same thread while taking advantage of
TestNG using as many threads as possible to run your tests.

parallel="classes": TestNG will run all
the methods in the same class in the same thread, but each class will
be run in a separate thread.

parallel="instances": TestNG will run
all the methods in the same instance in the same thread, but two methods
on two different instances will be running in different threads.

  • parallel="classes"  每个测试用例class级别多线程
  • thread-count="3" 线程数为5,可同时执行3个case
  • preserve-order="true"  根据官方解释(If you want the classes and methods
    listed in this file to be run in an unpredictible order, set the preserve-order attribute to false)设置为false乱序执行,设置为true会按照你配置执行
  • Parameter标签传递参数
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="Suite" parallel="classes" thread-count="3"><test verbose="2" preserve-order="true" name="TestDebug">  <parameter name="driverName" value="chrome" /><classes><class name="com.dbyl.tests.Case1" /><class name="com.dbyl.tests.JDaddress" /><class name="com.dbyl.tests.UseCookieLogin" /><class name="com.dbyl.tests.MapTest" /><class name="com.dbyl.tests.loginTest" /></classes></test> <!-- Test --></suite> <!-- Suite -->

参数怎么使用?

这要在case里添加@Parameters 的annotations

如果有多个parameter,可以一次传入

package com.dbyl.tests;

import org.testng.annotations.Parameters;import org.testng.annotations.Test;/** * This Test for verify Parameter annotation * @author Young **/public class passParameter {

/**     *      * @param parameter1     * @param parameter2*/    @Parameters({"parameter1","parameter2"})    @Test(groups="parameter")public void parameter(String parameter1,int parameter2 )    {        System.out.println("parameter1 is "+parameter1 );        System.out.println("parameter2 is "+parameter2 );    }}

其中testng的配置文件为:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="Suite" parallel="classes" thread-count="5"><test verbose="2" preserve-order="true" name="TestDebug">  <parameter name="parameter1" value="parameter1" /><parameter name="parameter2" value="123" /><classes><class name="com.dbyl.tests.passParameter" /><class name="com.dbyl.tests.TestngExample" /></classes></test> <!-- Test --></suite> <!-- Suite -->

这里使用两个parameter标签,传递两个参数

执行结果如下:

[TestNG] Running:
C:\Users\workspace\Demo\Parametertestng.xml

[TestRunner] Starting executor for test TestDebug with time out:2147483647 milliseconds.
parameter1 is parameter1
parameter2 is 123

接下来尝试从ant命令行执行test suite

首先在 build配置文件加入:

<taskdef resource="testngtasks" classpath="testng.jar"/>

在target执行文件设置testng.xml路径

<xmlfileset dir="${basedir}" includes="Parametertestng.xml"/>

详细:

 1 <?xml version="1.0"?> 2 <project name="Demo" default="run" basedir="."> 3     <echo  message="Start selenium Grid" /> 4     <echo  message="import libs" /> 5     <path id="run.classpath"> 6         <fileset dir="${basedir}"> 7             <include name="lib/poi/*.jar" /> 8             <include name="lib/poi/lib/*.jar" /> 9             <include name="lib/testng.jar" />10             <include name="lib/sikuli-script.jar" />11             <include name="lib/*.jar" />12         </fileset>13         <fileset dir="${basedir}/lib/selenium">14             <include name="selenium-java-2.45.0.jar" />15             <include name="libs/*.jar" />16         </fileset>17     </path>18     <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="run.classpath" />19     <target name="clean">20         <delete dir="build"/>21     </target>22     <target name="compile" depends="clean">23         <echo message="mkdir"/>24         <mkdir dir="build/classes"/>25         <javac srcdir="src" destdir="build/classes" debug="on" encoding="UTF-8">26             <classpath refid="run.classpath"/>27         </javac>28     </target>29     <path id="runpath"> 30          <path refid="run.classpath"/> 31          <pathelement location="build/classes"/> 32        </path> 33     <target name="run" depends="compile">34         <testng  classpathref="runpath"  outputDir="test-output" 35             haltonfailure="true"36             useDefaultListeners="false"   37             listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.FailedReporter" >  38             <xmlfileset dir="${basedir}" includes="testng.xml"/>39             <jvmarg value="-Dfile.encoding=UTF-8" />40             <sysproperty key="org.uncommons.reportng.title" value="AutoMation TestReport" /> 41         </testng>42         </target>43         <target name="runTestng" depends="compile">44         <testng  classpathref="runpath"  outputDir="test-output" 45             haltonfailure="true"46             useDefaultListeners="false"   47             listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.FailedReporter" >  48             <xmlfileset dir="${basedir}" includes="Parametertestng.xml"/>49             <jvmarg value="-Dfile.encoding=UTF-8" />50             <sysproperty key="org.uncommons.reportng.title" value="AutoMation TestReport" /> 51         </testng>52     </target>53 </project>

接下在在命令行执行: ant runTestng

运行结果:

C:\Users\workspace\Demo>ant runTestng
Buildfile: C:\Users\workspace\Demo\build.xml
[echo] Start selenium Grid
[echo] import libs

clean:
[delete] Deleting directory C:\Users\Young\workspace\Demo\build

compile:
[echo] mkdir
[mkdir] Created dir: C:\Users\Young\workspace\Demo\build\classes
[javac] C:\Users\Young\workspace\Demo\build.xml:25: warning: ‘includeantrunt
ime‘ was not set, defaulting to build.sysclasspath=last; set to false for repeat
able builds
[javac] Compiling 21 source files to C:\Users\Young\workspace\Demo\build\cla
sses

runTestng:
[testng] [TestNG] Running:
[testng] C:\Users\workspace\Demo\Parametertestng.xml
[testng]
[testng] [TestRunner] Starting executor for test TestDebug with time out:2147
483647 milliseconds.
[testng] parameter1 is parameter1
[testng] parameter2 is 123
[testng] This is beforeClass method .The Value of a is: 1
[testng] This is beforeMethod method. The Value of a is: 2
[testng] This is Test method1 .The Value of a is: 3
[testng] This is AfterMethod Method .The Value of a is: 6
[testng] This is beforeMethod method. The Value of a is: 2
[testng] This is Test method2 .The Value of a is: 4
[testng] This is AfterMethod Method .The Value of a is: 6
[testng] This is AfterClass Method .The Value of a is: 5
[testng]
[testng] ===============================================
[testng] Suite
[testng] Total tests run: 3, Failures: 0, Skips: 0
[testng] ===============================================
[testng]
[testng] [TestNG] Time taken by [FailedReporter passed=3 failed=0 skipped=0]:
0 ms
[testng] [TestNG] Time taken by [email protected]:
120 ms

BUILD SUCCESSFUL
Total time: 5 seconds

原文:https://blog.csdn.net/xfxf996/article/details/81413560

原文地址:https://www.cnblogs.com/peachh/p/12013394.html

时间: 2024-08-04 13:36:08

testng教程之testng.xml的配置和使用,以及参数传递的相关文章

ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun

ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun OpenWrt-Yun OpenWrt-Yun是基于OpenWrt的一个Linux发行版.有所耳闻的读者应该听说他是一个使用在路由器上的操作系统.其实准确地说OpenWrt是一个嵌入式Linux发型版,它可以安装在各种嵌入式芯片中,如Arduino Yun.在本节中,将为大家介绍OpenWrt-Yun系统相关的知识. 使用SSH连接Arduino Yun SSH是Secure Shell的缩写,它是建立在应用层和

Solr基础教程之solrconfig.xml(三)

前面介绍过schema.xml的一些配置信息,本章介绍solrconfig.xml的配置,以及怎样安装smartcn分词器和IK分词器,并介绍主要的查询语法. 1. solr配置solrconfig.xml solrconfig.xml这个配置文件能够在你下载solr包的安装解压文件夹的D:\solr-4.10.4\example\solr\collection1\conf中找到,这个配置文件内容有点多,主要内容有:使用的lib配置,包括依赖的jar和Solr的一些插件;组件信息配置;索引配置和

OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务

OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务 OpenVAS基础知识 OpenVAS(Open Vulnerability Assessment System)是开放式漏洞评估系统,其核心部分是一个服务器.该服务器包括一套网络漏洞测试程序,可以检测远程系统和应用程序中的安全问题.OpenVAS不同与传统的漏洞扫描软件.所有的OpenVAS软件都是免费的,而且还采用了Nessus(一款强大的网络扫描工具)较早版本的一些开放插件.虽然Nessus很强大,但是该工具

TestNG基础教程 - TestNG.xml中的测试级别和常用注解执行顺序

根据testng.xml 文件配置, 测试级别为suite -> test -> class -> methods. test 对应testng.xml 中的test 标签, 而不是测试类里的@Test. 测试类里的@Test 对应 testng.xml中的methods. 创建TestCase 如TC3 运行效果 所以在使用@BeforeSuite,@BeforeTest,@BeforeClass,@BeforeMethod 等标签时, 它们的实际执行顺序也是suite -> t

Java - Test - TestNG: Idea 引入 testng.xml 自动生成插件

1. 概述 Idea 引入自动生成 testng.xml 插件 自动生成 testng.xml 2. 背景 testng 调试 调试 testng, 主要是这两种方法 ide 下直接执行测试 方法 类 ide 下执行 testng.xml testng.xml 概述 测试套件 的配置文件 问题 每次都要手写, 会比较麻烦 解决方案 备份一个, 每次按格式改 使用插件自动创建 写个程序自动生成 xml 感觉 方案2 是最方便的 3. 环境 ide idea 2018.2 4. 步骤 安装插件: C

Kail Linux渗透测试教程之Recon-NG框架

Kail Linux渗透测试教程之Recon-NG框架 信息收集 信息收集是网络攻击最重要的阶段之一.要想进行渗透攻击,就需要收集目标的各类信息.收集到的信息越多,攻击成功的概率也就越大.本章将介绍信息收集的相关工具. Recon-NG框架 Recon-NG是由python编写的一个开源的Web侦查(信息收集)框架.Recon-ng框架是一个强大的工具,使用它可以自动的收集信息和网络侦查.下面将介绍使用Recon-NG侦查工具. 启动Recon-NG框架,执行命令如下所示: root@kali

ios系类教程之用instruments来检验你的app

ios系类教程之用instruments来检验你的app 为了节省大家的时间,提供一个演示的Demo给大家.代码传送门.下载后解压然后用xcode打开.编译运行APP后 然后在搜索框内输入任意词汇,点击结果你会看到下面的结果 正如你所见的,这个app很简单.程序其实调用的是Flickr的API,通过app顶部的搜索框执行搜索后在下面的tableview显示你搜索的搜索词,搜索词后面的括号内有搜索结果的个数,点击此行进入一个略所图的结果列表页面 如上图. 点击其中一行 进入图像的大图模式,在这个页

kali linux 系列教程之metasploit 连接postgresql可能遇见的问题

kali linux 系列教程之metasploit 连接postgresql可能遇见的问题 文/玄魂   目录 kali linux 下metasploit 连接postgresql可能遇见的问题................................ 1 前言............................................................................................................... 1

gulp教程之gulp-less

gulp教程之gulp-less Ooo_My_God发表于2015-02-24 分类:构建工具 阅读(4518) 评论(19) 简介: 使用gulp-less插件将less文件编译成css,当有less文件发生改变自动编译less,并保证less语法错误或出现异常时能正常工作并提示错误信息. 1.安装nodejs/全局安装gulp/项目安装gulp/创建package.json和gulpfile.js文件 1.1.gulp基本使用还未掌握?请参看: gulp详细入门教程 1.2.本示例目录结构