【问题标题】:testNG: How to capture return value of test case either in @AfterMethod or listener?testNG:如何在@AfterMethod 或侦听器中捕获测试用例的返回值?
【发布时间】:2012-10-04 16:21:17
【问题描述】:

只是为了设置上下文,我的测试用例创建了一些数据,我想在拆解时删除这些数据。我想以这样一种方式实现通用的拆卸过程(通过 AfterMethod/AfterClass/Listener),它会检查返回值(及其类型,主要是域对象)并清理数据(如下所示)。

public class SampleTest {

@AfterMethod
public void teardown(Object returnValueFromTest){
    //inspect returnValueFromTest and perform necessary clean up.
}

@Test
public String testEventGeneration(){
    //generate event

    //returning generated event id.
    return "E1234";
}

@Test
public Market testMarketGeneration(){
    //generate market

    //returning generated market.
    return someMarket;
}}

关于如何在 testNG 中实现这一点的任何想法/想法?我也考虑过实现像 IHookable 这样的监听器,但找不到可以帮助我获取返回值的监听器。

【问题讨论】:

    标签: testng


    【解决方案1】:

    你可以简单地处理这个,如果你想在所有测试执行后结束所有事情,你必须使用@AfterClass注解而不是@AfterMethod注解。 @AfterMethod 注解在测试类中的每个测试方法之后触发。

    流动的代码将简单地完成您的任务。如果您想在侦听器级别实现这一点,您可以实现 ITestListener 和 onFinish 方法,您可以实现您的逻辑。

    公共类 SampleTest { 私有字符串 event_id = null;

    @AfterClass(alwaysRun = true)
    public void teardown(){
        //delete event from the db(event_id) 
    
    }
    
    @Test
    public void testEventGeneration(){
        //generate event
    
        //returning generated event id.
        event_Id= "E1234";
    }}
    

    【讨论】:

    • 感谢您的回答,但这不是我想要的。我想以检查返回值(及其类型,主要是域对象)并清理数据的方式实现拆卸过程(通过 AfterMethod/AfterClass/Listener)。换句话说,根据从测试用例返回的域对象,拆卸方法/侦听器将知道要删除什么并执行清理。这里的关键是自动化拆卸过程,我不想在每个测试中维护本地实例只是为了拆卸,除非没有其他更好的方法。
    【解决方案2】:

    如果有与它们关联的返回值,AFAIK Testng 甚至不会执行注释为 Test 的方法。

    因此,如果您想根据设置的值进行拆解,那么您需要在全局上下文或外部设置这些值,而不是作为返回值。您可以根据您想要拆卸的时间将拆卸逻辑编写为侦听器或任何 After 方法。

    【讨论】:

    • 其实你可以让测试用例在testNG中返回值。
    【解决方案3】:

    您是否考虑过使用 InvokedMethodListener2?

    public Class BaseTestNg {
    
        ITestContext ctx;
    
        public ITestContext getContext(){
            return this.ctx;
        }
    
        @BeforeClass
        public void setContext( final ITestContext ctx ) {
            this.ctx = ctx;
        }
    }
    
    public Class SampleTest extends BaseTestNg {
    
        @Test
        public Market testMarketGeneration(){
            //generate market
            this.getContext().setAttribute("market", someMarket);
            //returning generated market.
            return someMarket;
        }
    }
    
    
    import org.testng.IInvokedMethod;
    import org.testng.IInvokedMethodListener2;
    import org.testng.ITestContext;
    import org.testng.ITestResult;
    
    public class InvokedMethodListener2Impl implements IInvokedMethodListener2 {
    
        @Override
        public void afterInvocation( final IInvokedMethod method, final ITestResult testResult ) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void afterInvocation( final IInvokedMethod method, final ITestResult testResult, final ITestContext context ) {
    
            //read object from context
            //Market = (Market) context.getAttribute("market");
    
            // testResult.setStatus( ITestResult.FAILURE );
    
        }
    
        @Override
        public void beforeInvocation( final IInvokedMethod method, final ITestResult testResult ) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void beforeInvocation( final IInvokedMethod method, final ITestResult testResult, final ITestContext context ) {
            // TODO Auto-generated method stub
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多