TestNG @Test 优先级 Selenium

TestNG 是一个意念波· 测试与验证 框架,涵盖不同类型的测试设计,如单元测试、功能测试、端到端测试、UI 测试和集成测试。

您可以在您的 测试 码。

如果在运行多个测试用例时未定义测试优先级, TestNG 将所有@Test 的优先级指定为零(0)。

现在,在运行时;较低的优先级将首先被安排。

演示 TestNG 无优先级的代码

让我们假设一个需要按顺序进行才能通过所有测试用例的场景:

场景: 生成一段代码,其中需要您执行以下操作: Google 使用特定关键词(例如“Facebook”)进行搜索。现在,确认浏览器标题已更改为“Facebook – Google 搜索”。

注意:您编写的每个步骤都应采用单独的方法

方法1:打开浏览器说 Firefox (打开浏览器())

方法2: 发射 Google.com(启动)Google())

方法3:使用“Facebook”执行搜索(performSearchAndClick1stLink())

方法4: 核实 Google 搜索页面标题(FaceBookPageTitleVerification())

Code 对于我们的场景:

import org.openqa.selenium.By;			
import org.openqa.selenium.WebDriver;			
import org.openqa.selenium.firefox.FirefoxDriver;			
import org.testng.Assert;			
import org.testng.annotations.Test;			

public class Priority_In_testNG {		
    WebDriver driver;			

	    // Method 1: Open Brower say Firefox			
	    @Test		
	    public void openBrowser() {				
	        driver = new FirefoxDriver();				
	    }		

	    // Method 2: Launch Google.com			
	    @Test		
	    public void launchGoogle() {				
	        driver.get("http://www.google.co.in");						
	    }		
        
	    // Method 3: Perform a search using "Facebook"			
	    @Test		
	    public void peformSeachAndClick1stLink() {				
	        driver.findElement(By.xpath(".//*[@title='Search']")).sendKeys("Facebook");								
	    }		

	    // Method 4: Verify Google search page title.			
	    @Test		
	    public void FaceBookPageTitleVerification() throws Exception {				
	        driver.findElement(By.xpath(".//*[@value='Search']")).click();						
	        Thread.sleep(3000);		
	        Assert.assertEquals(driver.getTitle().contains("Facebook - Google Search"), true);				
	    }		
	}		

的解释 Code

如上所述,我们创建了 4 个测试用例,用于以独立的方法执行每个操作。

  • 第一种方法 (打开浏览器) 要初始化的状态 Firefox 浏览器。
  • 第二种方法 (发射Google) 发射 Google.com 已在初始化的浏览器中。
  • 第三种方法 (执行搜索并点击第一个链接)在搜索框中执行搜索的状态(使用 xpath (“.//*[@title='搜索']”) 使用搜索词作为 Facebook
  • 第四个也是最后一个方法 (FaceBook页面标题验证) 点击搜索图标的州 Google 并确认浏览器标题已更改为 Facebook - Google 搜索。

现在使用 testNG 运行此代码,如视频中所示,你会发现所有的 测试用例 失败。失败原因:由于需要通过上一个测试用例,因此只有当前正在运行的测试用例才能通过。

在这种情况下,

  • 执行的第一个方法是 打开浏览器()。 由于它没有任何依赖关系,因此它已通过。
  • 执行的第二种方法是 FaceBookPageTitle验证(); 它失败了,因为我们正在尝试单击搜索按钮并验证浏览器标题。
  • 您可以看到,如果搜索活动没有进行,那么其他步骤如何通过。因此,这就是我的测试用例失败的原因。
PASSED: openBrowser
FAILED: FaceBookPageTitleVerification
FAILED: launchGoogle
FAILED: peformSeachAndClick1stLink

演示 TestNG 不按字母顺序排列的优先代码

如果我们没有提到任何优先级,testng 将按照方法名称的字母顺序执行 @Test 方法,而不管它们在代码中的实现位置。

package com.guru.testngannotations;

import org.testng.annotations.Test;

public class TestNG_Priority_Annotations {

@Test
public void c_method(){
System.out.println("I'm in method C");
}
@Test
public void b_method(){
System.out.println("I'm in method B");
}
@Test
public void a_method(){
System.out.println("I'm in method A");
}
@Test
public void e_method(){
System.out.println("I'm in method E");
}
@Test
public void d_method(){
System.out.println("I'm in method D");
}

}

输出

I'm in method A 
I'm in method B 
I'm in method C 
I'm in method D 
I'm in method E 

虽然我们以随机的方式定义方法(c,b,a,e,d),但 testng 会根据字母顺序根据方法名称执行方法,并且输出中也会反映出同样的情况。

如何设置优先级 TestNG

正如您在前面的例子中看到的,为了通过这个场景,需要进行排序,所以我们将修改前面的代码 优先级参数 这样每个测试都应按照分配给它们的优先级运行。

现在正如您所看到的,我们已经为每个测试用例分配了优先级,这意味着优先级较低的测试用例将首先执行。

testNG 中的优先级

import org.openqa.selenium.By;			
import org.openqa.selenium.WebDriver;			
import org.openqa.selenium.firefox.FirefoxDriver;			
import org.testng.Assert;			
import org.testng.annotations.Test;			

public class Priority_In_testNG {		
    WebDriver driver;			

    // Method 1: Open Browser say Firefox			
    @Test (priority=1)		
    public void openBrowser() {				
        driver = new FirefoxDriver();				
    }		

    // Method 2: Launch Google.com			
    @Test (priority=2)		
    public void launchGoogle() {				
        driver.get("http://www.google.co.in");						
    }		

    // Method 3: Perform a search using "Facebook"			
    @Test (priority=3)		
    public void peformSeachAndClick1stLink() {				
        driver.findElement(By.xpath(".//*[@title='Search']")).sendKeys("Facebook");								
    }		

    // Method 4: Verify Google search page title.			
    @Test (priority=4)		
    public void FaceBookPageTitleVerification() throws Exception {				
        driver.findElement(By.xpath(".//*[@value='Search']")).click();						
        Thread.sleep(3000);		
        Assert.assertEquals(driver.getTitle().contains("Facebook - Google Search"), true);				
    }		
}

的解释 Code

为每个测试用例分配优先级后,使用 testNG 运行上述代码,如下面提到的视频 2 所示。

在这里,您可以看到测试用例已按优先级排序。优先级较低的测试用例将首先执行,即现在根据测试用例的优先级顺序执行。因此,现在所有测试用例均已通过。

注意eclipse的控制台:

输出 :

PASSED: openBrowser
PASSED: launchGoogle
PASSED: peformSearchAndClick1stLink
PASSED: FaceBookPageTitleVerification

数字 0 具有最高优先级(它将被首先执行),并且优先级根据给定的数字进行,即 0 比 1 具有最高优先级。1 比 2 具有最高优先级,依此类推。

package com.guru.testngannotations;
import org.testng.annotations.Test;

public class TestNG_Priority_Annotations {

    @Test(priority=6)
    public void c_method(){
    System.out.println("I'm in method C");
    }
    @Test(priority=9)
    public void b_method(){
    System.out.println("I'm in method B");
    }
    @Test(priority=1)
    public void a_method(){
    System.out.println("I'm in method A");
    }
    @Test(priority=0)
    public void e_method(){
    System.out.println("I'm in method E");
    }
    @Test(priority=3)
    public void d_method(){
    System.out.println("I'm in method D");
    }

}

输出

I'm in method E 
I'm in method A 
I'm in method D 
I'm in method C 
I'm in method B

这里我们提供了优先级 0,1,3,6,9、0、1、XNUMX、XNUMX。因此,优先级为 XNUMX 的方法将首先执行,然后执行优先级为 XNUMX 的方法,依此类推。这里不会考虑按字母顺序排列的方法名称,因为我们提供了优先级

具有相同优先级的方法

方法可能具有相同的优先级。在这种情况下,testng 会考虑优先级相同的方法名称的字母顺序。

package com.guru.testngannotations;
import org.testng.annotations.Test;

public class TestNG_Priority_Annotations {

    @Test(priority=6)
    public void c_method(){
    System.out.println("I'm in method C");
    }
    @Test(priority=9)
    public void b_method(){
    System.out.println("I'm in method B");
    }
    @Test(priority=6)
    public void a_method(){
    System.out.println("I'm in method A");
    }
    @Test(priority=0)
    public void e_method(){
    System.out.println("I'm in method E");
    }
    @Test(priority=3)
    public void d_method(){
    System.out.println("I'm in method D");
    }

}

输出

I'm in method E 
I'm in method D 
I'm in method A 
I'm in method C 
I'm in method B 

这里,‘e’和‘d’根据它们的优先级值执行。但方法‘a’和‘c’包含相同的优先级值(6)。因此,这里 testng 会考虑‘a’和‘c’的字母顺序并相应地执行它们。

结合优先方法(具有相同的优先级)和非优先方法

在这种情况下,我们将在一个测试类中介绍两种情况。

  1. 具有相同优先级值的方法。
  2. 多种非优先方法。
package com.guru.testngannotations;

import org.testng.annotations.Test;

public class TestNG_Priority_Annotations {

	@Test()
	public void c_method(){
		System.out.println("I'm in method C");
	}
	@Test()
	public void b_method(){
		System.out.println("I'm in method B");
	}
	@Test(priority=6)
	public void a_method(){
		System.out.println("I'm in method A");
	}
	@Test(priority=0)
	public void e_method(){
		System.out.println("I'm in method E");
	}
	@Test(priority=6)
	public void d_method(){
		System.out.println("I'm in method D");
	}
}

输出:

I'm in method B 
I'm in method C 
I'm in method E 
I'm in method A 
I'm in method D
PASSED: b_method 
PASSED: c_method 
PASSED: e_method 
PASSED: a_method 
PASSED: d_method

说明:

首选: 非优先方法:“c”和“b”:根据字母顺序,首先执行“b”,然后执行“c”。

第二优先: 优先方法:'a'、'e' 和 'd':'e' 首先执行,因为它具有最高优先级(0)。由于 'a' 和 'd' 方法的优先级相同,testng 考虑了它们方法名称的字母顺序。因此,在它们之间,'a' 首先执行,然后是 'd'。

区分大小写 TestNG

仅供参考,在 testNG 中有一个定义优先级的标准语法,即 @Test(优先级=4),假设你用其他语法定义它 @测试(优先级=1) 那么你的 IDE 会将其显示为编译错误。请参阅下图:

区分大小写 TestNG

结语

正如您所看到的,如果需要按特定顺序运行一组测试用例,那么可以使用以下方法轻松完成 优先 使用testNG作为运行工具。

本教程得益于 Ramandeep Singh 和 Rama 的贡献 Krishna 加德

总结一下这篇文章: