0

I have a problem with JUnit4 and it's @Test(timeout=xxx) annotation. For example, two tests with same body, different name. None of them uses any global variables which can be initialized.
When I run tests it has totally different execution time (First test 0,811s, second 0,143).

It's very important for me to get similar results. Is there any way to resolve/workaround this problem ?

Reddy
  • 8,737
  • 11
  • 55
  • 73
cslysy
  • 810
  • 7
  • 11

2 Answers2

1

It is probable that the first test is reported as having taken longer because this includes the setup time JUnit takes to initialize the class. If you have 2 tests that need to run in approximitely the same amount of time, consider adding a third test before the other two just to get the init stuff out of the way.

Let me know if this works, am curious.

John B
  • 32,493
  • 6
  • 77
  • 98
  • Yes, you're right :) thanks. It's stragne behavior of JUnit library, class initialization time should not affect for method execution time. – cslysy Oct 17 '12 at 16:02
  • The ordering of test-method invocations is not guaranteed in JUnit. If you really want to specify the order, read - http://stackoverflow.com/questions/3089151/specifying-an-order-to-junit-4-tests-at-the-method-level-not-class-level – Dmytro Chyzhykov Oct 17 '12 at 16:03
0

Try using a timeout rule instead. The class will have been initialized before the timeout rule is applied, so class initialization time should be excluded.

Kkkev
  • 4,716
  • 5
  • 27
  • 43