TestNG中实现多线程、超时


多线程测试

多线程测试-注解方式实现

使用注解@Test的属性:invocationCount(线程数)、threadPoolSize(线程池)

package com.course.testng.multiThread;

import org.testng.annotations.Test;

public class MultiThreadOnAnnotion {
    @Test(invocationCount = 10,threadPoolSize = 3)
    public void test(){
        System.out.println(1);
        System.out.printf("ID:"+Thread.currentThread().getId());
    }
}

多线程测试-xml文件实现

在套件suite标签下增加属性parallel:methods,最大并发数:thread-count

parallel:(属性值),这是属性值是什么,那么就是以什么为标志,标志对应的name不同就使用不同的线程

例如:

methods级别:所有Test的用例使用不同线程执行

<?xml version="1.0" encoding="UTF-8" ?>

    
    
        
            
        
    

tests级别:相同的test tag下的用例可以在同一个的线程下执行

<?xml version="1.0" encoding="UTF-8" ?>

    
    
        
            
        
    
    
        
            
        
    

classes级别:相同的class tag 下的用例再同一个线程下

<?xml version="1.0" encoding="UTF-8" ?>

    
    
        
            
        
    
    
        
            
        
    

超时测试

使用注解Test的属性timeOut来设置

package com.course.testng;

import org.testng.annotations.Test;

public class TimeOutTest {
    @Test(timeOut = 3000)
    public void TestSuccess() throws InterruptedException {
        Thread.sleep(2000);

    }
    @Test(timeOut = 3000)
    public void TestFaild() throws InterruptedException {
        Thread.sleep(4000);

    }
}