运行Android检测测试时出现“未找到测试”错误

时间:2016-07-13 09:00:23

标签: android-studio junit4 android-espresso

我是测试的初学者。我在android studio中为登录活动创建了一个简单的测试用例。但我得到了一个错误,我无法解决它。这是我的测试代码。帮助将非常感激。

package com.example.hassidiczaddic.testinglist;

import android.app.Application;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ApplicationTest {

    public static final String STRING_TO_BE_TYPED = "Wolfmatrix";

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

    @Test
    public void LoginActivity() {
        onView(withId(R.id.etFName))
                .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
        onView(withId(R.id.etLName))
                .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
        onView(withId(R.id.btnSubmit))
            .perform(click());
        onView(withId(R.id.tvView))
            .check(matches(withText(STRING_TO_BE_TYPED)));
    }

}

这是我的错误:

Running tests

$ adb shell am instrument -w -r   -e debug false -e class       
com.example.hassidiczaddic.testinglist.ApplicationTest
com.example.hassidiczaddic.testinglist.
test/android.test.InstrumentationTestRunner
Client not ready yet..Test running started
junit.framework.AssertionFailedError: No tests found in     
com.example.hassidiczaddic.testinglist.ApplicationTest
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at    
android.test.InstrumentationTestRunner.onStart
(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run
(Instrumentation.java:1619)
Tests ran to completion.

这是我的gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.hassidiczaddic.testinglist"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.0.1'
        // App dependencies
        compile 'com.android.support:support-annotations:23.0.1'
        compile 'com.google.guava:guava:18.0'
        androidTestCompile 'com.android.support:support-annotations:23.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'
    }
}

4 个答案:

答案 0 :(得分:15)

您忘记将AndroidJUnitRunner设置为默认的测试工具运行器。

https://developer.android.com/topic/libraries/testing-support-library/index.html

要将AndroidJUnitRunner设置为Gradle项目中的默认测试工具运行器,请在build.gradle文件中指定此依赖关系:

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

答案 1 :(得分:2)

如果目标是 SDK版本28 ,则将使用支持库中重新包装的androidx类,因此AndroidJUnitRunner的配置如下...

android {
    defaultConfig {
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }
}

答案 2 :(得分:0)

检查build.gradle

替换 testInstrumentationRunner“ androidx.test.runner.AndroidJUnit4”

android {
    defaultConfig {
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }
}

寻找app / src / androidTest / AndroidManifest.xml

android:name 更改为 androidx.multidex.MultiDexApplication

<application
        android:allowBackup="true"
        android:label="Components Tests App"
        android:supportsRtl="true"
        android:name="android.support.multidex.MultiDexApplication">

收件人

<application
            android:allowBackup="true"
            android:label="Components Tests App"
            android:supportsRtl="true"
            android:name="androidx.multidex.MultiDexApplication">

更多建议: No tests found when running instrumented tests with AndroidX

答案 3 :(得分:0)

如果您是通过从方法生成测试到达这里的...尝试将测试类中的测试方法重命名...类似于 actualMethod() 到 testActualMethod()