In the second test, getActivity () never returns

I have some simple tests like assertNotNull(mActivity); (I am reading the "Android Application Testing Guide for MDTorres"). Activity under control is normal. Each test passes normally. But if I run several tests at once in the second getActivity() test, it does not return. There are no errors in logcat (the last line is "Starting Intent ..."), there is nothing. Debugging doesn't help either, if I go into getActivity() , it complains that there is no source code.
Another test project - ActivityTesting from Google works fine even with several tests, so Eclipse is configured correctly.
Has anyone ever come across something like this?

+6
source share
2 answers

I recreated the test project (for example, "clean room"), and it worked. Then I compared the two projects and found the culprit. It was an empty breakdown:

 protected void tearDown() throws Exception { } 

If I remove it, all tests will be green. If I push it back in, the second test freezes. Now I would like to read the explanation and am ready to mark it as an answer.

Edit: I have to call super.tearDown() at the end of the tearDown method. Sorry to bother everyone.

+8
source

I used ActivityInstrumentationTestCase2 to check activity with ExoPlayer and fix resources. Since clean and final checks are the same for all tests, I thought tearDown () was a good place to implement them. All tests run separately without any problems, but when I run several tests, sometimes getActivity () did not return. My tearDown () implemented various things:

  • check activity status (various assert () calls)
  • check player status (various assert () calls)
  • manually clear player resources (calling close () and release ())
  • setActivity (null) (this caused the problem)
  • super.tearDown ()

I tried all the suggested workarounds, such as overriding getActivity () and using other toolkit methods to create and clean up the activity. These methods did not help.

And a lot of debugging showed that using the onDestory () script above, the activity of the previous test can overlap with onCreate () for the next test to work. Thus, the logs displayed life cycle events in the following order:

 test1.getActivity(); test1.tearDown() called; test1.tearDown() over; test2.getActivity() test2.onCreate(); test1.onStop(); --> why is this late? test1.onDestroy(); --> why is this late? test2.tearDown() called; test2.tearDown() over; test3.getActiviy() --> this should call test3.onCreate, but did not and never returned. 

This happens even when test cases are implemented in separate classes / files of ActivityInstrumentationTestCase2. And the first time this overlap does not cause problems, so 2 tests are always in order, but executing 3 tests in any order that lead to this overlap causes the 3rd call to getActivity () to never return.

I tried everything like calling onPause () + onStop () + onDestroy () manually using the toolkit, representing very long waiting periods between tests, enhancing the cleaning of the TestCase toolkit activity, reordering the checks of my tearDown, but nothing helped. Finally, I accidentally deleted setActivity (null) before my checks, and the life cycle events got the correct ordering:

 test1.tearDown() called; test1.onStop(); test1.onDestroy(); test1.tearDown() over; test2.getActivity() test2.onCreate(); ... 

So what really mattered in my case: don't call ActivityTestCase.setActivity (). This leads to the fact that super.tearDown () does not directly trigger the activity lifecycle events, cleaning will happen later and cause problems.

+1
source

Source: https://habr.com/ru/post/906032/


All Articles