Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

First as info: I put each @Test in a different class (so of course each class only has 1 @Test annotation).

Actually my goal is to want to rerun the same class with different parameter, but I want to run another class first beforehand.

I've tried to find many references that TestNG doesn't allow repeat of a class or a @Test method annotation in one <test>. The repeat provided is an invocationCount function, I see about invocationCount, but I can't achieve my goal with invocationCount because this function repeats a @Test at the same time and then I can run another @Test.

public class SimpleTest1 {
    @Test
    @Parameters({"acc"})
    public void historyTransfer(String acc) {
        System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
    }
}
public class SimpleTest2 {
    @Test
    @Parameters({"senderAcc", "beneficiaryAcc", "amount"})
    public void tranfer(String senderAcc, String beneficiaryAcc, String amount) {
        System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
    }
}

I imagine to run like bellow configuration:

<suite name="Suite">
    <test name="My Test" >
        <classes>
            <class name="com.SimpleTest1">
                <parameter name="acc" value="11111"></parameter>
            </class>
            <class name="com.SimpleTest2">
                <parameter name="senderAcc" value="11111"></parameter>
                <parameter name="beneficiaryAcc" value="22222"></parameter>
                <parameter name="amount" value="100"></parameter>
            </class>
            <class name="com.SimpleTest1">
                <parameter name="acc" value="22222"></parameter>
            </class>
        </classes>
    </test>
</suite>

But the above configuration didn't go as planned because the second SimpleTest1 was not executed.

Then I tried running it in a separate <test> like bellow and success, but I'm facing new issue about delay time each <test>.

Run multiple <test> serially (not parallel) as follows:

<suite name="Suite">
    <test name="My Test1" >
        <classes>
            <class name="com.SimpleTest1">
                <parameter name="acc" value="11111"></parameter>
            </class>
        </classes>
    </test>
    <test name="My Test2" >
        <classes>
            <class name="com.SimpleTest2">
                <parameter name="senderAcc" value="11111"></parameter>
                <parameter name="beneficiaryAcc" value="22222"></parameter>
                <parameter name="amount" value="100"></parameter>
            </class>
        </classes>
    </test>
    <test name="My Test3" >
        <classes>
            <class name="com.SimpleTest1">
                <parameter name="acc" value="22222"></parameter>
            </class>
        </classes>
    </test>
</suite>

TestNG Maven dependency:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.0.0</version>
    <scope>compile</scope>
</dependency>

Surefire:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
      </configuration>
</plugin>

IDE : Eclipse (Version: 2018-09 (4.9.0))

OS : macOS Mojave (Version 10.14.6)

Output:

[RemoteTestNG] detected TestNG version 7.0.0
22-12-2020 21:59:32
22-12-2020 21:59:47
22-12-2020 21:59:57

===============================================
Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

But after the first <test> is finished, there is a delay of about 10 seconds before the next <test> runs, as well as the next test.

Note: I thought this was a problem with the IDE (I use Eclipse), but it wasn't. I've tried running it in 2 ways, via the IDE and the command line, and give me same result about this delay issue.

Via command line using this command :

mvn clean test -Dsurefire.suiteXmlFiles=testng.xml

Is there any configuration to reduce the delay time above?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
110 views
Welcome To Ask or Share your Answers For Others

1 Answer

First, a suite is not a test plan (could be a good feature request btw) but just a way to select tests. It means you can't define a dependency between tests. That's why having the same test class doesn't work (it should fail or create different instances).

As I understand your needs, the best way is to separate your own logic and its integration with the test framework:

  1. design your helper/fixture classes as you want:

_

public class SimpleClass1 {
  public void historyTransfer(String acc) {
    System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
  }
}

public class SimpleClass2 {
  public void tranfer(String senderAcc, String beneficiaryAcc, String amount) {
    System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
  }
}
  1. define the classes for the integration with the test framework

_

public class SimpleTest {
  @Test
  @Parameters({"acc"})
  public void step1(String acc) {
    (new SimpleClass1()).historyTransfer(acc);
  }

  @Test(dependsOnMethods = {"step1"})
  @Parameters({"senderAcc", "beneficiaryAcc", "amount"})
  public void step2(String senderAcc, String beneficiaryAcc, String amount) {
    (new SimpleClass2()).transfer(acc);
  }

  @Test(dependsOnMethods = {"step2"})
  @Parameters({"acc"})
  public void step3(String acc) {
    (new SimpleClass1()).historyTransfer(acc);
  }
}

And the suite with the expected parameters:

<suite name="Suite">
  <test name="My Test" >
    <classes>
        <class name="com.SimpleTest">
            <methods>
              <include name="step1">
              <parameter name="acc" value="11111"></parameter>
            </methods>
            <methods>
              <include name="step2">
              <parameter name="senderAcc" value="11111"></parameter>
              <parameter name="beneficiaryAcc" value="22222"></parameter>
              <parameter name="amount" value="100"></parameter>
            </methods>
            <methods>
              <include name="step3">
              <parameter name="acc" value="22222"></parameter>
            </methods>
        </class>
    </classes>
  </test>
</suite>

(disclaimer: I didn't check the XML against the dtd, could be wrong but you have the idea)

The naming or the way to create fixtures depends on your own conventions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...