English version (07)-Test methods, test classes, and test groups (5.7-5.12)

Source: Internet
Author: User
Tags testng
5.7-factory

The factory allows you to dynamically create tests. For example, suppose you need to create a test method and use it to access a Web page multiple times, each time with different parameters:

public class TestWebServer {
  @Test(parameters = { "number-of-times" })
  public void accessPage(int numberOfTimes) {
    while (numberOfTimes-- > 0) {
     // access the web page
    }
  }
}

Testng. xml:

<test name="T1">
  <parameter name="number-of-times" value="10"/>
  <class name= "TestWebServer" />
</test>

<test name="T2">
  <parameter name="number-of-times" value="20"/>
  <class name= "TestWebServer"/>
</test>

<test name="T3">
  <parameter name="number-of-times" value="30"/>
  <class name= "TestWebServer"/>
</test>

Once there are too many parameters, it is difficult to manage them, so we should use the factory to do it:

public class WebTestFactory {
  @Factory
  public Object[] createInstances() {
   Object[] result = new Object[10];  
   for (int i = 0; i < 10; i++) {
      result[i] = new WebTest(i * 10);
    return result;
  }
}

The new test classes are as follows:

public class WebTest {
  private int m_numberOfTimes;
  public WebTest(int numberOfTimes) {
    m_numberOfTimes = numberOfTimes;
  }

  @Test
  public void testServer() {
   for (int i = 0; i < m_numberOfTimes; i++) {
     // access the web page
    }
  }
}

YourTestng. xmlYou only need to reference the class containing the factory method, and the test instance will create at runtime:

<class name="WebTestFactory" />

Factory methods can accept such@ TestAnd@ Before/afterThe marked parameters are returned.Object []. These returned objects can be any class (not necessarily the same class as the factory method), and they do not even need the testng annotation (in this example, it will be ignored by testng)

5.8-class-level Annotation

Usually @ TestIt can also be used to label classes, not just methods:

@Test
public class Test1 {
  public void test1() {
  }

  public void test2() {
  }
}

Class-level@ TestAll public methods in the class will be used as test methods, regardless of whether they have been labeled. Of course, you can still use@ TestRepeat the annotation test method, especially when you want to add some special attributes to it.

For example:

@Test
public class Test1 {
  public void test1() {
  }

  @Test(groups = "g1")
  public void test2() {
  }
}

In the above exampleTest1 ()AndTest2 ()Are processed, but above thisTest2 () still belongs to the group"G1 ".

 

5.9-Parallel Running on timeout

You can useParallelAttribute to run the test method in different threads. This property can contain the following values:

<suite name="My suite" parallel="methods" thread-count="5">
<suite name="My suite" parallel="tests" thread-count="5">
<suite name="My suite" parallel="classes" thread-count="5">
  • Parallel = "methods": Testng will run the test method in different threads, unless the methods that depend on each other. The interdependent methods run in the same thread and follow the execution sequence.
  • Parallel = "tests": Testng will run all the methods under the same <Test> label in the same thread, but all the methods in each <Test> label will run in different threads. In this way, you can group all non-thread-safe classes under the same <Test> label, and make use of the features of testng multithreading, run these classes in the same thread.
  • Parallel = "classes": Testng will run all the methods in the same class in the same thread, but each class will run in different threads.

In addition, attributesThread-countYou can specify the number of threads that can be run for the current execution.

Note:@ TestProperties inTimeoutIt can work in parallel and non-parallel modes.

You can also specify@ TestMethods are called in different threads. You can use attributesThreadpoolsizeTo achieve:

@Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
public void testServer() {

In the above example, the methodTestserverIt will be called 10 times in three threads. In addition, the 10-second timeout setting ensures that none of the three threads will ever block the currently called threads.

5.10-test of re-running failure

Testng createsTestng-failed.xml. This XML file contains the necessary information for re-running the failed test, so that you can quickly re-run the failed test without running the entire test. Therefore, a typical session looks like:

java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs testng.xml
java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs test-outputs/testng-failed.xml

Note that,Testng-failed.xmlThe dependencies required for running all failed methods are included. Therefore, it can be ensured that no Skip is generated for the method that failed last time.

5.11-JUnit Test

Testng can run the JUnit test. All you have to do isTestng. classnamesSet the JUnit test class to run in the property, and setTestng. JUnitSet property to true:

<test name="Test1"   junit="true">
  <classes>
    <!-- ... -->

Testng is similar to JUnit in this case:

  • The test method to be run in all classes starts with test *.
  • If there is a setup () method in the class, it will be called before each test method is executed
  • If the class contains the teardown () method, it will be called after each test method
  • If the test class contains the suite () method, all the test classes returned by this method will be called.
5.12-JDK 1.4

Testng can also work under JDK 1.4. You need to use JDK 1.4 jar, which can be found in the releaseTestng-...-jdk14.jar). The only difference is the annotation method, which uses the popular XDoclet javadoc annotation Syntax:

public class SimpleTest {
 /**
  * @testng.before-class = "true"
  */
  public void setUp() {
    // code that will be invoked when this test is instantiated
 }
 /**
  * @testng.test groups = "functest" dependsOnGroups = "group1,group2"
  */
  public void testItWorks() {
    // your test code
 }
}

The javadoc syntax rules are quite direct, and the only difference with JDK 1.5 annotations is that the string array is expressed as a single comma or space separated form. However, double quotation marks are optional, but I strongly recommend that you use double quotation marks so that it is easier to migrate to JDK 1.5 in the future.

You also need<Testng> specified in tag SourcedirAttribute (or-Sourcedir is in the command line ).Testng can find the test file in order and parse the javadoc annotation.

Subscript is to compare the similarities and differences between JDK 1.4 and JDK 5 annotations.

JDK 5 JDK 1.4
@ Test (groups = {"A", "B "},
Dependsonmethods = {"M1 "})

/**
* @ Testng. test groups = "a B"
* Dependsonmethods = "M1"
*/

@ Aftermethod (alwaysrun = true)

/**
* @ Testng. Before-method alwaysrun = "true"
*/
@ Parameters ({"first-name", "last-name "})
/**
* @ Testng. parameters value = "first-name last-name"
*/
@ Factory
@ Parameters ({"first-name", "last-name "})

/**
* @ Testng. Factory
* @ Testng. parameters value = "first-name last-name"
*/
@ Dataprovider (name = "test1 ") /**
* @ Testng. Data-provider name = "test1"
*/

For the example and support of testng's JDK 1.4, seeTest-14/Directory, which contains the complete description of JDK 1.5 being mapped to the javadoc annotation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.