Junit 4 不识别测试

Junit 4 does not recognize test

我克隆了一个存储库并尝试 运行 进行测试。不幸的是,JUnit 4 无法识别我手动添加的目录

测试 GUI 在尝试加载测试后显示:

No tests were found.

Tests ran to completion.

Empty test suite.

这是我的 ExampleUnitTest 目录,是我从另一个项目复制粘贴的(这是一个没有问题的默认文件,它不起作用)

这是我的 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        testApplicationId "com.commonsware.android.retrofit.test"
    }
}

dependencies {
    compile 'com.squareup.retrofit:retrofit:1.6.1'
    compile 'de.greenrobot:eventbus:2.2.1'
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
}

根据此处的要求编辑 ExampleUnitTest 和 Run/Debug 配置的内容。

package com.commonsware.android.retrofit;

/**
 * To work on unit tests, switch the Test Artifact in the Build Variants view.
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() throws Exception {
        assertEquals(4, 2 + 2);
    }
}

Run/Debug 配置

首先,确保 Android Studio 在 Run/Debug 配置 菜单中看不到任何测试。为此,请单击 Edit Configurations... 选项,然后选中左侧的 JUnit 部分。

如果没有测试,点击、selectJUnit、selectapp模块和ExampleUnitTest class。不要忘记应用更改,然后再次尝试 运行 测试。

如果这没有帮助,请尝试从 Release 切换到 Debug 构建变体,并确保 Android Monitor 选项卡中没有异常堆栈跟踪。

如果您在 "Edit Configurations" 对话框中手动创建 运行 配置,您应该 select "JUnit" 用于本地测试,而 "Android Tests" 仅用于检测测试。对单个测试 class 或方法执行此操作的最简单方法是单击编辑器 window 左侧的任何 运行 图标。您还可以在项目视图中右键单击任何 class 或包,然后单击 select "Run 'Tests in ..."。或者,您可以左键单击 class 或包,然后按 Ctrl+Shift+F10。