【问题标题】:mvn test doesn't run any test (JUnit 4 or JUnit 5)mvn test 不运行任何测试(JUnit 4 或 JUnit 5)
【发布时间】:2020-04-01 09:18:57
【问题描述】:

我知道你一开始会怎么想:“你应该在命名你的测试类时加上‘Test’等等。” 但问题是:情况已经如此。我确实滚动了所有可能的 SO 帖子,但没有找到任何东西,所以不,很遗憾,这不是重复的。

我什至有一个名为 TestPleaseWillYouRunMyTest 的测试类和一个名为 testPleaseTest() 的测试方法。

当我运行“mvn test”时,“target/test-classes”文件夹被很好地填充,所以我真的不明白为什么我的测试没有运行......

以防万一有助于理解问题,这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SeleniumTests</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                </configuration>
            </plugin>
        </plugins>
        <testSourceDirectory> src/test/java </testSourceDirectory>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0-alpha-3</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.13.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.5.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>

    </dependencies>

</project>

【问题讨论】:

  • 您想使用哪个测试框架:JUnit Jupiter 还是 TestNG?顺便说一句:删除 &lt;testSourceDirectory&gt; src/test/java &lt;/testSourceDirectory&gt; 让 Maven 处理。
  • 将源和目标设置为 10 并不是最好的主意。使用 LTS 版本,如 Java 8 或 Java 11。
  • 能否分享一下你的测试类样本
  • 为什么要添加 testng、junit 4 和 5?首先决定你要使用哪个测试框架
  • @Nomanaliabbasi 你帮我解决了我的问题:我不得不从我的 POM 中删除 testng 依赖项。请把它写成这篇文章的答案,这样我就可以为你提供你应得的所有代表:)

标签: java maven selenium intellij-idea


【解决方案1】:

为什么要添加 testng、junit 4 和 5?首先决定你要使用哪个测试框架

【讨论】:

  • 你帮我解决了我的问题:我不得不从我的 POM 中删除 testng 依赖项;)
【解决方案2】:

只是将我的 5 美分添加到 khmarbaise 所说的内容中: 如果您的测试位于标准目录中,则根本不需要添加 testSourceDirectory,此标签适用于非标准位置。 此外,您输入的内容不正确:src/test/java - 这绝对不是您的测试所在的地方。

【讨论】:

  • 你怎么可能知道我的测试在哪里? :D
  • 您的路径是标准路径,但仍然不正确,它应该从项目位置开始
【解决方案3】:
mvn test

只编译测试类而不运行测试。为了让测试运行使用:

mvn verify

【讨论】:

  • 这是不正确的:在具有相同 POM 和相同项目结构的另一台机器上的另一个项目中,我能够使用 mvn test 运行所有测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
相关资源
最近更新 更多