【问题标题】:Surefire test report not workingSurefire 测试报告不起作用
【发布时间】:2026-01-08 12:05:02
【问题描述】:

我是 maven 的新手,我在使用它时遇到了一些麻烦。

我使用 IntellijIDEA 在网站上执行一些 junit 测试(使用 selenium webdriver)。 测试执行正确,但我无法确保生成报告。

我想我可能错过了一个目标或一个生命周期,但我无法真正理解它们是如何工作的以及它们应该放在哪里。 这是我的 POM:

<?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">

<groupId>com.lazerycode.selenium</groupId>
<artifactId>maven-template</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>

<name>Selenium Maven Template</name>
<description>A Maven Template For Selenium</description>
<url>http://www.lazerycode.com</url>

<licenses>
    <license>
        <name>Apache 2</name>
        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        <distribution>repo</distribution>
        <comments>A business-friendly OSS license</comments>
    </license>
</licenses>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <standalone.binary.root.folder>${project.basedir}/selenium_standalone_binaries</standalone.binary.root.folder>
    <browser>firefox</browser>
    <threads>1</threads>
</properties>

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.41.0</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>com.opera</groupId>
                <artifactId>operadriver</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.opera</groupId>
        <artifactId>operadriver</artifactId>
        <version>1.5</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-remote-driver</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.github.detro.ghostdriver</groupId>
        <artifactId>phantomjsdriver</artifactId>
        <version>1.1.0</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-remote-driver</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
                <version>2.3.2</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<profiles>
    <profile>
        <id>selenium-tests</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.lazerycode.selenium</groupId>
                    <artifactId>driver-binary-downloader-maven-plugin</artifactId>
                    <version>1.0.3</version>
                    <configuration>
                        <rootStandaloneServerDirectory>${standalone.binary.root.folder}</rootStandaloneServerDirectory>
                        <downloadedZipFileDirectory>${project.basedir}/selenium_standalone_zips</downloadedZipFileDirectory>
                        <customRepositoryMap>${project.basedir}/RepositoryMap.xml</customRepositoryMap>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>selenium</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.7.2</version>
                    <dependencies>
                        <!-- Force using the latest JUnit 47 provider -->
                        <dependency>
                            <groupId>org.apache.maven.surefire</groupId>
                            <artifactId>surefire-junit47</artifactId>
                            <version>2.8</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <includes>
                            <include>**/*/*/*/*.java</include>
                            <include>**/*/*/*.java</include>
                            <include>**/*/*.java</include>
                            <include>**/*.java</include>
                            <include>*.java</include>

                        </includes>
                        <parallel>methods</parallel>
                        <threadCount>${threads}</threadCount>
                    </configuration>

                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>2.17</version>
        </plugin>
    </plugins>
</reporting>

它是使用硒测试模板生成的。有人知道缺少什么吗?

【问题讨论】:

    标签: java maven selenium surefire


    【解决方案1】:

    首先,您正在根据 selenium 的使用进行集成测试,因此您应该改用 maven-failsafe-plugin。 除此之外,您使用的是 maven-surefire-plugin 的 2.7.2 版本,但报告您使用的是 maven-surefire-report-plugin 2.17...所以您应该对 maven-surefire-plugin 和 maven-surefire-report-plugin 使用相同的版本。

    【讨论】:

    • 好的,谢谢!所以我该怎么做?用我的 POM 上的故障安全替换我的万无一失,然后再试一次?还是我仍然缺少一些配置?
    • 我查看了故障安全插件的官方页面,我安装了它(或者至少我是这么认为的),然后我尝试在我的项目根目录上运行 mvn verify。事实证明,执行了 0/0 测试,在我看来,它正在尝试在我使用 Junit 时执行 NG 测试。当然,无论如何也没有报告。
    • 您已根据单元测试架构而不是集成测试架构来命名您的测试。集成测试应命名为*IT.java,而单元测试应命名为*Test.java
    • 老实说,我根本没有使用模式命名我的测试......我现在就试试
    • 仍然没有看到任何测试。我正在使用 IDEA(启动 junit)以及从外壳使用 mvn test 执行我的测试。这可能是问题吗?