Timeout for tests

如果想把timeout太久的测试自动标记为失败,有两种方法:

1.在 @Test里加上 Timeout 参数

定义"timeout=1000“的话,如果超过1000 毫秒,failure会被一个抛出的异常触发。

import static org.junit.Assert.*;
import org.junit.Test;

public class TimeoutTest {

	@Test(timeout=1000)
	public void test(){
		assertTrue("it should be true",true);
	}
}

2.定义Timeout 规则(应用于整个测试类)

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;

public class Timeout2 {
	public static String log;
	
	@Rule
	public Timeout globalTimeout=new Timeout(10000);
	
	@Test
	public void test01(){
		log +="ran1";
		for(;;){}
	}
	
	@Test
	public void test02(){
		log+="ran2";
		for(;;){}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The readPosInt method uses System.out.print (not println) to print its string argument on the screen (later when we use the readPosInt method, the string argument of the method will be a message telling the user to type some integer). Then the readPosInt method uses the input scanner object to read an integer from the user of the program. After reading the integer, the readPosInt method must also use the scanner’s nextLine method to read the single newline character that comes from the user pressing the Enter key on the keyboard after typing the integer (if you do not read this newline character using the nextLine method inside the readPosInt method, then the newline character will remain in the input stream, and, the next time you use the readLine method described above, the readLine method will just immediately read only the newline character from the input stream and return an empty string as result, without waiting for the user to type anything!) If the user types something which is not an integer, then the nextInt method of the scanner will throw an InputMismatchException. In that case the code of your readPosInt method must catch the exception, use System.out.println to print the error message "You must type an integer!" to the user (use System.out.println for this, not System.err.println, otherwise you might hit a bug in Eclipse...), use the scanner’s nextLine method to read (and ignore) the wrong input typed by the user of the program (if you do not do this, the wrong input typed by the user will remain in the input stream, and the next time you call the nextInt method again, you will get an InputMismatchException again!), and then do the whole thing again (including printing again the string argument of the readPosInt method) to try to read an integer again (hint: put the whole code of the method inside a while loop). After reading the integer and the newline character (which is just ignored), the readPosInt method tests the integer.写java文件
05-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值