java - JUnit:测试计算速度

标签 java junit execution-time

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

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/

相关文章:

java - 应用程序在蓝牙发现期间崩溃

java - 与Spring集成测试: Cannot convert value of type error

assembly - 如何减少阶乘循环的执行时间和周期数?和/或代码大小?

c++ - 如何计算 cilk_spawn 的时间

java - 如何更改 SortOrder 以避免 "unsupported collating sort order"错误?

java - 编辑默认部分并制作一组按钮

java - 将按钮面板放在中央 Java Swing

java - 参数化测试因 mvn 测试而失败

java - JUnit @Rule 生命周期与@Before 交互

php - 如何提高PHPFox开发的 'Facebook'这样的社交网站的执行速度并减少执行时间?