代码之家  ›  专栏  ›  技术社区  ›  Rakesh Goyal

将一个测试方法的输出传递给另一个方法testng

  •  7
  • Rakesh Goyal  · 技术社区  · 15 年前

    1. saveProductTest,如果产品详细信息成功保存在数据库中,它将返回productId。

    在testng中,将一个测试方法的输出传递给另一个方法的最佳方法是什么。

    3 回复  |  直到 9 年前
        1
  •  23
  •   Cedric Beust    15 年前

    对于simendsjo来说,所有测试都应该相互独立,这是一种教条式的方法,有很多例外。

    例如

    private int mResult;
    
    @Test
    public void f1() {
      mResult = ...
    }
    
    @Test(dependsOnMethods = "f1")
    public void f2() {
      // use mResult
    }
    
        2
  •  10
  •   Dragan Bozanovic    9 年前

    ITestContext 对象。它是一个在套件上下文中全局可用的对象,可以通过每个@Test中的参数进行disponsible。

    例如:

    @Test 
    public void test1(ITestContext context, Method method) throws Exception {
        // ...
        context.setAttribute(Constantes.LISTA_PEDIDOS, listPaisPedidos);
        // ...
    }
    
    @Test
    public void test2(ITestContext context, Method method) throws Exception {
        List<PaisPedido> listPaisPedido = (List<PaisPedido>)
        context.getAttribute(Constantes.LISTA_PEDIDOS);
        // ...
    }
    
        3
  •  3
  •   simendsjo    15 年前

    每个单元测试应该独立于其他测试,这样您就可以更容易地看到失败的部分。您可以使用一个helper方法保存产品并返回id,然后从两个测试中调用它。