如果很有个测试方法,并且这几个方法又有先后顺序,那么如果让TestNG按照自己想要的方法执行呢

一、通过Dependencies

1.在测试类中添加Dependencies

    @Test
public void test1() {
System.out.println("this is test1");
}
@Test(dependsOnMethods = { "test1" })
public void test2() {
System.out.println("this is test2");
}
@Test(dependsOnMethods = { "test2" })
public void test3() {
System.out.println("this is test3");
}
@Test(dependsOnMethods = { "test3" })
public void test4() {
System.out.println("this is test4");
}
@Test(dependsOnMethods = { "test4" })
public void test5() {
System.out.println("this is test5");
}

执行顺序,print:

this is test1
this is test2
this is test3
this is test4
this is test5

2.在xml文件中添加Dependencies,代码中无需再添加Dependencies

  <test name="Test">
<classes>
<class name="test.testng.TestOrder"/>
<methods>
<dependencies>
<method name="test2" depends-on="test1"/>
<method name="test3" depends-on="test2"/>
<method name="test4" depends-on="test3"/>
<method name="test5" depends-on="test4"/>
<method name="test1" />
</dependencies>
</methods>
</classes>
</test>

执行顺序,打印结果:

this is test1
this is test2
this is test3
this is test4
this is test5

二、通过preserve-order="true"

在xml添加<suite name="Suite" preserve-order="true">,方法将按照XML中配置的先后顺序由上按下执行(代码无需额外控制)

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" preserve-order="true">
<test name="Test">
<classes>
<class name="test.testng.TestOrder"/>
<methods>
<include name="test2" />
<include name="test3" />
<include name="test1" />
</methods>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

执行顺序,打印结果:

this is test2
this is test3
this is test1

三、并发执行,Parallel

1、如果没有并发执行用例的需要,建议把Parallel=false,如果等于Parallel=true,则会并发执行用例,除非你设置了Dependencies,否则执行的顺序将不受控制

如:<suite name="Suite" parallel="true">

2、添加Parallel的时候也可以设置线程数,如:

<suite name="My suite" parallel="methods" thread-count="5">

<suite name="My suite" parallel="tests" thread-count="5">

<suite name="My suite" parallel="classes" thread-count="5">

<suite name="My suite" parallel="instances" thread-count="5">

官方的解释,有兴趣的可以自行翻译:

  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.

Additionally, the attribute thread-count allows you to specify how many threads should be allocated for this execution.

TestNG之执行顺序的更多相关文章

  1. TestNG测试执行顺序

    1.preserve-order属性,之前一直认为preserve-order属性是控制配置方法的执行顺序的,其实不是,preserve-order主要是控制test下节点classes执行顺序的 例 ...

  2. testNG设置测试的执行顺序

    在java类中,设置Test的执行顺序可以使用priority,或者enabled等属性.但是在testng.xml中,需要设置它的 preserve-order="true" 另 ...

  3. TestNG学习-002-annotaton 注解概述及其执行顺序

    此文主要讲述用 TestNG 基础的 annotation (注解)知识,及其执行的顺序,并通过一个 TestNG 简单的实例演示 annotation 的执行顺序. 希望能对初学 TestNG 测试 ...

  4. 使用TestNG框架测试用例执行顺序问题

    既然是讨论执行顺序问题,那么用例肯定是批量执行的,批量执行的方法有mvn test.直接运行testng.xml文件,其中直接运行testng.xml文件的效果与pom文件中配置执行testng.xm ...

  5. 14、testng.xml 设置用例执行顺序

    目录如下: TestGroup.java 代码如下: package com.testng.cn; import org.testng.annotations.*; import static org ...

  6. testng基础知识:注解的执行顺序

    1. 单类,无继承父子关系 code: public class basicTest { @BeforeSuite(alwaysRun = true) public void beforeSuite_ ...

  7. JMeter学习-005-JMeter 主要组件概要介绍及执行顺序

    本文将对 JMeter 主要组件(主要涉及 Threads(Users).Test Fragment.逻辑控制器.配置元件.定时器.前置处理器.Sampler.后置处理器.断言.监听器 十大组件)进行 ...

  8. TestNG-详解preserve-order的作用与测试case的执行顺序

    在TestNG xml配置文件中,关于<test>的配置里面,有一个属性叫preserve-order,一开始以为这个属性可以用来控制测试case(那些被@Test注解标注的方法)的执行顺 ...

  9. python unittest控制用例的执行顺序

    为什么要进行顺序控制呢?使用过testng的同学就知道,它相对于junit来说有更强大的功能,其中的一个功能就是依赖测试.什么是依赖测试呢?简单的说一下就是,A方法运行时,其中有个变量的取值是B方法的 ...

随机推荐

  1. 整合spring,springmvc和mybatis

    我创建的是maven项目,使用到的依赖架包有下面这些: <dependencies> <dependency> <groupId>org.springframewo ...

  2. Tomcat服务器与MyEclipse绑定

    myeclipse中eclipse添加了很多非常实用的插件,几乎包含了常用的所有应用服务器插件,其中自然包括支持各个版本的Tomcat插件. 先来看看Tomcat处理浏览器请求的过程图: 1.Tomc ...

  3. [python拾遗]列表

    python列表拾遗 1.列表可以修改,使用 ‘+’ 将一个新列表附加在原列表的尾部: >>> a = [1,'a'] >>> b = a + [2,'b'] &g ...

  4. [python学习笔记]Day3

    函数 如: def is_leapyear(year): if (year%4 == 0 and year%100 != 0) or (year%400 == 0): return True else ...

  5. 数据库的有关知识==>>我们的血泪史之经典练习(1-2)

    今天给大家说说数据库的有关知识 抒情一下,想在好困,真的,虽然我在这温暖的教室,身边有知心的盆友, ,很高兴还能是学生的一员,我们还年轻,我们也不会想的太多,高高兴兴上学,快快乐乐回家,每天吃的饱饱, ...

  6. 我所了解的WEB开发(3) - 彩虹的颜色

    据说彩虹有七彩颜色,从外至内分别为:红.橙.黄.绿.青.蓝.紫.这些我倒是没有验证过,但是学生时代就不止一次色盲检测,还是让我足够确信对颜色的分辨应该和大多数人相似的. 还听说大多数哺乳动物是色盲.如 ...

  7. [WCF REST] Web消息主体风格(Message Body Style)

    对于Web HTTP编程模型来说,服务契约中作为操作的方法无须应用OperationContractAttribute特性,只需要根据需要应用WebGetAttribute与WebInvokeAttr ...

  8. 收集oracle统计信息

    优化器统计范围: 表统计: --行数,块数,行平均长度:all_tables:NUM_ROWS,BLOCKS,AVG_ROW_LEN:列统计: --列中唯一值的数量(NDV),NULL值的数量,数据分 ...

  9. Android 计算布局背景的透明度

    1.#ff000000 此为16进制颜色代码,前2位ff为透明度,后6位为颜色值(000000为黑色,ffffff为白色,可以用ps等软件获取). 2.透明度分为256阶(0-255),计算机上用16 ...

  10. 【原】结构体包含CString类型成员变量出错的原理

    问题如下:我定义了如下的一个结构体: typedef struct{   CString csText;}MyStruct; 并有如下的程序段1:MyStruct * p=NULL;p=(MyStru ...