简体   繁体   English

Maven 运行 testng 测试时不执行 BeforeGroups 方法

[英]BeforeGroups method is not executed when running testng tests by maven

I see that the method marked with "@BeforeGroups" is not executed by .我看到标有“@BeforeGroups”的方法不是由 .

Java test class: Java测试类:

import org.testng.annotations.Test;

import static org.testng.Assert.fail;

@Test(groups = "http")
public class MyIT {

    @Test(groups = "http")
    public void method1() {
        System.out.println("test 1 - value from TestServerConfigurator: "
            + TestServerConfigurator.getSomeString());
    }
}

in the same package - the class to configure server:在同一个包中 - 配置服务器的类:

public class TestServerConfigurator {
    private static String someString;

    @BeforeGroups(groups = "http")
    public static void init() {
        System.out.println("initializing server...");
        someString = "value set!";
    }

    @AfterGroups(groups = "http")
    public static void after() {
        System.out.println("stopping server...");
    }

    public static String getSomeString() {
        return someString;
    }
}

pom.xml file: pom.xml 文件:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.alskor</groupId>
    <artifactId>mytests</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>mytests</name>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <groups>http</groups>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

running:跑步:

mvn integration-test mvn 集成测试

[INFO] --- maven-failsafe-plugin:2.19.1:integration-test (default) @ mytests ---

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running MyIT
test 1 - value from TestServerConfigurator: null
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.218 sec - in MyIT

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

From Maven Failsafe Plugin – Inclusions and Exclusions of Tests :来自Maven 故障安全插件 - 测试的包含和排除

By default, the Failsafe Plugin will automatically include all test classes with the following wildcard patterns:默认情况下,Failsafe 插件将自动包含所有具有以下通配符模式的测试类:

  • "**/IT*.java" - includes all of its subdirectories and all Java filenames that start with "IT". "**/IT*.java" - 包括其所有子目录和所有以“IT”开头的 Java 文件名。
  • "**/*IT.java" - includes all of its subdirectories and all Java filenames that end with "IT". "**/*IT.java" - 包括其所有子目录和所有以“IT”结尾的 Java 文件名。
  • "**/*ITCase.java" - includes all of its subdirectories and all Java filenames that end with "ITCase". "**/*ITCase.java" - 包括其所有子目录和所有以“ITCase”结尾的 Java 文件名。

As the TestServerConfigurator class name does not match any of the default inclusion patterns it is being excluded from your test run.由于TestServerConfigurator类名与任何默认包含模式都不匹配,因此它被从您的测试运行中排除。

You can rename TestServerConfigurator to something that matches the default patterns (ie ITTestServerConfigurator , TestServerConfiguratorIT , or TestServerConfiguratorITCase ) or define your own inclusion patterns:您可以将TestServerConfigurator重命名为与默认模式匹配的内容(即ITTestServerConfiguratorTestServerConfiguratorITTestServerConfiguratorITCase )或定义您自己的包含模式:

            <configuration>
                <includes>
                    <include>**/*IT.java</include>
                    <include>**/*Configurator.java</include>
                </includes>
                <groups>http</groups>
            </configuration>

I see it works if I replace "configuration" section with this:如果我用这个替换“配置”部分,我看到它有效:

    <configuration>
        <groups>http</groups>
        <includes>
            <include>*</include>
        </includes>
    </configuration>

I thought "includes" section was intended to list tests only and not all classes that should be visible to testng, but apparently that's not the case.我认为“包含”部分旨在仅列出测试而不是所有应该对 testng 可见的类,但显然情况并非如此。

Faced with the same problem.面临同样的问题。 Add @Test(groups = "http") to your TestServerConfigurator class@Test(groups = "http")到您的TestServerConfigurator

尝试根据您的系统浏览器更新ChromeDriver版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TestNG:即使我正在运行一组测试,也会为每个组条目调用@BeforeGroups - TestNG: @BeforeGroups gets called for every group entry even though I am running one group of tests 在Maven中运行TestNG测试失败,因为拆卸方法 - Running TestNG tests in Maven fails because teardown method 通过Maven运行testng测试时更改控制台输出 - change the console output when running testng tests via maven TestNG 测试在作为 Maven 测试运行时失败,但在作为 TestNG 套件运行时通过 - TestNG tests fail when ran as Maven Test, but pass when running as TestNG suite 从命令行执行时 Maven 测试未运行 - Maven tests not running when executed from command line 未针对 TestNG 执行测试 - Tests are not being executed for TestNG 在 maven 中运行单个测试 -&gt; 未执行任何测试 - Running a single test in maven -> No tests were executed 我从命令行运行testng,但未执行任何测试 - I'm running testng from command line but no tests get executed TestNG:是否应该通过按方法或类的测试来运行测试,请调用@BeforeSuite方法? - TestNG: Should running tests by tests by method or class call the @BeforeSuite method? 在 DataProvider 为单个测试方法使用不同数据运行多个测试的情况下,在 TestNG/Maven 报告中获取测试方法名称 - Get Test method name in TestNG/Maven reporting in case of DataProvider running multiple tests with different data for single Test method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM