简体   繁体   中英

TestNG parallel execution behavior doesn't match the expected one

I'm currently facing a problem that I can't identify.

I'm trying to run tests in parallel using testNG. I have no problem running different tests in parallel, but when I try to run a single test multiple time in different thread, I have a strange behavior.

So, I tried to debug to find out the problem. The error occurs when I'm trying to get a value from the TestNG XML configuration file :

public class LocalWebDriverListener implements IInvokedMethodListener {

    static Logger log = Logger.getLogger(LocalWebDriverListener.class);

    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        log.debug("BEGINNING: LocalWebDriverListener.beforeInvocation");
        if (method.isTestMethod()) {
            //Error on this line
            String browserName = method.getTestMethod().getXmlTest().getLocalParameters().get("browserName");
            //getXmlTest() returns null
            WebDriver driver = LocalDriverFactory.createInstance(browserName);
            DriverManager.setWebDriver(driver);
        } else {
            log.warn("Not a TestNG test");
        }
        log.debug("END: LocalWebDriverListener.beforeInvocation");
    }
}

I don't understand why getXmlTest() returns null only when threadPoolSize > 1 in @Test parameters.

public class TestClass {

    static Logger log = Logger.getLogger(TestClass.class);

    @Test(invocationCount = 5, threadPoolSize = 5)
    public void testMethod1() {
        invokeBrowser("http://www.google.com/");
    }
}

XML :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test executed using WebDriver" parallel="tests" thread-count="5">
    <listeners>
        <listener class-name="my.organization.WebDriverListener" />
    </listeners>
    <test name="Tests run in Chrome">
        <parameter name="browserName" value="chrome" />
        <packages>
            <package name="my.organization.tests" />
        </packages>
    </test>
</suite>

Can you help me on this one ?

Thanks

The problem is that IInvokedMethod does not know about testng.xml Replace String browserName = method.getTestMethod().getXmlTest().getLocalParameters().get("browserName"); with String browserName = testResult.getTestContext().getCurrentXmlTest().getLocalParameters().get("browserName");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM