简体   繁体   English

创建一个对象以在多个TestNG类之间共享数据

[英]Create an object to share data between multiple TestNG classes

I have created following relationship with object 我与对象建立了以下关系

public class Parent {
  //Making API calls to get some data which can be used
  //across multiple test classes
}

Then I'm inherting the Test classes as 然后我将Test类继承为

public class Child1 extends Parent {
 //I need data collected in Parent class
}

Again I have second class as 我再次有二等

public class Child2 extends Parent {
 //I also need the same  data collected in Parent class
}

My TestNG suite is as given below 我的TestNG套件如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="e2e-suite" verbose="1">

    <test name="e2e tests" preserve-order="true">
        <classes>
            <class name="com.abc.test.Child1" />
            <class name="com.abc.test.Child2" />
        </classes>
    </test>
</suite>

But , when I trigger the test suites , my Parent class API data collection logic get invoked properly, I also get data that is direct available to first class which consumes it ie Child1 但是,当我触发测试套件时,我的Parent类API数据收集逻辑会被正确调用,我还会获取直接使用该类的第一类数据,即Child1

But, for Child2 class , the Parent class logic isn't get invoked again (even I don't want it to execute again) . 但是,对于Child2类,不会再次调用Parent类逻辑(即使我不希望它再次执行) The question is how the data that I collected in Parent class can be shared & persistent across other Child objects like Child2 . 问题是我如何在Parent类中收集的数据可以在诸如Child2类的其他Child对象之间共享和持久化。

Currently, I'm using static Parent class Fields to share data. 目前,我正在使用static父类字段共享数据。 I'm looking for some Implementation which can allow me to store data from Parent class to another object which can be used across all Child Test objects (If any other better way to share data collected in parent class to later test classes is also fine for me) 我正在寻找一些实现方法,可以让我将数据从父类存储到另一个对象,该对象可以在所有子级测试对象中使用(如果还有其他更好的方法可以将父类中收集的数据共享给以后的测试类,我)

//Making API calls to get some data which can be used //across multiple test classes //进行API调用以获取可以在多个测试类中使用的数据

private String config1;
private String config2;

public Parent() {
this.instance = getInstance();
}

private Parent(String config1, String config2) {
this.config1 = config1;
this.config2 = config2;

}

private static Parent instance = null;

public static Parent getInstance() {
if (instance == null) {
    synchronized (Parent.class) {
    if (instance == null) {
        instance = new Parent("SingleTonConfig1", "SingleTonConfig2");
    }
    }
}
return instance;
}

public String getConfig1() {
return config1;
}

public void setConfig1(String config1) {
this.config1 = config1;
}

public String getConfig2() {
return config2;
}

public void setConfig2(String config2) {
this.config2 = config2;
}

} }

import com.demo.config.Parent; 导入com.demo.config.Parent;

public class Child1 extends Parent { 公共类Child1扩展了Parent {

private String Child1Config1;
private String Child1Config2;

public Child1(String child1Config1, String child1Config2) {
super();
Child1Config1 = child1Config1;
Child1Config2 = child1Config2;
}

} }

public class Child2 extends Parent { 公共类Child2扩展了Parent {

private String Child2Config1;
private String Child2Config2;

public Child2(String child2Config1, String child2Config2) {
super();
Child2Config1 = child2Config1;
Child2Config2 = child2Config2;
}

} }

public static void main(String[] args) {
Child1 c1 = new Child1("child1Config1", "child1Config2");
Child1 c11 = new Child1("child1Config11", "child1Config22");

Child1 c2 = new Child1("child2Config1", "child2Config2");
Child1 c22 = new Child1("child2Config11", "child2Config22");

System.out.println("Parent object c1- config1" + c1.getInstance().getConfig1());

System.out.println("Parent object c1- config2" + c1.getInstance().getConfig2());

System.out.println("Parent object c11- config1" + c11.getInstance().getConfig1());

System.out.println("Parent object c11- config2" + c11.getInstance().getConfig2());

System.out.println("Parent object c2- config1" + c2.getInstance().getConfig1());

System.out.println("Parent object c2- config2" + c2.getInstance().getConfig2());

System.out.println("Parent object c22- config1" + c22.getInstance().getConfig1());

System.out.println("Parent object c22- config2" + c22.getInstance().getConfig2());

System.out.println("Parent Object c1- parentInstance: " + c1.getInstance());

System.out.println("Parent Object c11- parentInstance: " + c11.getInstance());

System.out.println("Parent Object c2- parentInstance: " + c2.getInstance());

System.out.println("Parent Object c22- parentInstance: " + c22.getInstance());
}

} }

When you will run the above main method you will see that the object always remains same since its singleton. 当您运行上述main方法时,您将看到该对象自单例以来始终保持不变。 But we have to give access to one public constructor where we will instantiate the object. 但是,我们必须访问一个公共构造函数,在该实例中将实例化该对象。 There are different approaches on how you can instantiate the instance like eager initialization ,static block initialization, and lazy initialization. 关于实例化实例的方法有很多,例如急切初始化,静态块初始化和延迟初始化。

So, here is explanation of my test suite. 因此,这是我的测试套件的说明。 I have exact below situation which is helping me a lot 我有以下情况,这对我有很大帮助

TestNg's @BeforeTest on base class only happening once per fixture 每个类的TestNg @BeforeTest在基类上只发生一次

public class TestBase {
    @BeforeTest
    public void before() {
        System.out.println("BeforeTest");
    }
}

public class TestClass extends TestBase {
    @Test
    public void test1(){}

    @Test
    public void test2(){}
}

public class TestClass1 extends TestBase {
    @Test
    public void test3(){}

    @Test
    public void test4(){}
}

Here, @BeforeTest playing a role of @BeforeSuite for me and I also want that particular piece to execute only once in lifetime of my suite. 在这里, @BeforeTest @BeforeSuite对我扮演了@BeforeTest的角色,我也希望该特定部分在我的套件的生命周期内仅执行一次。 first problem solved. 第一个问题解决了。

Second Problem: to share the data between multiple Test classes. 第二个问题:在多个测试类之间共享数据。 I got the solution here https://stackoverflow.com/a/38792029/1665592 我在这里找到解决方案https://stackoverflow.com/a/38792029/1665592

Set value: 设定值:

@Test
public void setvaluetest(ITestContext context) {
        String customerId = "GDFg34fDF";
        context.setAttribute("customerId", customerId);
}

Get value: 获得价值:

@Test
public void getvaluetest(ITestContext context) {
        String id = context.getAttribute("customerId");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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