ceshiren.com
霍格沃兹测试学社
@Timeout(5)
注解配置超时时间import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import static java.lang.Thread.sleep;
class TimeoutExampleTest {
@Test
@Timeout(7)
void timeoutDemo1() throws InterruptedException {
sleep(10000);
System.out.println("超时用例1");
}
@Test
void timeoutDemo2(){
System.out.println("超时用例2");
}
}
@BeforeEach
与@AfterEach
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import static java.lang.Thread.sleep;
class TimeoutExampleTest {
@AfterEach
@Timeout(3)
void teardown() throws InterruptedException {
sleep(40000);
System.out.println("结束测试");
}
@BeforeEach
@Timeout(3)
void setup() throws InterruptedException {
sleep(40000);
System.out.println("开始测试");
}
@Test
void timeoutDemo2(){
System.out.println("超时用例2");
}
}
时间 | 配置内容 |
---|---|
42毫秒 | @Timeout(value = 42, unit = MILLISECONDS) |
42秒 | @Timeout(value = 42, unit = SECONDS) |
42分 | @Timeout(value = 42, unit = MINUTES) |
42小时 | @Timeout(value = 42, unit = HOURS) |
42天 | @Timeout(value = 42, unit = DAYS) |
package com.hogwarts.JUnit5;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;
public class TimeoutExampleTest {
// 如果超过5秒则执行失败
@BeforeEach
@Timeout(5)
void setUp() {
}
// 通过value参数配置数量,通过unit参数配置时间单位
@Test
@Timeout(value = 100, unit = TimeUnit.MILLISECONDS)
void failsIfExecutionTimeExceeds100Milliseconds() {
}
}