2016-06-16 4 views
4

私のアプリケーションの自動テストを書きたいと思います。すべての機能にはログインが必要です。Espresso + Junit4 - すべてのテストを実行する前に一度ログインしてください

私はテストを書いていますが、テストごとに、ログインして機能をテストしています。とにかく私は一度だけログインしてすべてのテストを実行するのに役立つでしょうか?

最も簡単な方法は、すべてのテストを1つのテスト方法で書くことです。しかし、私はそれを達成するには醜い方法だと思う。どんなクリーナーソリューションでも、テストは一度だけログインしてからテストセットを実行します。続き

は私のテストコードです:

@RunWith(AndroidJUnit4.class) 
public class AllDisabledTest { 
    public static final String USER_NAME = "all_disabled"; 
    public static final String DISPLAY_NAME = "All Disabled"; 
    public static final String PASSWORD = "1234"; 

    @Rule 
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
      LoginActivity.class); 

    @Before 
    public void loginToApp(){ 

     onView(withId(R.id.edit_email)).perform(replaceText(USER_NAME)); 
     onView(withId(R.id.edit_password)).perform(replaceText(PASSWORD)); 

     onView(withId(R.id.login_button)).perform(click()); 
    } 

    @Test 
    public void checkIfFoodItemAddedToCart(){ 
     onData(anything()).inAdapterView(withId(R.id.menu_item_grid)).atPosition(2).perform(click()); 

     onData(anything()).inAdapterView(withId(R.id.listview)).atPosition(0).onChildView(withId(R.id.item_name)).check(matches(withText("BLUEITEM"))); 
    } 
} 

は:)事前にありがとうございます。

答えて

3

@BeforeClassアノテーションと@AfterClassアノテーションを持つメソッドを使用できます。

@RunWith(AndroidJUnit4.class) 
public class AllDisabledTest { 
    public static final String USER_NAME = "all_disabled"; 
    public static final String DISPLAY_NAME = "All Disabled"; 
    public static final String PASSWORD = "1234"; 

    @Rule 
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
      LoginActivity.class); 
    } 

    @BeforeClass 
    public static void setUpBeforeClass() { 
     // do login stuff here 
    } 

    @AfterClass 
    public static void tearDownAfterClass() { 
     // ... 
    } 

    // ... 
} 

注:@BeforeClassメソッドと@AfterClassメソッドは静的である必要があります。

関連する問題