tags:

views:

46

answers:

3

Hi,

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.

Thanks in advance.

+1  A: 

Each unit test should be independent of other tests so you more easily can see what fails. You can have a helper method saving the product and returning the id and call this from both tests.

simendsjo
+2  A: 

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.

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).

For example

private int mResult;

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

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

I am facing a similar problem. I have 2 methods and these methods are in different classes..

Class A

{

protected String var1;

Method 1()

{

var1 = value is generated dynamically using the current time stamp }

}

Class B extends A

{

need to retrieve the value of var1 here first

}

When I try to access the value of var1 in class B, it displays a null value.

Can someone please suggest a solution to this problem?