昨天学习了一下testng基础教程,http://www.cnblogs.com/tobecrazy/p/4579414.html

昨天主要学习的是testng 的annotation基本用法和生命周期,今天学习一下如何使用testng.xml和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"/>

详细:

 <?xml version="1.0"?>
<project name="Demo" default="run" basedir=".">
<echo message="Start selenium Grid" />
<echo message="import libs" />
<path id="run.classpath">
<fileset dir="${basedir}">
<include name="lib/poi/*.jar" />
<include name="lib/poi/lib/*.jar" />
<include name="lib/testng.jar" />
<include name="lib/sikuli-script.jar" />
<include name="lib/*.jar" />
</fileset>
<fileset dir="${basedir}/lib/selenium">
<include name="selenium-java-2.45.0.jar" />
<include name="libs/*.jar" />
</fileset>
</path>
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="run.classpath" />
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile" depends="clean">
<echo message="mkdir"/>
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes" debug="on" encoding="UTF-8">
<classpath refid="run.classpath"/>
</javac>
</target>
<path id="runpath">
<path refid="run.classpath"/>
<pathelement location="build/classes"/>
</path>
<target name="run" depends="compile">
<testng classpathref="runpath" outputDir="test-output"
haltonfailure="true"
useDefaultListeners="false"
listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.FailedReporter" >
<xmlfileset dir="${basedir}" includes="testng.xml"/>
<jvmarg value="-Dfile.encoding=UTF-8" />
<sysproperty key="org.uncommons.reportng.title" value="AutoMation TestReport" />
</testng>
</target>
<target name="runTestng" depends="compile">
<testng classpathref="runpath" outputDir="test-output"
haltonfailure="true"
useDefaultListeners="false"
listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.FailedReporter" >
<xmlfileset dir="${basedir}" includes="Parametertestng.xml"/>
<jvmarg value="-Dfile.encoding=UTF-8" />
<sysproperty key="org.uncommons.reportng.title" value="AutoMation TestReport" />
</testng>
</target>
</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 org.uncommons.reportng.HTMLReporter@65fe58e0:
120 ms

BUILD SUCCESSFUL
Total time: 5 seconds

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

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

    ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun OpenWrt-Yun OpenWrt-Yun是基于OpenWrt的一个Linux发行版.有所耳闻的读者应该听说他是 ...

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

    前面介绍过schema.xml的一些配置信息,本章介绍solrconfig.xml的配置,以及怎样安装smartcn分词器和IK分词器,并介绍主要的查询语法. 1. solr配置solrconfig. ...

  3. Maven 教程之 pom.xml 详解

    作者:dunwu https://github.com/dunwu/blog 推荐阅读(点击即可跳转阅读) 1. SpringBoot内容聚合 2. 面试题内容聚合 3. 设计模式内容聚合 4. My ...

  4. 新人补钙系列教程之:XML处理方法

    初始化XML对象XML对象可以代表一个XML元素.属性.注释.处理指令或文本元素.在ActionScript 3.0中我们可以直接将XML数据赋值给变量: var myXML:XML = <or ...

  5. Git使用教程之SSH连接方式配置(二)

    什么是GitHub?这个网站就是提供Git仓库托管服务的. 什么是SSH Key?你的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,大白话理解就是这两个仓库如果要进行远程同步,则我们需 ...

  6. Jenkins入门教程之linux下安装配置jenkins(一)

    https://blog.csdn.net/zjh_746140129/article/details/80835866

  7. maven + appium + testng + java之pom.xml

    参考来源:<https://search.maven.org/remotecontent?filepath=io/appium/java-client/3.3.0/java-client-3.3 ...

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

    OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务   1.  OpenVAS基础知识 OpenVAS(Open Vulnerability Assessment Sys ...

  9. TestNg 10. 多线程测试-xml文件实现

    代码如下: package com.course.testng.multiThread; import org.testng.annotations.Test; public class MultiT ...

随机推荐

  1. Web前端之复选框选中属性

    熟悉web前端开发的人都知道,判断复选框是否选中是经常做的事情,判断的方法很多,但是开发过程中常常忽略了这些方法的兼容性,而是实现效果就好 了.博主之前用户不少方法,经常Google到一些这个不好那个 ...

  2. jquery更改输入框type为密码框password

    很蛋疼的一个问题: <input type="text" id="e1" value="123" /> 用juqery将文本框变 ...

  3. 微信小程序之数据绑定(五)

    [未经作者本人允许,请勿以任何形式转载] 前几篇讲述微信小程序开发工具使用.生命周期和事件. 本次讲述微信小程序数据和视图绑定 >>>数据视图绑定 做前端开发的同学,尤其是WEB前端 ...

  4. 【腾讯GAD暑期训练营游戏程序班】游戏中的特效系统作业说明文档

  5. 恢复 Windows 7 的“回到父目录”按钮

    Windows 7 使用以来很多方面一直不习惯,特别是让我无比纠结的”回到父目录“ 按钮从资源管理器中消失了. 不能不说这是一个失败! 很多时候,Win 7 地址栏中自以为是的显示的很多层目录层次的面 ...

  6. 基于SoCkit的opencl实验1-基础例程

    基于SoCkit的opencl实验1-基础例程 准备软硬件 Arrow SoCkit Board 4GB or larger microSD Card Quartus II v14.1 SoCEDS ...

  7. MySQL多配置方式的多实例的部署

    安装MySQL需要注意的事项: 选择MySQL的版本的建议: 1)稳定版:选择开源的社区版的稳定版GA版本 2)选择MySQL数据库GA版本发布后六个月以后得GA版本 3)选择发布版本前后几个月没有大 ...

  8. MVC架构模式分析与设计(一)---简单的mvc架构

    首先 我要感谢慕课网的老师提供视频资料 http://www.imooc.com/learn/69 下面我来进行笔记 我们制作一个简单的mvc架构 制作第一个控制器 testController.cl ...

  9. linux red hat 给普通用户开启root权限

    环境:虚拟机:red hat 6.5:root角色用户:普通用户:宏基笔记本:win7: 操作过程: 1.登录普通用户,进入图形界面(可以设置为启动登录进入命令行界面): 2.按Crl+ALT+F2进 ...

  10. a版本冲刺第四天

    队名:Aruba   队员: 黄辉昌 李陈辉 林炳锋 鄢继仁 张秀锋 章  鼎 学号 昨天完成的任务 今天做的任务 明天要做的任务 困难点 体会 408 完成学习Java从入门到精通基础篇 通读了构建 ...