AndroidX Espresso 测试:未找到测试且测试套件为空

AndroidX Espresso Test: No tests were found and Empty test suite

我正在尝试 运行 src/androidTest 这是我的 Android 项目中使用 [= 的仪器测试37=]X 库,但我收到 No tests were found 错误和 Empty test suite 日志。

我检查了所有使用 AndroidX 库的示例和文档都在做同样的事情。

我已经尝试根据以下 link 设置 Edit Configurations 但仍然没有成功。

我做错了什么吗?或缺少任何步骤?

Test Run log

请检查这张图片

MainActivityTest.java代码:

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

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

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

   @Test
   public void checkViewsVisibility() {
     // verify the visibility of recycler view on screen
     onView(withId(R.id.record_list_rv)).check(matches(isDisplayed()));

     // verify the visibility of progressbar on screen
     onView(withId(R.id.progressBar)).check(matches(isDisplayed()));

     // verify the visibility of no data TextView on screen By default it should be GONE
     onView(withId(R.id.no_data_tv)).check(matches(isDisplayed()));

     onView(withId(R.id.no_data_tv)).check(matches(withText("No Data")));

     onView(withId(R.id.no_data_tv)).perform(typeText("No Data"));
   }
}

Gradle 文件依赖:

android {
    defaultConfig {
       testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunne"
    }

    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
    }
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestUtil 'androidx.test:orchestrator:1.1.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.1'
}

您的默认配置中有错字。大概应该是:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

对我而言,在确保所有依赖项都可用后,将 targetSdk 从 30 更改为较低版本解决了我的问题。

就我而言,我也努力 运行 UI 测试。检查(并尝试)类似主题中的大部分内容对我来说没有任何结果。最后我 运行 我的仪器测试通过 gradle 命令:

gradlew connectedProdDebugAndroidTest

或者(如果你想运行单测,或者单测class)

gradlew connectedProdDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class={package.class}#{methodName}

在通过命令 运行 测试后,不知何故,可以通过专用按钮 运行 它们。