gpt4 book ai didi

java - JUnit:测试计算速度

转载 作者:行者123 更新时间:2023-11-29 05:22:11 29 4
gpt4 key购买 nike

例如,我有一个方法可以使用以下一些操作“加、减、乘、除”来计算两个数字并返回结果。

public int sum(int a, int b) {
return a + b;
}

如果计算时间不超过 0.5 秒,我是否可以测试该方法的执行时间?

最佳答案

使用 @Test(timeout=1000) 注释您的测试方法。

Timeout parameter on @Test Annotation (applies to test method)

You can optionally specify timeout in milliseconds to cause a test method to fail if it takes longer than that number of milliseconds. If the time limit is exceeded, the the failure is triggered by an Exception being thrown:

查看junit doku .

关于java - JUnit:测试计算速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24249136/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com