在构建期间不执行测试

Posted

技术标签:

【中文标题】在构建期间不执行测试【英文标题】:Test are not executed during build time 【发布时间】:2021-11-19 22:17:41 【问题描述】:

我使用这个 Maven 配置来执行 TestNG 的测试集合:

<?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>Automation</artifactId>
    <version>1.1</version>

    <repositories>
        <repository>
            <id>Central Repository</id>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
        <repository>
            <id>google-api-services</id>
            <url>http://google-api-client-libraries.appspot.com/mavenrepo</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>repository.spring.release</id>
            <name>Spring GA Repository</name>
            <url>https://repo.spring.io/plugins-release/</url>
        </pluginRepository>
    </pluginRepositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version> <!-- There is a bug into the latest version 7.4.0. Wait for next stable version to be released before upgrade. -->
            <scope>test</scope>
        </dependency>
        <!-- Selenium dependencies -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-htmlunit-driver</artifactId>
            <version>2.52.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.21.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.22</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

当我编译项目或运行clean test 时,没有执行测试。你知道我可以如何配置要执行的测试吗?

编辑。测试示例:

package org.mobile.login;

import org.automation.utils.BrowserDriver;
import org.automation.utils.jaxb.Environment;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Timeout;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.utils.LoginProcess;

import java.util.concurrent.TimeUnit;

import static org.automation.utils.Constants.*;

public class ReloadPageTest extends LoginProcess 

    private Environment env;

    @BeforeSuite
    public void setup() throws Exception 
        env = setupEnvironment();
    

    @Tag("description")
    @Order(1)
    @Test(testName = "52333 Mobile Web - Reload a page to refresh it", groups =  "multiple_runs" )
    @Timeout(value = 80, unit = TimeUnit.SECONDS)
    public void Mobile_Web_Reload_a_page_to_refresh_it_52333() throws InterruptedException
    
        WebDriver driver = new BrowserDriver().initDriver();

        // Navigate to Test Environment
        driver.get(env.getConfiguration().getUrl());
        .....

【问题讨论】:

你的test sources in src/test/java and the classes named *Test.java是吗? 测试总是默认执行,除非你在命令行中添加--skip-tests。确保您使用来自正确包的正确注释以及 Gerold 所说的内容。 你使用哪个框架?测试NG?还是 JUnit Jupiter? 我使用TestNG,从依赖关系可以看出。 我看到了 testng 以及 JUnit Jupiter (junit-jupiter-engine) 的依赖关系,这是这里的问题......你必须决定使用哪一个...... 【参考方案1】:

使用 maven-surefire-plugin 版本 2.22.2 而不是 3.0.0-M5:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>

更改后,将执行测试。

【讨论】:

可以验证这也修复了我最近的一个项目。

以上是关于在构建期间不执行测试的主要内容,如果未能解决你的问题,请参考以下文章

@Sql 注释脚本执行,但测试期间数据不可见

EXCEL VBA 窗体显示期间不执行下面代码,也不影响操作工作表?

从可执行文件中找出编译优化标志

配置 Flash 播放器以与在 Maven 发布期间执行的 Flexmojos 一起使用:执行

在构建期间生成 ramdisk 并填充内容

在构建异常期间调用的 setState() 或 markNeedsBuild() 阻止我执行回调