繁体   English   中英

Android Espresso:测试运行失败。 没有测试结果 空的测试套件。 为什么?

[英]Android Espresso: Test running failed. No test results Empty test suite. Why?

无法让我的 Espresso UI 测试工作。 问题出在哪里?
对于这个简单的应用程序,我应该得到什么结果才能认为它正在运行?

我有“android.support.test.runner.AndroidJUnitRunner”用于Edit Confirurations Instrumentation runner。

来自 AndroidManifest.xml:

    <instrumentation
android:name="android.support.test.runner.AndroidJUnitRunner"
android:targetPackage="my.package"/>

从 build.gradle:

defaultConfig {
    applicationId "my.package"
    minSdkVersion 18
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

packagingOptions {
    exclude 'LICENSE.txt'}

repositories { jcenter()}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:support-annotations:22.2.0'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
testCompile 'junit:junit:4.12'}

主活动.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText editText = (EditText)findViewById(R.id.edit_text);
        Button button = (Button) findViewById(R.id.confirm);
        final TextView textView = (TextView)findViewById(R.id.you_entered);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText("You entered: " + editText.getText());
            }
        });
    }
}

应用测试程序

    public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }
}

主要活动测试

public class MainActivityTest extends ActivityInstrumentationTestCase2 <MainActivity> {

    Activity activity;
    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        activity = getActivity();
    }

    @SmallTest
    public void testInputOutput() {
        Espresso.onView(ViewMatchers.withId(R.id.edit_text)).perform(ViewActions.click())
                .perform(ViewActions.typeText("Espresso"));
        Espresso.onView(ViewMatchers.withId(R.id.confirm)).perform(ViewActions.click());
        Espresso.onView(ViewMatchers.withId(R.id.you_entered)).check(ViewAssertions.matches
                (ViewMatchers.withText("Espresso")));
        assertEquals(((TextView)ViewMatchers.withId(R.id.you_entered)).getText(), "Espresso");
    }
}

运行所有测试的结果:

Testing started at 12:36 ...
Installing my.package
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/my.package"
pkg: /data/local/tmp/my.package
Success

Uploading file
    local path: /home/jane_doe/project/android/MyTestApp/app/build/outputs/apk/app-debug-androidTest-unaligned.apk
    remote path: /data/local/tmp/my.package.test
Installing my.package.test
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/my.package.test"
pkg: /data/local/tmp/my.package.test
Success

Running tests
Test running startedTest running failed: No test results
Empty test suite.

仅运行 MainActivityTest 的结果:

Testing started at 12:39 ...
12:39:39: Executing external tasks 'cleanTest test --tests cosysoft.cupofcoffee.MainActivityTest'...
:app:cleanTest UP-TO-DATE
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72220Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42220Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:preCompileDebugUnitTestJava
:app:preDebugUnitTestBuild UP-TO-DATE
:app:prepareDebugUnitTestDependencies
:app:processDebugUnitTestJavaRes UP-TO-DATE
:app:compileDebugUnitTestJava UP-TO-DATE
:app:compileDebugUnitTestSources UP-TO-DATE
:app:mockableAndroidJar UP-TO-DATE
:app:assembleDebugUnitTest UP-TO-DATE
:app:testDebug
:app:testDebug FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> No tests found for given includes: [my.package.MainActivityTest]
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 3.079 secs
No tests found for given includes: [my.package.MainActivityTest]

请注意,您使用的是已弃用的 ActivityInstrumentationTestCase2 并且

不推荐使用 ActivityInstrumentationTestCase2 或 ServiceTestCase 之类的测试用例,取而代之的是 ActivityTestRule 或 ServiceTestRule。

所以尝试切换到使用规则,这实际上非常简单。 另外,请务必使用正确的注释。 在此处查看我的其他答案以获得更多有用的参考。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM