2

The setup:

An older project I've inherited has a lot of legacy instrumentation tests and I would like to impose a timeout on them, since a lot of them can hang indefinitely and this makes it hard to get a test report. I'm in the process of updating the tests to be Junit4 style, but at the moment they're all extending ActivityInstrumentationTestCase2.

Tried so far:

In the documentation for AndroidJUnitRunner it says to set this flag:

Set timeout (in milliseconds) that will be applied to each test: -e timeout_msec 5000 ...

... All arguments can also be specified in the in the AndroidManifest via a meta-data tag

I've tried adding AndroidJUnitRunner configuration to the app manifest and the test manifest, but the timeout_msec meta-data item has had no effect so far.

Turnsole
  • 3,422
  • 5
  • 30
  • 52

1 Answers1

8

You can use a rule to provide a timeout for each test in the class as shown below.

@Rule public Timeout timeout = new Timeout(120000, TimeUnit.MILLISECONDS);

You can also specify per test basis timeouts by using the following

@Test(timeout = 100) // Exception: test timed out after 100 milliseconds
public void test1() throws Exception {
    Thread.sleep(200);
}

You can read more about the differences using this link

https://stackoverflow.com/a/32034936/2128442

Randy
  • 1,400
  • 2
  • 12
  • 30
  • 2
    Love getting downvotes for answering questions that show up in the top 5 of a google search. Filling gaps shouldn't result in negative votes, especially since this is supposed to be a helpful community, not cynical. – Randy Aug 28 '17 at 17:05