防止 TestNg 在并行测试之间共享数据 [英] Prevent TestNg sharing data between parallel tests

查看:32
本文介绍了防止 TestNg 在并行测试之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:并行独立运行 2 个类,其中每个测试将方法名称存储到一个变量中,以便稍后在测试中访问.

Goal: To run 2 classes in independently in parallel where each test stores the Method name into a variable that could be accessed later in the test.

问题:当测试并行运行时,它们开始在彼此之间共享数据,从而破坏测试.

Issue: When the tests are run in parallel, they start sharing data between themselves thus corrupting the tests.

如果你看到控制台输出这是错误的:

If you see the console output this is wrong:

INFO: Name of Test from Before Method: classB_Method1
INFO: Name of Test from Before Method: classB_Method1

因为这是两个独立的类,并且正在运行方法.和我在这里设置了正确的名称:

Since these are two separate classes and methods are being run. And I set the correct name here:

  !! Setting Method name to: classA_Method1
    !! Setting Method name to: classB_Method1  

输出应如下所示:

INFO: Name of Test from Before Method: classA_Method1
INFO: Name of Test from Before Method: classB_Method1

测试A

import java.lang.reflect.Method;
import org.testng.annotations.*;
import com.xxxx.util.*;

public class TestA {


    @Test(/*dataProvider = "DP_MVPLoan_Login",*/ groups = {"parallel_test" }, invocationCount = 1, priority = 2, enabled = true)
    public void classA_Method1(/*String... excelData*/) throws Exception {

    }

    /////////////////////////////////////////////////////////////////////////////
    // ****SetUp and Tear Down

    @BeforeTest(alwaysRun=true)
    public void setupClass() throws Exception {
    }


    @BeforeMethod(alwaysRun=true)
    public void setupMethod(Method method) throws Exception {
        SeleniumHelperDebug.setCurrentMethodName(method.getName());
        SeleniumHelperDebug.defaultBeforeMethod(); 

    }

}

测试B

import java.lang.reflect.Method;
import org.testng.annotations.*;
import com.xxxx.util.*;

public class TestB {


@Test(/*dataProvider = "DP_MVPLoan_Login",*/ groups = { "parallel_test" }, invocationCount = 1, priority = 2, enabled = true)
public void classB_Method1(/*String... excelData*/) throws Exception {

}

/////////////////////////////////////////////////////////////////////////////
// ****SetUp and Tear Down

@BeforeTest(alwaysRun=true)
public void setupClass() throws Exception {
}


@BeforeMethod(alwaysRun=true)
public void setupMethod(Method method) throws Exception {
    SeleniumHelperDebug.setCurrentMethodName(method.getName());
    SeleniumHelperDebug.defaultBeforeMethod(); 

}

}

辅助方法

public class SeleniumHelperDebug { 



    //Name of the method/Test being run
    private static String currentMethodName;
    public static String getCurrentMethodName() {
        return currentMethodName;
    }
    public static void setCurrentMethodName(String currentMethodName) {
        System.out.println("!! Setting Method name to: "+ currentMethodName);
        SeleniumHelperDebug.currentMethodName = currentMethodName;
    }

    //Setup Method. BeforeTest
    public static void defaultBeforeMethod() throws Exception {
        Thread.sleep(500);
        /*setCurrentMethodName(method.getName());*/
        System.out.println("INFO: Name of Test from Before Method: " +getCurrentMethodName() );


        System.out.println("REMINDER: Keep Browser Window in Foreground to Help prevent F@ilures");
    }
}

Testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" verbose="2" thread-count="2">

<!-- 
<listeners>
<listener class-name="com.progressfin.util.WebDriverListener"></listener>
</listeners -->>
<tests>
    <test name="Test" preserve-order="true">
        <!-- <parameter name="browserName" value="firefox"></parameter> -->

    <groups>
      <run>
        <include name="parallel_test"/>
      </run>
    </groups>


    <classes>

        <class name="com.xxxx.test.TestA" />
        <class name="com.xxxx.test.TestB"/>
    </classes>


    </test> <!-- Test -->
</tests>
</suite> <!-- Suite -->

控制台输出

...
... TestNG 6.8.6 by Cédric Beust (cedric@beust.com)
...

[TestNG] Running:
  C:\Users\samuel.safyan\workspace\JavaSelenium2\testNgParallelism2.xml

[TestRunner] Starting executor for test Test with time out:2147483647 milliseconds.
!! Setting Method name to: classA_Method1
!! Setting Method name to: classB_Method1
INFO: Name of Test from Before Method: classB_Method1
REMINDER: Keep Browser Window in Foreground to Help prevent F@ilures
INFO: Name of Test from Before Method: classB_Method1
REMINDER: Keep Browser Window in Foreground to Help prevent F@ilures
PASSED: classB_Method1
PASSED: classA_Method1

===============================================
    Test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

推荐答案

您的 SeleniumHelperDebug 类是静态的,因此不是线程安全的.每次测试不能有一个实例有什么原因吗?

Your SeleniumHelperDebug class is static, hence not thread safe. Is there any reason why you cannot have an instance per test?

你想用 SeleniumHelperDebug 类解决什么问题?

What problem are you trying to solve with the SeleniumHelperDebug class?

可能有更好的线程安全解决方案,但尚不清楚该类试图实现什么

There may be a better solution which is threadsafe but it is not clear what that class is trying to achieve

这篇关于防止 TestNg 在并行测试之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆