2015-12-19 38 views
9

Tôi muốn thử nghiệm MainActivity trong ứng dụng Android của mình. Vì vậy, tôi đã tạo ra một trường hợp thử nghiệm đầu tiên kiểm tra chức năng của một nút. Nếu người dùng nhấp vào nút cụ thể này, một Hoạt động mới sẽ mở ra.Không tìm thấy thử nghiệm nào trong <package> khi thử nghiệm với Espresso

Đây là mã của tôi:

@RunWith(AndroidJUnit4.class) 
public class MainActivityTest { 

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

    @Test 
    public void startNewActivityTest() { 
     Intents.init(); 
     onView(withId(R.id.main_new)).perform(click()); 
     intended(hasComponent(NewActivity.class.getName())); 
     Intents.release(); 
    } 

    @Before 
    public void setup() { 
     closeSoftKeyboard(); 
    } 

} 

Thật không may, tôi nhận được ngoại lệ sau đây:

junit.framework.AssertionFailedError: No tests found in package.MainActivityTest 
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) 
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) 
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701) 

Đây là tập tin Gradle tôi:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 19 
    buildToolsVersion "23.0.1" 

    lintOptions { 
     abortOnError false 
    } 

    defaultConfig { 
     applicationId "package" 
     minSdkVersion 14 
     targetSdkVersion 19 
    } 

    signingConfigs { 
     debug { 
      storeFile file(RELEASE_STORE_FILE) 
      storePassword RELEASE_STORE_PASSWORD 
      keyAlias RELEASE_KEY_ALIAS 
      keyPassword RELEASE_KEY_PASSWORD 
     } 
     release { 
      storeFile file(RELEASE_STORE_FILE) 
      storePassword RELEASE_STORE_PASSWORD 
      keyAlias RELEASE_KEY_ALIAS 
      keyPassword RELEASE_KEY_PASSWORD 
     } 
    } 
    compileOptions { 
     sourceCompatibility JavaVersion.VERSION_1_7 
     targetCompatibility JavaVersion.VERSION_1_7 
    } 
} 

dependencies { 
    compile 'com.android.support:support-v4:19.1.0' 
    compile 'com.google.android.gms:play-services:4.2.+' 
    androidTestCompile 'com.android.support:support-annotations:19.0.1' 
    androidTestCompile 'com.android.support.test:runner:0.4.1' 
    androidTestCompile 'com.android.support.test:rules:0.4.1' 
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' 
    // Set this dependency if you want to use Hamcrest matching 
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3' 
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1' 
} 
+0

Bạn có thể gửi một số thông tin về môi trường của bạn (như phiên bản Gradle, file gradle, vv)? Bạn đã cân nhắc sử dụng [Robolectric] (http://robolectric.org/) chưa? – Schrieveslaach

+0

có lẽ https://github.com/googlesamples/android-testing/blob/master/ui/espresso/IntentsBasicSample/app/build.gradle#L13 – albodelu

Trả lời

21

Set the instrumentation runner

Thêm vào build.gradle cùng nộp dòng sau trong android.defaultConfig: testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

apply plugin: 'com.android.application' 

android { 
    ... 

    defaultConfig { 
     ... 

     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
} 

dependencies { 
    // App's dependencies, including test 
    compile 'com.android.support:support-annotations:23.0.1' 

    ... 
} 

tôi không bao giờ sử dụng Espresso Intents nhưng có lẽ bạn cần this như here:

sử dụng IntentsTestRule thay vì ActivityTestRule khi sử dụng Espresso-Intents. IntentsTestRule giúp bạn dễ dàng sử dụng API Espresso-Intents trong các kiểm tra giao diện người dùng chức năng. Lớp này là một phần mở rộng của của ActivityTestRule, khởi tạo Espresso-Intents trước mỗi bài kiểm tra được chú thích bằng @Test và phát hành Espresso-Intents sau mỗi lần chạy thử. Hoạt động sẽ bị chấm dứt sau mỗi lần kiểm tra và quy tắc này có thể được sử dụng theo cách tương tự như ActivityTestRule.

0
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 

bị mất tích

Các vấn đề liên quan