TestNG 中 ParallelMode.METHODS,ParallelMode.CLASSES,ParallelMode.TESTS区别
来源:互联网 发布:在线网络投资靠谱吗 编辑:程序博客网 时间:2024/06/06 07:39
在 TestNG 中,可以设置testNG 是否是多线程的,这时候 需要用到ParallelMode来定义
那么ParallelMode.METHODS,ParallelMode.CLASSES, ParallelMode.TESTS 有什么区别呢?
结论:
ParallelMode.METHODS:一个线程负责一个@Test标签的程序,多个并行时,每个class,method之间的线程ID都不一样
ParallelMode.CLASSES:一个线程 负责一个class里面所有的带有@Test标签的method,多个并行时,每个class之间的线程id不一样,但是同一个class里的@Test的method线程id是一样的
ParallelMode.TESTS:一个线程负责一个TestNG.xml中的一个< Test >标签,多个并行时,每个 < Test > 所包含的class,method之间的线程ID 是一致的,但是 每个< Test > 之间的线程ID是不一致的
做个实验:
ParallelMode.METHODS:
package com.howtodoinjava.parallelism;import org.testng.annotations.AfterClass;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeClass;import org.testng.annotations.BeforeMethod;import org.testng.annotations.Test;public class ParallelClassesTestTwo { @BeforeClass public void beforeClass() { long id = Thread.currentThread().getId(); System.out.println("222 Before test-class. Thread id is: " + id); } @BeforeMethod public void beforeMethod() { long id = Thread.currentThread().getId(); System.out.println("222 Before test-method. Thread id is: " + id); } @Test public void testMethodOne() { long id = Thread.currentThread().getId(); System.out.println("222 Sample test-method One. Thread id is: " + id); } @Test public void testMethodTwo() { long id = Thread.currentThread().getId(); System.out.println("222 Sample test-method Two. Thread id is: " + id); } @AfterMethod public void afterMethod() { long id = Thread.currentThread().getId(); System.out.println("222 After test-method. Thread id is: " + id); } @AfterClass public void afterClass() { long id = Thread.currentThread().getId(); System.out.println("222 After test-class. Thread id is: " + id); }}
对应的testNG.xml 如下,
<suite name="Test-method Suite" parallel="methods" thread-count="2"> <test name="Test-method test" group-by-instances="true"> <classes> <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" /> </classes> </test></suite>
得到的结果为:
[TestNG] Running: /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml222 Before test-class. Thread id is: 11222 Before test-method. Thread id is: 10222 Before test-method. Thread id is: 11222 Sample test-method Two. Thread id is: 11222 Sample test-method One. Thread id is: 10222 After test-method. Thread id is: 10222 After test-method. Thread id is: 11222 After test-class. Thread id is: 11===============================================Test-method SuiteTotal tests run: 2, Failures: 0, Skips: 0===============================================
ParallelMode.CLASSES:
package com.howtodoinjava.parallelism;import org.testng.annotations.AfterClass;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeClass;import org.testng.annotations.BeforeMethod;import org.testng.annotations.Test;public class ParallelClassesTestTwo { @BeforeClass public void beforeClass() { long id = Thread.currentThread().getId(); System.out.println("222 Before test-class. Thread id is: " + id); } @BeforeMethod public void beforeMethod() { long id = Thread.currentThread().getId(); System.out.println("222 Before test-method. Thread id is: " + id); } @Test public void testMethodOne() { long id = Thread.currentThread().getId(); System.out.println("222 Sample test-method One. Thread id is: " + id); } @Test public void testMethodTwo() { long id = Thread.currentThread().getId(); System.out.println("222 Sample test-method Two. Thread id is: " + id); } @AfterMethod public void afterMethod() { long id = Thread.currentThread().getId(); System.out.println("222 After test-method. Thread id is: " + id); } @AfterClass public void afterClass() { long id = Thread.currentThread().getId(); System.out.println("222 After test-class. Thread id is: " + id); }}
package com.howtodoinjava.parallelism;import org.testng.annotations.AfterClass;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeClass;import org.testng.annotations.BeforeMethod;import org.testng.annotations.Test;public class ParallelMethodTest { @BeforeClass public void beforeClass(){ long id = Thread.currentThread().getId(); System.out.println("111 Before class-method. Thread id is: " + id); } @BeforeMethod public void beforeMethod() { long id = Thread.currentThread().getId(); System.out.println("111 Before test-method. Thread id is: " + id); } @Test public void testMethodsOne() { long id = Thread.currentThread().getId(); System.out.println("111 Simple test-method One. Thread id is: " + id); } @AfterMethod public void afterMethod() { long id = Thread.currentThread().getId(); System.out.println("111 After test-method. Thread id is: " + id); } @AfterClass public void AfterClass(){ long id = Thread.currentThread().getId(); System.out.println("111 After class-method. Thread id is: " + id); }}
<suite name="Test-method Suite" parallel="classes" thread-count="2"> <test name="Test-method test" group-by-instances="true"> <classes> <class name="com.howtodoinjava.parallelism.ParallelMethodTest" /> <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" /> </classes> </test></suite>
In the result , you could clearly to see that thread 10 is only working for the 111, and thread 11 working for the 222.
[TestNG] Running: /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml111 Before class-method. Thread id is: 10111 Before test-method. Thread id is: 10111 Simple test-method One. Thread id is: 10111 After test-method. Thread id is: 10111 After class-method. Thread id is: 10222 Before test-class. Thread id is: 11222 Before test-method. Thread id is: 11222 Sample test-method One. Thread id is: 11222 After test-method. Thread id is: 11222 Before test-method. Thread id is: 11222 Sample test-method Two. Thread id is: 11222 After test-method. Thread id is: 11222 After test-class. Thread id is: 11===============================================Test-method SuiteTotal tests run: 3, Failures: 0, Skips: 0===============================================
ParallelMode.TESTS:
Same java code as above, with different testNG.xml:
1> 在同一个test标签中:
<suite name="Test-method Suite" parallel="tests" thread-count="2"> <test name="Test-method test" group-by-instances="true"> <classes> <class name="com.howtodoinjava.parallelism.ParallelMethodTest" /> <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" /> </classes> </test> </suite>
得到如下结果:
[TestNG] Running: /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml111 Before class-method. Thread id is: 10111 Before test-method. Thread id is: 10111 Simple test-method One. Thread id is: 10111 After test-method. Thread id is: 10111 After class-method. Thread id is: 10222 Before test-class. Thread id is: 10222 Before test-method. Thread id is: 10222 Sample test-method One. Thread id is: 10222 After test-method. Thread id is: 10222 Before test-method. Thread id is: 10222 Sample test-method Two. Thread id is: 10222 After test-method. Thread id is: 10222 After test-class. Thread id is: 10===============================================Test-method SuiteTotal tests run: 3, Failures: 0, Skips: 0===============================================
2> 在不同的Test标签中
<suite name="Test-method Suite" parallel="tests" thread-count="2"> <test name="Test-method test" group-by-instances="true"> <classes> <class name="com.howtodoinjava.parallelism.ParallelMethodTest" /> </classes> </test> <test name="Test-method test2" group-by-instances="true"> <classes> <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" /> </classes> </test></suite>
得到结果如下:多线程出现
[TestNG] Running: /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml222 Before test-class. Thread id is: 11111 Before class-method. Thread id is: 10111 Before test-method. Thread id is: 10222 Before test-method. Thread id is: 11111 Simple test-method One. Thread id is: 10222 Sample test-method One. Thread id is: 11222 After test-method. Thread id is: 11111 After test-method. Thread id is: 10111 After class-method. Thread id is: 10222 Before test-method. Thread id is: 11222 Sample test-method Two. Thread id is: 11222 After test-method. Thread id is: 11222 After test-class. Thread id is: 11===============================================Test-method SuiteTotal tests run: 3, Failures: 0, Skips: 0===============================================
- TestNG 中 ParallelMode.METHODS,ParallelMode.CLASSES,ParallelMode.TESTS区别
- testng翻译之五--Test methods, Test classes and Test groups
- Partial Classes and Methods
- Classes, Methods and RTTI
- Classes, Objects, and Methods
- 06. Methods & Classes
- Java-Abstract classes and methods
- About Abstract Methods and Classes
- Executing Selenium TestNG tests using ANT
- Executing Selenium TestNG tests using ANT
- Include and Exclude Test Methods in TestNG
- Partial Classes and Methods (C# Programming Guide)
- Categories Add Methods to Existing Classes
- Preference中 onCreateView and onBindView methods的区别
- Vuejs中关于computed、methods、watch的区别
- Vuejs中关于computed、methods、watch的区别
- computed 和 methods 区别
- CSS 中类 (classes) 和 ID 的区别
- 在Dubbo中开发REST风格的远程调用(RESTful Remoting)
- Block属性声明strong和retain竟然不一样
- Druid连接池简介和配置
- Xcode --自动注释插件VVDocumenter-Xcode(安装须知)
- 用户身份切换-su sudo
- TestNG 中 ParallelMode.METHODS,ParallelMode.CLASSES,ParallelMode.TESTS区别
- 打印 上一主题 下一主题 众民协同办公系统破解版
- iOS 基础
- eclipse分别导入新浪微博 Android sdk 第三方登录demo
- iOS学习之——自定义转场
- Jetty是什么 Jetty项目简介
- Eclipse 调试器:零距离接触实战技巧
- 关于AsynTask在执行onExcute之后,很久之后才执行doInBackGround的问题
- OpenGLES---熟悉全局变量uniform,局部变量attribute,共享变量varying