I want to fail the build if anyone writes a test that takes longer than 1 second to run, but if I run in perTest mode it takes much, much longer.
I could probably write a custom task that parses the junit reports and fails the build based on that, but I was wondering if anyone knows or can think of a better option.
Reviving an old question since the answer doesn't provide an example.
You can specify timeout
Per test method:
For all methods in a test class using
Timeout
@Rule
:Globally for the total time to run all test methods in the class using a static
Timeout
@ClassRule
:And even apply timeout (either
@Rule
or@ClassRule
) to all classes in your entire suite:EDIT: timeout was deprecated recently to utilize this initialization
You should provide the Timeunit now, as this will provide more granularity to your code.
If you use JUnit 4 and
@Test
, you can specifiy thetimeout
parameter that will fail a tests that's taking longer than specified. Downside for that is that you'd have to add it to every test method.A probably better alternative is the use of a
@Rule
withorg.junit.rules.Timeout
. With this, you can do it per class (or even in a shared super class).