📜  TestNG-套件测试

📅  最后修改于: 2020-11-10 04:58:58             🧑  作者: Mango


测试套件是旨在测试软件程序的一种或多种行为的测试用例的集合。在TestNG中,我们无法在测试源代码中定义套件,但是套件由一个XML文件表示,因为套件是执行的功能。它还允许运行测试的灵活配置。套件可以包含一个或多个测试,并由标记定义。

是您的testng.xml的根标记。它描述了一个测试套件,该套件又由几个部分组成。

下表列出了接受的所有合法属性。

Sr.No. Attribute & Description
1

name

The name of this suite. It is a mandatory attribute.

2

verbose

The level or verbosity for this run.

3

parallel

Whether TestNG should run different threads to run this suite.

4

thread-count

The number of threads to use, if parallel mode is enabled (ignored other-wise).

5

annotations

The type of annotations you are using in your tests.

6

time-out

The default timeout that will be used on all the test methods found in this test.

在本章中,我们将向您展示一个示例,该示例具有两个测试类Test1和Test2,可以使用Test Suite一起运行。

建立课程

C:\> JUNIT_WORKSPACE中创建一个要测试的Java类,例如MessageUtil.java

/*
* This class prints the given message on console.
*/

public class MessageUtil {
   private String message;

   // Constructor
   // @param message to be printed
   public MessageUtil(String message) {
      this.message = message;
   }

   // prints the message
   public String printMessage() {
      System.out.println(message);
      return message;
   }

   // add "Hi!" to the message
   public String salutationMessage() {
      message = "Hi!" + message;
      System.out.println(message);
      return message;
   }
}

创建测试用例类

C:\> TestNG_WORKSPACE中创建一个名为Test1.java的Java类文件。

import org.testng.Assert;
import org.testng.annotations.Test;

public class Test1 {
   String message = "Manisha";
   MessageUtil messageUtil = new MessageUtil(message);

   @Test
   public void testPrintMessage() {
      System.out.println("Inside testPrintMessage()");
      Assert.assertEquals(message, messageUtil.printMessage());
   }
}

C:\> TestNG_WORKSPACE中创建一个名为Test2.java的Java类文件。

import org.testng.Assert;
import org.testng.annotations.Test;

public class Test2 {
   String message = "Manisha";    
   MessageUtil messageUtil = new MessageUtil(message);
     
   @Test
   public void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Manisha";
      Assert.assertEquals(message,messageUtil.salutationMessage());
   }
}

现在,让我们在C:\> TestNG_WORKSPACE中编写testng.xml ,其中将包含标记,如下所示:






   
      
         
      
   
  
   
      
         
      
   
  
      

Suite1包括exampletest1exampletest2

使用javac编译所有Java类。

C:\TestNG_WORKSPACE>javac MessageUtil.java Test1.java Test2.java

现在,运行testng.xml,它将运行在提供的Test Case类中定义的测试用例。

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

验证输出。

Inside testPrintMessage()
Manisha
Inside testSalutationMessage()
Hi!Manisha

===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================

您还可以检查测试输出文件夹。在Suite1文件夹下,您可以看到创建了两个html文件,分别为exampletest1.html和exampletest2.html,如下所示-

写作测试

写作测试