2

Android Test Orchestrator Runs several times the BeforeClass method. Since I'm using Espresso to do end-to-end testing, I'm calling some APIS to get some users created as a testsetup and they being created for every test, It's slowing down the test execution.

On gradle

testOptions {
    execution 'ANDROID_TEST_ORCHESTRATOR'
}
...
androidTestUtil 'com.android.support.test:orchestrator:1.0.1'



public class LoginTest extends IntegrationTest {
  @Rule
  public ActivityTestRule<LoginActivity> activityTestRule = new ActivityTestRule<LoginActivity>(LoginActivity.class);

  @BeforeClass
  static public void doThisFirstOnlyOnce() {
    Log.d("Testing_tag", "BEFORE CLASS");
  }

  @Before
  public void doThisFirst() {
    Log.d("Testing_tag", "BEFORE every test");
  }

  @After
  public void doThisLast() {
    Log.d("Testing_tag", "After every test");
  }

  @AfterClass
  static public void doThisLastOnlyOnce() {
    Log.d("Testing_tag", "AFTER CLASS");
  }

  @Test
  public void test1() {
    Log.d("Testing_tag", "Test 1");
  }

  @Test
  public void test2() {
    Log.d("Testing_tag", "Test 2");
  }
}

I uses the same configuration for espresso and the rest.

Results

03-08 13:38:50.978 18756-18782/com.app D/Testing_tag: BEFORE CLASS
03-08 13:38:51.844 18756-18782/com.app D/Testing_tag: BEFORE every test
03-08 13:38:51.844 18756-18782/com.app D/Testing_tag: Test 1
03-08 13:38:51.844 18756-18782/com.app D/Testing_tag: After every test
03-08 13:38:52.141 18756-18782/com.app D/Testing_tag: AFTER CLASS
03-08 13:38:53.503 18825-18851/? D/Testing_tag: BEFORE CLASS
03-08 13:38:54.366 18825-18851/com.app D/Testing_tag: BEFORE every test
03-08 13:38:54.366 18825-18851/com.app D/Testing_tag: Test 2
03-08 13:38:54.366 18825-18851/com.app D/Testing_tag: After every test
03-08 13:38:54.728 18825-18851/com.app D/Testing_tag: AFTER CLASS

Do you know anyway to fix this?

grizzthedj
  • 7,131
  • 16
  • 42
  • 62
  • Can you try something from here? https://stackoverflow.com/questions/12087959/junit-run-set-up-method-once It seems that most popular answer might fit your needs. – Dragan Marjanović Mar 08 '18 at 13:28
  • I think this question is not quite complete; classical mantra is "what did you want to achieve, what you did, what you expected to happen, what actually happened". I would expect here a little more comment on what's wrong with your test results. You just throw some data at us and expect us to analyse them. – mcepl Mar 08 '18 at 13:29

1 Answers1

3

@mcepl, the question is spot on, and I have the same problem. If you use Android Test Orchestrator and have methods with @BeforeClass and @AfterClass, they are broke. I mean, they are called before and after the every test. For example:

@BeforeClass

@test1

@AfterClass

@BeforeClass

@test2

@AfterClass

@BeforeClass

@test3

@AfterClass

If you dont use Android Test Orchestrator, the method are called just once (the default behavior)

@BeforeClass

@test1

@test2

@test3

@AfterClass

Here is the link of the similar q: @BeforeClass and @AfterClass called before and after each test

Is there any workaround?

Solution: I have used different approach, and it works for me:

  • Set running tests in name order using:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

  • Create method with name: aaa_test_what_needs_to_be_done_once_before_all_test

  • Create method with name: zzz_test_what_needs_to_be_done_once_after_all_test

  • Rename test methods to start with test_..

User1980
  • 163
  • 1
  • 8