Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing output of one test method to another method testng

Tags:

java

testng

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

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

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

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.

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

like image 840
Rakesh Goyal Avatar asked Jun 25 '10 05:06

Rakesh Goyal


People also ask

How do you pass output of one test method to another in 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).

How can we skip a test case conditionally in TestNG?

Use throw new SkipException(String message) to skip a test. Conditional Skip – The user can have a conditional check. If the condition is met, it will throw SkipException and skip the rest of the code.

What is invocationCount in TestNG?

TestNG supports multi-invocation of a test method, i.e., a @Test method can be invoked multiple times sequentially or in parallel. If we want to run single @Test 10 times at a single thread, then invocationCount can be used. To invoke a method multiple times, the keyword invocationCount = <int> is required.


1 Answers

With the ITestContext object. It's a object available globally at the Suite context and disponible via 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);
    // ...
}
like image 152
Jorge Muñoz Avatar answered Nov 15 '22 18:11

Jorge Muñoz