Espresso 2.1无法找到我的测试

时间:2015-05-08 15:09:35

标签: android gradle android-espresso

我已经查看了堆栈中我遇到类似问题的所有问题,但没有一个给我一个实际工作的答案(https://stackoverflow.com/search?q=espresso+empty+test+suite)。

我有一个简单的项目,我想尝试浓缩咖啡。我的类文件如下所示:

package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FirstActivity extends Activity implements View.OnClickListener {

    Button mClickMeButton;
    TextView mHelloWorldTextView;

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

        setupView();
    }

    private void setupView() {
        mHelloWorldTextView = (TextView) findViewById(R.id.helloWorldTextView);
        mHelloWorldTextView.setText(getTitleText());
        mClickMeButton = (Button) findViewById(R.id.clickMeBtn);
        mClickMeButton.setOnClickListener(this);
    }

    private String getTitleText() {
        return "First Activity";
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}

我的测试文件如下所示:

package com.example.myapplication.test;

import android.graphics.Bitmap;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.SmallTest;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

import com.example.myapplication.BuildConfig;
import com.example.myapplication.FirstActivity;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class FirstActivityTest extends ActivityInstrumentationTestCase2<FirstActivity> {

    public FirstActivityTest() {
        super(FirstActivity.class);
    }

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

    @Test
    public void testListGoesOverTheFold() {
        onView(withText("First Activity")).check(matches(isDisplayed()));
    }
}

和build.gradle一样:

apply plugin: 'com.android.application'
apply plugin: 'jacoco'
apply plugin: "sonar-runner"
apply plugin: 'spoon'

android {
    compileSdkVersion 21
    buildToolsVersion '22.0.1'

    packagingOptions {
        exclude 'LICENSE.txt'
    }

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        testApplicationId "com.example.myapplication.test"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

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

    dependencies {
        testCompile 'org.easytesting:fest:1.0.16'
        testCompile 'junit:junit:4.12'
        testCompile 'org.robolectric:robolectric:2.4'
        testCompile 'com.squareup:fest-android:1.0.8'

        androidTestCompile 'com.squareup.spoon:spoon-client:1.1.9'
        androidTestCompile 'com.android.support.test:runner:0.2'
        androidTestCompile 'com.android.support.test:rules:0.2'
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
            exclude group: 'javax.inject'
        }
    }

    spoon {
        debug = true
        //devices = ['333236E9AE5800EC']
        if (project.hasProperty('spoonClassName')){
            className = project.spoonClassName
        }
    }
}

def coverageSourceDirs = [
        '../app/src/main/java', 'src/gen'
]


task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }

    classDirectories = fileTree(
            dir: './build/intermediates/classes/debug',
            excludes: ['**/R.class',
                       '**/R$*.class'
            ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('build/jacoco/testDebug.exec')
}
}

每当我尝试运行它时,我得到:

Testing started at 16:57 ...
Target device: genymotion-google_nexus_5___5_0_0___api_21___1080x1920-192.168.56.101:5555
Uploading file
    local path: /Users/pernillauhlin/development/pingdom/android-robolectric-test/app/build/outputs/apk/app-debug.apk
    remote path: /data/local/tmp/com.example.myapplication
No apk changes detected. Skipping file upload, force stopping package instead.
DEVICE SHELL COMMAND: am force-stop com.example.myapplication
Uploading file
    local path: /Users/pernillauhlin/development/pingdom/android-robolectric-test/app/build/outputs/apk/app-debug-androidTest-unaligned.apk
    remote path: /data/local/tmp/com.example.myapplication.test
No apk changes detected. Skipping file upload, force stopping package instead.
DEVICE SHELL COMMAND: am force-stop com.example.myapplication.test
Running tests
Test running startedTest running failed: Unable to find instrumentation info for: ComponentInfo{com.example.myapplication.test/android.test.InstrumentationTestRunner}
Empty test suite.

我的结构是:

enter image description here

另外,从命令运行勺子给我相同的结果。所以,我的问题是:

  • 我错过了什么?
  • 测试的注释;在测试之前,我已经在课堂上看到了很多变化,等等。什么是正确的方法?或者你可以在课前指定@LargeTest,在实际测试方法之前指定@SmallTest吗?

编辑:

我的运行配置如下所示:

看起来android.support.test.runner.AndroidJUnitRunner并不存在。

构建变体:

enter image description here

2 个答案:

答案 0 :(得分:4)

我设法通过在测试配置中设置错误 android.test.InstrumentationTestRunner来重现您的情况。

我建议您检查一下运行菜单中是否指定了正确的android.support.test.runner.AndroidJUnitRunner - (或无)。编辑配置

此外,你的问题的标题有点令人困惑,因为从构建gradle依赖项,我的理解是你实际上正在使用Espresso 2.1。如果这是真的,还要注意:

  

不推荐使用ActivityInstrumentationTestCase2或ServiceTestCase等TestCases,而推荐使用ActivityTestRule或ServiceTestRule。

还可以看到herehere,注释将按以下方式使用:(将调整您已弃用的测试类,因为我很好)

//annotate JUnit4 test classes with
@RunWith(AndroidJunit4.class)
@LargeTest
public class FirstActivityTest {

//use the following annotation and declare an ActivityTestRule for your activity under test
@Rule
public ActivityTestRule<FirstActivity> mActivityRule = new ActivityTestRule(FirstActivity.class);

//use @Before to setup your test fixture
@Before
public void setUp() { ... }

//annotate all test methods with
@Test
public void testHelloWorld() { ... }

//release resources by using
@After
public void tearDown() { ... }
}

更多关于Rules

了解有关test size annotations

的更多信息的其他好地方

答案 1 :(得分:0)

也许这将有助于未来的某些人:如果你得到一个&#34;设备上没有空间&#34;安装Android内容时出错,您必须重新安装所有内容。

(如果你的磁盘在安装过程中几乎没有装满,请不要读它。)

我有类似的问题,测试没有运行。原因是Android工作室公布的内存要求不包括测试所需的组件。 (顺便说一句,你不仅需要2G,而是主目录中/person/2/address/3 2G中的2G。)现在,当磁盘内存耗尽时,您将得到一个无效的安装。清除一些额外空间并重新运行安装程序后,它仍然无法正常工作。您必须删除所有内容,清除/tmp(因为删除软件后只会浪费宝贵的空间),然后重新安装。 (很可能,他们下载.zip档案并且不检查结果。即使他们认为md5检查太复杂,他们仍然可以检查文件长度!但他们没有。)

要在Linux下的目录中打印内存使用情况,请使用~/.AndroidStudio1.5(或du -h -d 1用于2个目录级别,du -h -d 2代表人类可读的内容。