为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

凤凰求蛊 2023-07-19 10:54:56
我已经创建了与对象的以下关系public class Parent {  //Making API calls to get some data which can be used  //across multiple test classes}然后我将测试类继承为public class Child1 extends Parent { //I need data collected in Parent 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类 API 数据收集逻辑被正确调用,我还获得了可直接用于使用它的第一类的数据,即Child1但是,对于Child2class ,父类逻辑不会再次被调用(即使我不希望它再次执行)。问题是我在 Parent 类中收集的数据如何在其他 Child 对象(例如Child2.目前,我正在使用static父类字段来共享数据。我正在寻找一些实现,它可以允许我将数据从父类存储到另一个对象,该对象可以在所有子测试对象中使用(如果有任何其他更好的方法将父类中收集的数据共享到以后的测试类也可以)我)
查看完整描述

2 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

//进行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;

}

}


导入com.demo.config.Parent;


公共类 Child1 扩展 Parent {


private String Child1Config1;

private String Child1Config2;


public Child1(String child1Config1, String child1Config2) {

super();

Child1Config1 = child1Config1;

Child1Config2 = child1Config2;

}

}


公共类 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());

}

}


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


查看完整回答
反对 回复 2023-07-19
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

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

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

}

在这里,为我@BeforeTest扮演一个角色@BeforeSuite,我也希望这个特定的部分在我的套件的生命周期中只执行一次。第一个问题解决了。

第二个问题:在多个测试类之间共享数据。 

设定值:

@Test

public void setvaluetest(ITestContext context) {

        String customerId = "GDFg34fDF";

        context.setAttribute("customerId", customerId);

}

获取值:


@Test

public void getvaluetest(ITestContext context) {

        String id = context.getAttribute("customerId");

}


查看完整回答
反对 回复 2023-07-19
  • 2 回答
  • 0 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信