Android JUnitRunner Empty Test Suite:没有测试结果

时间:2015-09-20 12:16:48

标签: android junit junit4 android-testing

我在Android Studio中运行测试时遇到问题。我已经在Stack Overflow上阅读了大量有关同一问题的帖子,但无济于事。为了清楚起见,我想在模拟器中进行测试,而不是在JVM中进行测试。

运行配置:

run-config

输出:

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

的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "ntnu.stud.markul.crowdshelf"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    // Unit testing dependencies
    androidTestCompile ('com.android.support.test:rules:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:runner:0.3') {
        // Necessary if your app targets Marshmallow (since the test runner
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile 'junit:junit:4.12'
    testCompile 'junit:junit:4.12'
}

测试类(位于/ src / androidTest / java / com / appname / app /):

@RunWith(AndroidJUnit4.class)
public class NetworkAndDBInstrumentationTest extends ActivityInstrumentationTestCase2<MainActivity> {
    private MainActivity mActivity;
    public NetworkAndDBInstrumentationTest() {
        super(MainActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();

        // Injecting the Instrumentation instance is required
        // for your test to run with AndroidJUnitRunner.
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }

    @Test
    public void typeOperandsAndPerformAddOperation() {
        NetworkController.getCrowd("55f01f29f0a5fad2120bb1db");
        System.out.print("ok");
        Log.d("1TEST", "1TEST");
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }
}

3 个答案:

答案 0 :(得分:1)

我用它来工作:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class NetworkAndDBInstrumentationTest {
    //use the following annotation and declare an ActivityTestRule for your activity under test
    @Rule
    public ActivityTestRule<RealmActivity> mActivityRule = new ActivityTestRule(RealmActivity.class);

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

    //annotate all test methods with
    @Test
    public void testGetCrowd() {
        Assert.assertEquals(1, 1);
    }
    @After
    public void tearDown() {  }
}

RealmActivity是我想要测试的活动。 Build.gradle和其他所有内容都在问题帖子中。现在,一个后续问题:我用RealmActivity制作@Rule有什么关系?我不能在该测试中使用其他活动吗?我可以进行与活动无关的测试吗?

答案 1 :(得分:0)

您的配置看起来正确。您可以尝试将其添加到build.gradle文件中:

/*
 * Working build.gradle test config. Gradle version 1.3.0
 */


apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "test.your.app"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode versionMajor * 100 + versionMinor * 10 + versionPatch
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"

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

    signingConfigs {
    }

    configurations.all {
        // Currently espresso is dependent on support-annotations:22.2.1
        resolutionStrategy.force 'com.android.support:support-annotations:22.2.1'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    lintOptions {
        disable 'SetJavaScriptEnabled'
        disable 'InvalidPackage'
        abortOnError false
        textReport true
        textOutput 'stdout'
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }

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

        release {
        }
    }

    productFlavors {
        dev {
            versionName "DEV"
        }
    }

    sourceSets {  //this one may not be needed. But wont hurt.
        androidTest.setRoot('src/androidTest')
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Android
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:support-v13:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:support-annotations:23.0.1'

    // Google Play Services
    compile 'com.google.android.gms:play-services-base:7.8.0'
    compile 'com.google.android.gms:play-services-analytics:7.8.0'
    compile 'com.google.android.gms:play-services-appindexing:7.8.0'

    // Integration Tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2') {
        exclude group:'com.android.support', module:'support-v4'
        exclude module: 'recyclerview-v7'
    }

    // Unit Tests
    testCompile 'junit:junit:4.12'
    testCompile 'com.google.truth:truth:0.25'

    //Mocking
    androidTestCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile "com.google.dexmaker:dexmaker:${DEXMAKER_VERSION}"
    androidTestCompile "com.google.dexmaker:dexmaker-mockito:${DEXMAKER_VERSION}"
    androidTestApt "com.google.dagger:dagger-compiler:${DAGGER_VERSION}"

    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support.test:runner:0.3'

    // Leak detection
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
}

您还可以尝试使用JUnit 3样式来查看是否可以获得更好的结果。你在扩展ApplicationTestCase吗?试着把事情搞定。

答案 2 :(得分:-1)

我认为您需要在依赖项中添加此行:

androidTestCompile ('org.hamcrest:hamcrest-library:1.3')