将一种测试方法的输出传递给另一种方法testng [英] Passing output of one test method to another method testng

查看:208
本文介绍了将一种测试方法的输出传递给另一种方法testng的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在testng中编写以下单元测试用例:

I have to write the following unit test cases in testng:


  1. saveProductTest如果保存产品详细信息将返回productId在DB中成功。

  1. saveProductTest which would return productId if product details are saved successfully in DB.

modifyProductTest,它应该使用以前保存的productId作为参数。

modifyProductTest, it should use previously saved productId as a parameter.

我正在使用testNg数据提供程序从XML文件中获取saveProductTest和modifyProductTest方法的产品详细信息输入(PrdouctName,ReleaseDate)。由于productId是在save方法中生成的,我必须通过它修改方法。

I am taking the product details input(PrdouctName, ReleaseDate) for saveProductTest and modifyProductTest method from an XML file using testNg data providers.Since productId is generated in save method, I have to pass it to the modify method.

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

What is the best way to pass output of one test method to another method in testng.

推荐答案

对simendsjo充分尊重,所有测试应该相互独立的事实是一种教条式的方法,有很多例外。

With all due respect to simendsjo, the fact that all tests should be independent from each other is a dogmatic approach that has a lot of exceptions.

回到原始问题:1)使用依赖方法和2)将中间结果存储在一个字段中(TestNG不不要从头开始重新创建实例,因此该字段将保留其值。)

Back to the original question: 1) use dependent methods and 2) store the intermediate result in a field (TestNG doesn't recreate your instances from scratch, so that field will retain its value).

例如

private int mResult;

@Test
public void f1() {
  mResult = ...
}

@Test(dependsOnMethods = "f1")
public void f2() {
  // use mResult
}

这篇关于将一种测试方法的输出传递给另一种方法testng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆