简体   繁体   English

TestNG @AfterMethod 与 DataProvider

[英]TestNG @AfterMethod with DataProvider

I was playing around with TestNG and found that @AfterMethod & @BeforeMethod is getting invoked more than once when I'm using dataProvider.我在玩 TestNG,发现 @AfterMethod 和 @BeforeMethod 在我使用 dataProvider 时被多次调用。 Is it possible to invoke a method only once after the @Test got executed with all the parameters passed from dataProvider.是否可以在使用从 dataProvider 传递的所有参数执行 @Test 后仅调用一次方法。 Like can we invoke 'tearDown' method only once after 'testPrimeNumberChecker' got called for 5 times by dataProvider.就像我们可以在 dataProvider 调用 'testPrimeNumberChecker' 5 次之后只调用一次 'tearDown' 方法。

import org.testng.Assert;    
import org.testng.annotations.BeforeMethod;    
import org.testng.annotations.DataProvider;    
import org.testng.annotations.Test;    

public class ParamTestWithDataProvider1 {    
    private PrimeNumberChecker primeNumberChecker;    
    private static final Logger logger = Logger.getLogger(ParamTestWithDataProvider1.class);    

    @BeforeMethod    
    public void initialize() {    
        logger.info("Before Method Fired !! - " );    
        primeNumberChecker = new PrimeNumberChecker();    
    }    

    @@AfterMethod    
    public void tearDown() {    
        logger.info("After Method Fired !!  " );      
    }    

    @DataProvider(name = "test1")    
    public static Object[][] primeNumbers() {    
        return new Object[][] { { 2, true }, { 6, false }, { 19, true },    
            { 22, false }, { 23, true } };    
    }    

    @Test(dataProvider = "test1")    
    public void testPrimeNumberChecker(Integer inputNumber,    
        Boolean expectedResult) {    
    logger.info(inputNumber + " " + expectedResult);    
    Assert.assertEquals(expectedResult,    
            primeNumberChecker.validate(inputNumber));    
    }    
}

I want propose you a lazy idea: You can use a TestListener extended class and put tearDown method into a specific method of this class (ie tearDownListener).给大家提个懒人的想法:可以使用TestListener扩展类,把tearDown方法放到这个类的特定方法中(即tearDownListener)。
In onTestFailure and onTestSuccess you can increment a counter.在 onTestFailure 和 onTestSuccess 中,您可以增加一个计数器。
When TestListener intercept last test you can execute tearDownListener.当 TestListener 拦截最后一个测试时,您可以执行 tearDownListener。
Regards问候

One way can be to use @BeforeGroups and @AfterGroups.一种方法是使用@BeforeGroups 和@AfterGroups。 Make your test fall in a group and then use the Before/After groups annotation to do setup/teardown once for the tests.让您的测试属于一个组,然后使用 Before/After 组注释为测试进行一次设置/拆卸。

One of the workarounds is to use ITestNGMethod#hasMoreInvocation to check whether it is the last method invocation.一种解决方法是使用ITestNGMethod#hasMoreInvocation来检查它是否是最后一次方法调用。

    @AfterMethod(alwaysRun = true)
    public void afterMethod(ITestResult testResult) {
        if (!testResult.getMethod().hasMoreInvocation()) {
            // This will be executed once, after a test method has been invoked for all parameters sequences defined in the corresponding @DataProvider
        }
    }

Please make sure to add alwaysRun = true if you want the method to be executed even in case of failure.如果您希望即使在失败的情况下也能执行该方法,请确保添加alwaysRun = true

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM