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

Create an object to share data between multiple TestNG classes

我已经与对象建立了以下关系

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

然后我继承测试 classes 作为

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

我还有第二个class作为

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

我的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>

但是,当我触发测试套件时,我的 Parent class API 数据收集逻辑得到正确调用,我还获得了可直接用于第一个 class 消耗它,即 Child1

但是,对于 Child2 class ,父 class 逻辑不会再次被调用 (即使我不希望它再次执行) 。问题是我在 Parent class 中收集的数据如何在 Child2.

等其他 Child 对象之间共享和持久化

目前,我正在使用 static 父 class 字段来共享数据。我正在寻找一些实现,它可以让我将数据从 Parent class 存储到另一个对象,该对象可以在所有 Child Test 对象中使用(如果有任何其他更好的方法来共享在 parent class 中收集的数据稍后测试 classes 对我来说也很好)

所以,这里是对我的测试套件的解释。我有以下确切的情况,这对我有很大帮助

TestNg's @BeforeTest on base class only happening once per fixture

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

在这里,@BeforeTest 对我来说扮演着 @BeforeSuite 的角色,我也希望那个特定的片段在我的套件的生命周期中只执行一次。第一个问题解决了。

第二个问题:在多个Test之间共享数据类。 我在这里找到了解决方案

设置值:

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

获取值:

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

//进行API调用以获取一些可以使用的数据 //跨越多个测试classes

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;
}

}

进口com.demo.config.Parent;

public class Child1 扩展了 Parent {

private String Child1Config1;
private String Child1Config2;

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

}

public class 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());
}

}

当您 运行 上面的主要方法时,您会看到该对象自其单例以来始终保持不变。但是我们必须访问一个 public 构造函数,我们将在其中实例化对象。 关于如何实例化实例有不同的方法,如急切初始化、静态块初始化和惰性初始化。