我刚开始读大学的计算机科学课程,我在使用IntelliJ时遇到了一些问题。当我试图运行单元测试时,我得到了消息

Process finished with exit code 1
Class not found: "edu.macalester.comp124.hw0.AreaTest"Empty test suite.

我还在屏幕左侧看到一条标题为“未找到测试”的消息。我的测试代码如下:

package edu.macalester.comp124.hw0;


import org.junit.Test;
import static org.junit.Assert.*;

public class AreaTest {

    @Test
    public void testSquare() {
    assertEquals(Area.getSquareArea(3.0), 9.0, 0.001);
    }

    @Test
    public void testCircle() {
    assertEquals(Area.getCircleArea(3.0), 28.2743, 0.001);
    }
}

我的项目代码在这里:

package edu.macalester.comp124.hw0;

import java.lang.Math;
public class Area {

/**
 * Calculates the area of a square.
 * @param sideLength The length of the side of a square
 * @return The area
 */
public static double getSquareArea(double sideLength) {
    // Has been replaced by correct formula
    return sideLength * sideLength;
}

/**
 * Calculates the area of a circle.
 * @param radius The radius of the circle
 * @return The area
 */
public static double getCircleArea(double radius) {
    // Replaced by correct value
    return radius * 2 * Math.PI;
}

}

我怎样才能让我的测试正常工作?我使用的是IntelliJ IDEA CE的最新版本。


当前回答

我有同样的问题,重建/无效缓存等不工作。似乎这只是Android Studio的一个bug……

一个临时的解决方案是从命令行运行单元测试:

./gradlew test

参见:https://developer.android.com/studio/test/command-line.html

其他回答

这里使用IDEA 15.0.6也有同样的问题,除了重命名测试类所在的包外,没有任何帮助。之后我重命名为它原来的名字,它仍然工作,所以重命名操作可能已经清除了一些缓存。

在我的案例中,通过进入我的构建,这个问题得到了解决。Gradle和change

dependencies {
    testImplementation 'junit:junit:4.12'
}

to

dependencies {
    testCompile 'junit:junit:4.12'
}

我也有同样的问题。我重建了项目,这对我很有帮助。

进入构建—>重建项目

之后,如果你正在使用Maven工具,我建议使用选项“重新导入所有Maven项目”

如果没有帮助,尝试其他可能的解决方案:

进入文件——>Invalidate cache /Restart——>Invalidate and Restart

or:

在你的Maven项目结构中,src/main/java右键单击java目录并选择选项Mark directory as——> Sources root类似地对test directory做同样的操作:src/test/java右键单击java目录并选择选项Mark directory as——> test 源根

or:

运行——>编辑配置并在JUnit部分删除测试配置。应用更改。然后尝试运行您的测试。应该自动创建新的配置。

or:

Go to File --> Project Structure, select Modules, then select your proper module and go to the Paths tab. Check options: Radio button Use module compile output path should be selected. Output path should be inside your project. Also Test output path should be directory inside your project. For example it can look similarly: Output path: C:\path\to\your\module\yourModule \target\classesTest Output path: C:\path\to\your\module\yourModule \target\test-classesExclude output paths should be deselected.

我也遇到了同样的问题(Android Studio 3.2 Canary 4),我尝试了其他答案中描述的大部分建议——没有任何成功。注意,这发生在我将文件从test文件夹移动到androidTest文件夹之后。它仍然在运行配置中显示为测试,而不是仪器测试。

我最终创建了一个新文件:

用不同的名称创建新的测试类。 从类中复制所有代码。 运行它。 删除旧类。 将新类重命名为所需的名称。

在Intellij中遵循以下步骤(为了更好地理解,有截图):

转到文件—>项目结构

导航到模块,现在选择Junit测试文件所在的模块,并选择“使用模块编译输出路径”单选按钮。 提到各自的类文件夹路径,类似于所附的屏幕截图。

请申请和好的。 这对我很管用!