Passing the output of one test method to another testng method

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

  • saveProductTest, which will return productId if product information is successfully saved to the database.

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

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

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

+7
java testng
source share
3 answers

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

Return to the original question: 1) use dependent methods and 2) save the intermediate result in the field (TestNG does not recreate your instances from scratch, so the field will save its value).

for example

private int mResult; @Test public void f1() { mResult = ... } @Test(dependsOnMethods = "f1") public void f2() { // use mResult } 
+13
source share

With an ITestContext object. This is an object that is available globally in the context of the Suite and provided through a parameter in each @Test.

For example:

 @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); // ... } 
+4
source share

Each unit test should be independent of other tests, so it’s easier for you to see what fails. You may have a helper method of saving the product and returning the identifier and calling this from both tests.

+3
source share

All Articles