简体   繁体   English

Testng-为所有数据提供者运行测试方法后如何运行清除代码?

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

I have a testng test (below) where I use one value of "totalAmount" for each array given by the data provider "junkDP". 我有一个testng测试(如下),其中对数据提供者“ junkDP”给出的每个数组使用一个“ totalAmount”值。 I want to reset "totalAmount", ONLY after "test" method has run for each array in "junkDP". 我想重置“ totalAmount”,仅在为“ junkDP”中的每个数组运行“测试”方法之后。 Is this possible in testng ? 在testng中这可能吗? How to do it ? 怎么做 ?

Please note that @AfterMethod & @AfterTest do not do what I want. 请注意,@AfterMethod和@AfterTest不执行我想要的操作。 @AfterMethod resets "totalAmount" before "test" method is run for each array after the first array in "junkDP". @AfterMethod在“ junkDP”中的第一个数组之后为每个数组运行“测试”方法之前,重置“ totalAmount”。 @AfterTest is run after the @AfterClass. @AfterTest在@AfterClass之后运行。

The code: 编码:

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

Output: 输出:

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

If i understand the question, you are searching for executor after the test method ? 如果我理解这个问题,您是在测试方法之后搜索执行程序吗?

...if yes, depending on the test runner ...如果是,取决于测试跑步者

here you go, how testng is doing it: 在这里,testng的工作方式:

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

@AfterTest or @AfterMethod @AfterTest或@AfterMethod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在所有类@Test方法之后但在TestNG中的@AfterGroups之前运行代码 - Run code after all class @Test methods but before @AfterGroups in TestNG 所有测试套件的TestNG运行方法一次 - TestNG run method once for all test suite 在testng框架中的每种测试方法之后进行清理 - Cleanup after each test method in testng framework 在Testng中使用数据提供程序和并行方法,如何在给定测试的同一线程中在方法之前,方法之后和测试中运行? - Using data-provider and parallel in Testng, how to run before method, after method and test in same thread for a given test.? 如何在Eclipse的软件包中运行所有TestNG测试? - How to run all TestNG test in a package in Eclipse? 仅在 testng 中指定的测试方法之后运行带有 AfterMethod 注释的方法 - Run method annotated with AfterMethod only after specified Test method in testng 如何在运行方法完成后清理线程? - How do I cleanup a Thread after its run method finishes? 如何仅在标记测试后运行清理方法? - How can I run cleanup method only after tagged tests? 如何作为 TESTNG 测试文件运行 - How to Run as a TESTNG test file 如何使用 testng 对列表中的所有数据运行相同的测试用例? - How can I run the same test cases for all the data in a list using testng?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM