jeudi 9 janvier 2020

Dynamic method selectors in TestNG are not working

I would like to set method selectors in TestNG dynamically. I am using IAlterSuiteListener implementation to set method selector at suite level and IMethodSelector implementation to define filter for method selectors.

Source code for IAlterSuiteListener implementation is (CustomAlterSuiteListener.java file):

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

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) {
    }
}

Sample test:

package com.sample.testflow;

import org.testng.annotations.Test;

public class SampleTest {

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

testng.xml file:

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

I am using service loader to run CustomAlterSuiteListener class, org.testng.ITestNGListener file inside META-INF/services in resources directory contains:

com.sample.testflow.CustomAlterSuiteListener

Command for running test:

mvn clean test

And lastly pom.xml file with surefire plugin configuration:

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

But this set up is not working, I mean, message defined in custom method selector class is not displayed. Am I doing something wrong?

Aucun commentaire:

Enregistrer un commentaire