簡體   English   中英

所有測試套件的TestNG運行方法一次

[英]TestNG run method once for all test suite

//deleted, see a refactoring at Update section.

我有編寫示例的這段代碼。 @BeforeClass批注出現時,它將值設置為我期望的值,並且繼承的TestsOther類將正確打印該值。 但是它每次都運行testInit方法,一次用於SomeTest ,一次用於OtherTest

因此,我想做的顯而易見的事情是將其更改為@BeforeTest批注,因此它將在所有測試之前僅對該方法進行一次評估,但隨后繼承的TestsOther類將為null

有人可以向我解釋為什么它會為null以及使testInit方法對所有套件僅運行一次的正確方法是什么。

謝謝。

更新:為了更加清楚,我對代碼進行了一些重構。

    public class TestsInit {

    protected String value;

    @BeforeClass
    public void testInit(){
        System.out.println("Entered testInit in TestsInit class,to set the value.");
        value="Something";
    }

    @Test
    public void SomeTest(){
        boolean isTrue=true;
        System.out.println("Entered SomeTest in TestsInit class.");
        Assert.assertTrue(isTrue);
    }
}

public class TestsOther extends TestsInit {

    @Test(dependsOnMethods = {"SomeTest"})
    public void OtherTest(){
        System.out.println("Entered OtherTest test in TestsOther class with value: "+value);
    }
}

我得到的輸出是:

Entered testInit in TestsInit class,to set the value.
Entered SomeTest in TestsInit class.
Entered testInit in TestsInit class,to set the value.
Entered SomeTest in TestsInit class.
Entered OtherTest test in TestsOther class with value: Something

我想看起來像這樣:

Entered testInit in TestsInit class,to set the value.
Entered SomeTest in TestsInit class.
Entered OtherTest test in TestsOther class with value: Something

因此,它將僅運行一次testInit方法,並在TestsOther測試之前運行SomeTest測試一次。 我需要將值存儲在父類中,以便所有孩子都可以使用它。 我只需要運行一次testInit,也只需運行一次SomeTest測試。

有什么建議么?

這是您的問題的答案:

我已經采用了自己的代碼,如下所示:

TestsInit類:

package Q45436872;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestsInit 
{

    protected String value;

    @BeforeClass
    public void testInit(){
    System.out.println("Entered testInit in TestsInit class,to set the value.");
    value="Something";
    }

    @Test
    public void SomeTest(){
    boolean isTrue=true;
    System.out.println("Entered SomeTest in TestsInit class.");
    Assert.assertTrue(isTrue);
    }
}

TestsOther類別:

package Q45436872;

import org.testng.annotations.Test;

public class TestsOther  extends TestsInit
{

    @Test (dependsOnMethods = {"SomeTest"})
    public void OtherTest(){
    System.out.println("Entered OtherTest test in TestsOther class with value: "+value);
    }
}

testng.xml

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <classes> <class name="Q45436872.TestsInit"/> <class name="Q45436872.TestsOther"/> </classes> </test> <!-- Test --> </suite> <!-- Suite --> 

現在,當我以TestNG Suite運行此安裝程序時,確實得到以下結果:

Entered testInit in TestsInit class,to set the value.
Entered SomeTest in TestsInit class.
Entered testInit in TestsInit class,to set the value.
Entered SomeTest in TestsInit class.
Entered OtherTest test in TestsOther class with value: Something

===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

我認為直到這里我們都在同一頁上。

需求:

現在,您的要求似乎是:

  1. 僅運行一次testInit方法。
  2. SomeTest測試之前運行一次TestsOther測試

解:

作為解決方案,我只是注釋了TestsInit類的強制執行。 所以我更新的testng.xml如下:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <classes> <!-- <class name="Q45436872.TestsInit"/> --> <class name="Q45436872.TestsOther"/> </classes> </test> <!-- Test --> </suite> <!-- Suite --> 

所以現在我只執行TestsOther類。 TestsOtherextends TestsInit 因此,由於testInit()@BeforeClass批注中,因此該方法將首先執行。

接下來,將執行@Test批注下的SomeTest() ,它符合您的要求。

最后,將執行TestsOther Class的@Test注釋下的OtherTest()方法(被標記為(dependsOnMethods = {"SomeTest"}) ),這再次符合您的要求。

我的控制台上的輸出是:

Entered testInit in TestsInit class,to set the value.
Entered SomeTest in TestsInit class.
Entered OtherTest test in TestsOther class with value: Something

此輸出完全符合您的要求。

讓我知道這是否回答了您的問題。

這兩個類都有測試,因此它們都是測試類,@ @BeforeClass將按類運行一次。 使用@BeforeTest ,您會看到null因為您有2個實例,但僅在1個實例上調用該方法(另一個實例將具有null值)。

如果只想調用一個不帶null的方法,則需要存儲並從ITestContext查找值。

@BeforeTest
public void testInit(ITestContext context) {
    context.setAttribute("value", "Something");
}

@Test(dependsOnMethods = {"SomeTest"})
public void OtherTest() {
    System.out.println(context.getAttribute("value"));
}

添加一個解釋靜態變量用法的示例

public class TestsInit {
    protected String value;
    private static boolean initialised = false;

    @BeforeClass
    public void testInit() {
        synchronized (TestsInit.class) {
            if (initialised) {
                return;
            }
            System.out.println("Entered testInit in TestsInit class,to set the value.");
            value = "Something";
            initialised = true;
        }
    }

    @Test
    public void SomeTest() {
        boolean isTrue = true;
        System.out.println("Entered SomeTest in TestsInit class.");
        Assert.assertTrue(isTrue);
    }
}

由於@BeforeClass按類運行一次,因此您的方案中有兩個類,因此它將運行兩次。

使用@BeforeClass注釋輸出

Entered testInit in TestsInit class,to set the value.
Entered SomeTest in TestsInit class.
Entered OtherTest test in TestsOther class with value: Something
Entered testInit in TestsInit class,to set the value.
Entered SomeTest in TestsInit class.
PASSED: SomeTest
PASSED: OtherTest
PASSED: SomeTest

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

如果只想運行一次TestsInit ,則將@BeforeClass替換為@BeforeSuite

使用@BeforeSuite注釋輸出

Entered testInit in TestsInit class,to set the value.
Entered SomeTest in TestsInit class.
Entered OtherTest test in TestsOther class with value: Something
Entered SomeTest in TestsInit class.
PASSED: SomeTest
PASSED: OtherTest
PASSED: SomeTest

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

注意SomeTest運行了兩次,這是因為TestsOther是另一個類,它擴展了TestsInit類,並且它只有一個方法依賴於SomeTest測試用例。

希望這會有所幫助。

要對所有類僅執行一次testInit方法,請嘗試在單獨的抽象類中進行初始化,並在需要時使用該類擴展其他類。

如果您收到這樣的輸出

東西

通過:SomeTest

通過:OtherTest

據了解,父類的@Test將在當前子類的@Test之前運行。 因為兩個類中都有@Test ,所以在每個類中都將執行@Test並在控制台上打印。

使用@BeforeTest批注,它也可以正常工作。(這意味着我沒有在此更改上得到null

如果在此建議的解決方案之后仍然為空,可以請您分享更多代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM