我刚开始读大学的计算机科学课程,我在使用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的最新版本。


当前回答

我也有同样的问题。在我的情况下,我在主文件夹外的包/文件夹中有一些测试类。但是当我检查运行配置时,它总是试图在主文件夹内寻找类(而不是我的包在主文件夹外)。 因此,如果是这种情况,您要么必须将包移动到Run配置所指向的位置。 或者更改运行配置以指向您的包。

其他回答

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

dependencies {
    testImplementation 'junit:junit:4.12'
}

to

dependencies {
    testCompile 'junit:junit:4.12'
}

在安装了新版本的IntelliJ而IntelliJ插件还没有更新之后,可能会发生这种情况(对我来说至少有一次;)。

您可能需要手动执行IntelliJ帮助菜单中的“检查更新…”

您的测试是否需要Android设备(模拟器或硬件)? 如果是,它被称为“仪器测试”,位于“module-name/src/androidTest/java/”。 如果不是,它被称为“本地单元测试”,位于“module-name/src/test/java”

https://developer.android.com/training/testing/start/index.html

我得到了同样的错误,因为我已经编写了一个本地单元测试,但它被放置在用于测试的文件夹中。将本地单元测试移到“src/test/java”文件夹中可以解决这个问题。

这可能是因为文件夹没有设置为测试源,这可以通过模块设置>模块来完成。

我也有同样的问题。我使用了一个不是英语的字符,重命名它解决了我的问题。 例如:i。