Set the delay time for each <test> in TestNG

霸气de小男生 提交于 2021-01-15 19:43:58

问题


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?


回答1:


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.




回答2:


You can set priority or dependency on other method using the annotations as described here:

https://testng.org/doc/documentation-main.html#annotations

check for @Test.priority or @Test.dependsOnMethods

Doing this, will allow you run the tests one after the other serially.




回答3:


I don't know why you need to run same test class in separate tests, but when starting test it may load all relevant context which maybe take time and maybe after test end it may need to close some resources, both may take time/few seconds

You can use verbose to use more details of the cause of the delay (and add more logs to view timing)

<suite name="Suite" verbose="10">

The verbosity level is 0 to 10, where 10 is most detailed. Once you set it to 10, you’ll see that console output will contain information regarding the tests, methods, and listeners, etc.

To speed the process you can also use TestNG's parallel feature

The threadPoolSize attribute allows you to specify how many threads should be allocated for this execution.



来源:https://stackoverflow.com/questions/65285018/set-the-delay-time-for-each-test-in-testng

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!