簡體   English   中英

Testng-為所有數據提供者運行測試方法后如何運行清除代碼?

[英]Testng - How to run cleanup code after a test method is run for all data providers?

我有一個testng測試(如下),其中對數據提供者“ junkDP”給出的每個數組使用一個“ totalAmount”值。 我想重置“ totalAmount”,僅在為“ junkDP”中的每個數組運行“測試”方法之后。 在testng中這可能嗎? 怎么做 ?

請注意,@AfterMethod和@AfterTest不執行我想要的操作。 @AfterMethod在“ junkDP”中的第一個數組之后為每個數組運行“測試”方法之前,重置“ totalAmount”。 @AfterTest在@AfterClass之后運行。

編碼:

import org.testng.annotations.DataProvider;
import org.testng.annotations.*;

public class JUNKdP {

    @DataProvider( name = "junkDP")
    public static Object[][] junkDP() {
        Object [] [] dataSet = new Object[][] {
                new Object[] {1, 2},
                new Object[] {3, 4},
                new Object[] {5, 6}};
        return dataSet;
    }

}


public class JUNK {

    private int totalAmount = 0;

    @BeforeClass
    public void beforeClass(){System.out.println("BeforeClass\n");}

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("BeforeMethod\n");
    }

    @AfterMethod
    public void afterMethod(){
        System.out.println("AfterMethod\n");
    }

    @AfterTest
    public void afterTest(){
        System.out.println("AfterTest\n");
        this.totalAmount = 0;
        System.out.println("Reset total amount to 0");
    }

    @AfterClass
    public void afterClass(){System.out.println("AfterClass\n");}

    @Test(dataProvider = "junkDP", dataProviderClass = JUNKdP.class, enabled = true)
    public void test(int a, int b){
        System.out.println("Test method");
        int sum = a + b;
        System.out.println("Sum: " + sum);
        this.totalAmount = this.totalAmount + sum;
        System.out.println("totalAmount: " + this.totalAmount + "\n");
    }
}

輸出:

BeforeClass

BeforeMethod

Test method
Sum: 3
totalAmount: 3

AfterMethod

BeforeMethod

Test method
Sum: 7
totalAmount: 10

AfterMethod

BeforeMethod

Test method
Sum: 11
totalAmount: 21

AfterMethod

AfterClass

AfterTest

Reset total amount to 0

如果我理解這個問題,您是在測試方法之后搜索執行程序嗎?

...如果是,取決於測試跑步者

在這里,testng的工作方式:

http://testng.org/doc/documentation-main.html#annotations

@AfterTest或@AfterMethod

暫無
暫無

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

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