Difference between BeforeClass and BeforeTest in TestNG

后端 未结 7 2296
旧巷少年郎
旧巷少年郎 2020-11-28 05:00

As we know from official TestNG documentation:

@BeforeClass: The annotated method will be run before the first test method in the current class is invok

7条回答
  •  悲哀的现实
    2020-11-28 05:57

    What is missing in the above answers is the usage difference between @BeforeClass and @BeforeTest annotations.

    As it is clear that the method (most commonly the setup method) annotated with @BeforeClass will execute only once before all the testcases written in the class. And the method annotated with '@BeforeTest' will execute before every testcase regardless of their count/sequence/underline logic.

    Therefore, @BeforeClass is used when our method has long calls with long execution time and the output of these calls will be unchanged by any of the testcases. For example, getting an API response in the setup method, which will be used by all the tests. Whereas, @BeforeTest@ is used when we need to do some cleanup stuff, and every test needs some fresh resource to start with. For example, one newly created order etc.

提交回复
热议问题