Reputation: 41
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?
Upvotes: 2
Views: 736
Reputation: 163
@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:
@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_..
Upvotes: 3