我想在TestNG中动态设置方法选择器。我正在使用IAlterSuiteListener实现在套件级别设置方法选择器,并使用IMethodSelector实现为方法选择器定义过滤器。

IAlterSuiteListener实现的源代码为(CustomAlterSuiteListener.java文件):

package com.sample.testflow;

import java.util.Collections;
import java.util.List;

import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlMethodSelector;
import org.testng.xml.XmlSuite;

public class CustomAlterSuiteListener implements IAlterSuiteListener {

    @Override
    public void alter(List<XmlSuite> suites) {
        XmlSuite suite = suites.get(0);

        XmlMethodSelector xmlMethodSelector = new XmlMethodSelector();
        xmlMethodSelector.setClassName("com.sample.testflow.CustomMethodSelectors");
        xmlMethodSelector.setPriority(0);

        suite.setMethodSelectors(Collections.singletonList(xmlMethodSelector));
    }
}


CustomMethodSelectors.java文件:

package com.sample.testflow;

import java.util.List;

import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;

public class CustomMethodSelectors implements IMethodSelector {

    @Override
    public boolean includeMethod(IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod) {
        System.out.println("Method --> " + method.getMethodName());

        return true;
    }

    @Override
    public void setTestMethods(List<ITestNGMethod> testMethods) {
    }
}


样品测试:

package com.sample.testflow;

import org.testng.annotations.Test;

public class SampleTest {

    @Test
    public void testSample() {
        System.out.println("Test sample 1");
    }
}


testng.xml文件:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="sample" data-provider-thread-count="20" parallel="methods" thread-count="3">
    <test name="all">
        <packages>
            <package name="com.sample.*"/>
        </packages>
    </test>
</suite>


我正在使用服务加载器运行CustomAlterSuiteListener类,资源目录中META-INF / services中的org.testng.ITestNGListener文件包含:

com.sample.testflow.CustomAlterSuiteListener


运行测试的命令:

mvn clean test


最后是带有surefire插件配置的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>com.sample</groupId>
    <artifactId>testflow</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>target/test-classes/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <failIfNoTests>false</failIfNoTests>
                    <trimStackTrace>false</trimStackTrace>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


但是此设置无效,我的意思是,不会显示在自定义方法选择器类中定义的消息。难道我做错了什么?

最佳答案

我找到了解决方案,尽管我认为这不是最好的。而不是在套件级别设置方法选择器,您应该在测试级别设置方法选择器,即IAlterSuiteListener实现(CustomAlterSuiteListener.java文件)将是:

package com.sample.testflow;

import java.util.Collections;
import java.util.List;

import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlMethodSelector;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class CustomAlterSuiteListener implements IAlterSuiteListener {

    @Override
    public void alter(List<XmlSuite> suites) {
        XmlMethodSelector xmlMethodSelector = new XmlMethodSelector();
        xmlMethodSelector.setClassName("com.sample.testflow.CustomMethodSelectors");
        xmlMethodSelector.setPriority(0);

        XmlSuite suite = suites.get(0);
        XmlTest xmlTest = suite.getTests().get(0);
        xmlTest.setMethodSelectors(Collections.singletonList(xmlMethodSelector));
    }
}

10-05 18:52