簡體   English   中英

創建一個對象以在多個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
}

然后我將Test類繼承為

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

但是,對於Child2類,不會再次調用Parent類邏輯(即使我不希望它再次執行) 問題是我如何在Parent類中收集的數據可以在諸如Child2類的其他Child對象之間共享和持久化。

目前,我正在使用static父類字段共享數據。 我正在尋找一些實現方法,可以讓我將數據從父類存儲到另一個對象,該對象可以在所有子級測試對象中使用(如果還有其他更好的方法可以將父類中收集的數據共享給以后的測試類,我)

//進行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方法時,您將看到該對象自單例以來始終保持不變。 但是,我們必須訪問一個公共構造函數,在該實例中將實例化該對象。 關於實例化實例的方法有很多,例如急切初始化,靜態塊初始化和延遲初始化。

因此,這是我的測試套件的說明。 我有以下情況,這對我有很大幫助

每個類的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對我扮演了@BeforeTest的角色,我也希望該特定部分在我的套件的生命周期內僅執行一次。 第一個問題解決了。

第二個問題:在多個測試類之間共享數據。 我在這里找到解決方案https://stackoverflow.com/a/38792029/1665592

設定值:

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

獲得價值:

@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