通过maven运行testng测试时,不执行BeforeGroups方法

时间:2016-03-11 03:37:14

标签: java maven testng

我看到标有“@BeforeGroups”的方法没有被执行。

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());
    }
}

在同一个包中 - 配置服务器的类:

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文件:

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

运行:

mvn integration-test

[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

4 个答案:

答案 0 :(得分:1)

来自Maven Failsafe Plugin – Inclusions and Exclusions of Tests

  

默认情况下,Failsafe插件将自动包含具有以下通配符模式的所有测试类:

     
      
  • // posts.js angular.module('myapp.posts', []).factory('Posts', ...) // users.js angular.module('myapp.users', []).factory('Users', ...) // users-for-posts.js angular.module('myapp.users-for-posts', ['myapp.posts', 'myapp.users']) .factory('UsersForPosts', function(Posts, Users) { // ... }) - 包括所有子目录和以&#34; IT&#34;开头的所有Java文件名。
  •   
  • "**/IT*.java" - 包括所有子目录和所有以&#34; IT&#34;结尾的Java文件名。
  •   
  • "**/*IT.java" - 包括所有子目录和所有以&#34; ITCase&#34;结尾的Java文件名。
  •   

由于"**/*ITCase.java"类名称与任何默认包含模式都不匹配,因此它将从您的测试运行中排除。

您可以将TestServerConfigurator重命名为与默认模式匹配的内容(例如TestServerConfiguratorITTestServerConfiguratorTestServerConfiguratorIT),或者定义您自己的包含模式:

TestServerConfiguratorITCase

答案 1 :(得分:0)

如果我将“配置”部分替换为:

,我认为它有效
    <configuration>
        <groups>http</groups>
        <includes>
            <include>*</include>
        </includes>
    </configuration>

我认为“includes”部分仅用于列出测试,而不是所有应该对testng可见的类,但显然情况并非如此。

答案 2 :(得分:0)

面临同样的问题。 将@Test(groups = "http")添加到您的TestServerConfigurator课程

答案 3 :(得分:0)

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

相关问题