📜  TestNG-基本注释

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


在JUnit 3中指示测试方法的传统方法是在它们的名称前加上test。这是一种非常有效的方法,用于将类中的某些方法标记为具有特殊含义,但是命名的伸缩性不是很好(如果我们想为不同的框架添加更多标签,该怎么办?)并且相当不灵活(如果想要将其他参数传递给测试框架?)。

批注已正式添加到JDK 5中的Java语言中,TestNG决定选择使用批注对测试类进行批注。

这是TestNG支持的注释列表-

Sr.No. Annotation & Description
1

@BeforeSuite

The annotated method will be run only once before all tests in this suite have run.

2

@AfterSuite

The annotated method will be run only once after all tests in this suite have run.

3

@BeforeClass

The annotated method will be run only once before the first test method in the current class is invoked.

4

@AfterClass

The annotated method will be run only once after all the test methods in the current class have run.

5

@BeforeTest

The annotated method will be run before any test method belonging to the classes inside the tag is run.

6

@AfterTest

The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

7

@BeforeGroups

The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

8

@AfterGroups

The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

9

@BeforeMethod

The annotated method will be run before each test method.

10

@AfterMethod

The annotated method will be run after each test method.

11

@DataProvider

Marks a method as supplying data for a test method. The annotated method must return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.

12

@Factory

Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].

13

@Listeners

Defines listeners on a test class.

14

@Parameters

Describes how to pass parameters to a @Test method.

15

@Test

Marks a class or a method as a part of the test.

使用注释的好处

以下是使用注释的一些好处-

  • TestNG通过查找注释来标识其感兴趣的方法。因此,方法名称不限于任何模式或格式。

  • 我们可以将其他参数传递给注释。

  • 注释是强类型的,因此编译器将立即标记任何错误。

  • 测试类不再需要扩展任何内容(例如,对于JUnit 3,是TestCase)。