0
votes

Consider a scenario where I am testing a claims system. I want to submit quotes and check the total bill. Two or more claims in a day is supposed to earn me a bonus. So I need to check total amount after 1 claim, again after 2 claims, and then again after removing a claim and so on. The execution will be as below -

  1. Login
  2. Add 1 claim
  3. Calculate total receivable amount
  4. Add 1 more claim
  5. Calculate total receivable amount
  6. Remove a claim
  7. Calculate total receivable amount
  8. Logout

My TestNG.xml looks like this -

<test>
    <classes>
        <class name="Quotes">
            <methods>
                <include name="fLogin" />
                <include name="fAddQuotes" />
                <include name="fCheckTotal" />
                <include name="fAddQuotes" />
                <include name="fCheckTotal" />
                <include name="fRemoveQuotes" />
                <include name="fCheckTotal" />
                <include name="fLogout" />
            </methods>
        </class>
    </classes>
</test>

As mentioned, the function 'fCheckTotal' needs to be called multiple times in the same test. And I want to be able to push variable numbers of add/remove function in between.

But testNG is only executing the first occurence of the repeated methods (fAddQuotes, fCheckTotal).

Effectively the xml is doing this -

  1. Login
  2. Add 1 claim
  3. Calculate total receivable amount
  4. Remove a claim
  5. Logout

How can I solve/work around this?

3
A query identical to mine was posted before - stackoverflow.com/questions/15703499/… But there was no conclusive solution in that post. - Soumen Ghosh
Can testng.org/doc/documentation-main.html#factories be of any help? You can run multiple tests using it with different values. - girish-sortur
@Girish - Don't think so.. I can handle running 5 iterations of a method at once. The issue is when I need to run method A (n times), then execute some other methods, and run method A (x times) again. Including the same method name more than once in testNG.xml is not working (as shown in my question). - Soumen Ghosh
Hi @SoumenGhosh, this question is long time ago, but now I want to achieve the same scenario like yours with testng.xml. Have you found a way yet? - Ragnarsson

3 Answers

1
votes

I was facing the same problem and I found a workaround for this.

Modify your XML like the below. So have every class run as a test. This worked for me.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="1">
  <test name="Test">
      <classes>
       <class name="com.DemoOne"/>
       </classes>
   </test> <!-- Test -->
   <test name="Test1">
      <classes>
        <class name="com.DemoTwo"/>
       </classes>
  </test> <!-- Test -->
  <test name="Test2">
       <classes>
        <class name="com.DemoOne"/>
       </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
0
votes

Answer given by @bob in for question mentioned in your comment is I think most relevant.(https://stackoverflow.com/a/16259647/1431434)

You can't rely on testNG for flow control.

if you want your method to be executed in some specific order you will have to use dependsOn attribute as below other wise you may face issue reported under below question

https://stackoverflow.com/a/16259647/1431434

@Test
public void Test1() {

}

@Test (dependsOnMethods={"Test1"})
public void Test2() {

}

@Test (dependsOnMethods={"Test2"})
public void Test3() {

}

So first approach can be

define groups like login , changeLocation, FirstClaim, SecondClaim(make fAddQuotes and fCheckTotal part of firstClaim and SecondClaim group) and you can use these groups along with dependsOn tag. Which is likely to cause pain in longer term in term of maintainability

Other approach and better approach is

rather than defining all given methods with test annotation, keep them as supporting methods, and only use annotation against method which is testing end to end flow.

for example

 flogin(){}
 fAddQuotes(){}
 fCheckTotal(){}
 fRemoveQuotes(){}
 fLogout(){}


 @Test
 void Login()
 {
    fLogin()
    Assert
 }

 @Test
 void testAddQuote()
 {
    fLogin()
    fAddQuotes(){}
    fCheckTotal(){}  
 }
 @Test
 void testMultipleAddQuote()
 {
    fLogin()
    fAddQuotes(){}
    fCheckTotal(){}
    fChangeLocation(){}  
    fAddQuotes(){}
    fCheckTotal(){}
 }

I hope this will be helpful.

0
votes

If you are using TestNG, you need to use the invocationCount :

@Test(invocationCount=4)

Note: 4 is the number of times you want to run your method.