乐闻世界logo
搜索文章和话题

What is the default priority of a test method in TestNG?

1个答案

1

In TestNG, the default execution order of test methods is determined by the alphabetical order of method names. This means that when no priority or dependencies are explicitly specified, TestNG executes these test methods in alphabetical order from A to Z.

For example, consider the following three test methods:

java
@Test public void testB() { System.out.println("Running testB"); } @Test public void testA() { System.out.println("Running testA"); } @Test public void testC() { System.out.println("Running testC"); }

In the above scenario, even though testB() appears first in the code, testA() is executed first because its method name precedes it alphabetically. The execution order will be testA(), testB(), testC().

To control the execution order of test methods, you can explicitly specify the priority attribute:

java
@Test(priority = 2) public void testB() { System.out.println("Running testB"); } @Test(priority = 1) public void testA() { System.out.println("Running testA"); } @Test(priority = 3) public void testC() { System.out.println("Running testC"); }

With this configuration, TestNG will execute these methods in the specified priority order: testA() first, testB() next, and testC() last.

2024年8月14日 00:17 回复

你的答案