Annotating a test with @Stopwatch
will measure the time the test takes to execute and will report the result to the TestReporter
.
How that information is displayed depends on the tool used to run the tests and how it processes test report entries.
Usage
@Stopwatch
can be applied to a method to report its execution time:
@Test
@Stopwatch
void test() {
// execution time will be reported
}
It can also be applied to a class, in which case it reports the execution times of each test as if it were applied to each method individually:
@Stopwatch
class TestCases {
@Test
void test_1() {
// execution time will be reported
}
@Test
void test_s() {
// execution time will be reported
}
}
It can also be applied to a class and a method therein but since a class-level annotation already works as if each method was annotated, the method-level annotations would be redundant.
Output
This is how IntelliJ displays a report entry (in the Run/Debug panel):
timestamp = 2022-05-26T12:16:14.021646, StopwatchExtension = Execution of 'test()' took [11] ms.
Other tools may or may not print report entries.
The most reliable way to gather entries is to create a TestExecutionListener
and register it with the launcher.
Report entries from the stopwatch extension can be identified by checking their key-value pairs - at least one key will start with the string "StopwatchExtension"
.
Thread-Safety
This extension is safe to use during parallel test execution.