Intellij无法运行测试

6

在将我的项目导入Intellij并成功构建后,我尝试运行一些项目测试。我导航到测试文件并选择运行 -> 运行。然而,这不会运行我的测试,只会打开一个小的“编辑配置”窗口,如附图所示。

enter image description here

当我按照提示选择编辑配置时,JUnit找不到。窗口如下所示。

enter image description here

我需要做什么来运行测试?


你的测试实际上是在一个测试源文件夹中吗? - Makoto
1
请查看这篇Stack Overflow帖子。我猜测你的IntelliJ找不到JUnit JAR文件,这就是为什么它没有出现在下拉菜单中的原因(它在我的中出现了)。 - Tim Biegeleisen
请在项目设置 -> 模块中检查您的测试包是否标记为“Tests”。 - Rufi
在代码窗口或项目面板中右键单击测试类名称,然后选择“运行<classname>”。如果您在弹出菜单中看不到运行选项,则表示您还没有选择测试,或者您没有安装 Junit 插件。 - Software Engineer
4个回答

2

对我来说,这也是与我的pom.xml有关的问题。在查看Junit5样例pom后,我发现测试插件缺失了。所以我只需要添加:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
</plugin>

您可能还需要检查您的pom文件是否包含以下内容:
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.6.2</version>
    <scope>test</scope>
</dependency>

1
对我来说,这是一个与我的pom.xml有关的问题,并且在使用JUnit5IntelliJ时,我的测试没有被检测到,并且显示0 executed 0 skipped等内容。下面是我添加到pom.xml中以使JUnit5测试在IntelliJ中运行的内容:
    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0-M1</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.2.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

这是我添加的依赖项:

    <dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.easytesting</groupId>
        <artifactId>fest-assert-core</artifactId>
        <version>2.0M10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-surefire-provider</artifactId>
        <version>1.2.0-M1</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.2.0-M1</version>
    </dependency>
</dependencies>

0

尝试点击您的项目并单击运行“所有测试”。之后,转到“编辑配置..”并确保在VM选项区域使用-ea值。

enter image description here


0

确保您的IDEA已安装Junit插件和Junit jar,并将其添加到类路径中:enter image description here

然后只需单击此处运行测试用例:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接