TestNG @테스트 우선순위 Selenium

TestNG 하는 지원 단위 테스트, 기능 테스트, 엔드 투 엔드 테스트, UI 테스트 및 통합 테스트와 같은 다양한 유형의 테스트 디자인을 다루는 프레임워크입니다.

단일 또는 다중 테스트 사례를 실행할 수 있습니다. 테스트 중 암호.

여러 테스트 케이스를 실행하는 동안 테스트 우선순위가 정의되지 않은 경우, TestNG 모든 @Test에 우선순위를 0(XNUMX)으로 할당합니다.

이제 달리는 동안; 낮은 우선순위가 먼저 예약됩니다.

데모 TestNG 우선순위가 없는 코드

모든 테스트 사례를 통과하기 위해 시퀀싱이 필요한 시나리오를 생각해 보겠습니다.

시나리오 : 특정 작업을 수행해야 하는 코드를 생성하세요. Google 특정 키워드로 검색하려면 "페이스북"이라고 말하세요. 이제 브라우저 제목이 “페이스북 –”으로 변경되었는지 확인하세요. Google 찾다".

참고: 코딩하는 각 단계는 별도의 메서드에 있어야 합니다.

방법 1: 브라우저 열기 Firefox (오픈브라우저())

방법 2: 시작하다 Google.com (출시)Google())

방법 3: “페이스북”을 이용하여 검색합니다. (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 "페이스북"			
	    @Test		
	    public void peformSeachAndClick1stLink() {				
	        driver.findElement(By.xpath(".//*[@title='Search']")).sendKeys("페이스북");								
	    }		

	    // 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("페이스북 - Google Search"), true);				
	    }		
	}		

설명 Code

위에서 언급한 대로 우리는 독립적인 방법으로 각 작업을 수행하기 위한 4개의 테스트 사례를 만들었습니다.

  • 첫 번째 방법 (오픈브라우저) 초기화할 상태 Firefox 브라우저.
  • 두 번째 방법 (시작하다Google) 출시하는 주 Google.com은 초기화된 브라우저에 있습니다.
  • 세 번째 방법 (peformSeachAndClick1stLink)검색창에서 검색을 수행하는 상태(xpath 사용) (“.//*[@title='검색']”) 다음과 같은 검색어로 수면 북
  • 네 번째이자 마지막 방법 (FaceBookPageTitle인증) 검색 아이콘을 클릭하는 상태를 나타냅니다. Google 브라우저 제목이 변경되었는지 확인하십시오. 페이스북 – Google 검색 할 수 있습니다.

이제 비디오에 표시된 대로 testNG를 사용하여 이 코드를 실행하면 모든 것을 찾을 수 있습니다. 테스트 케이스 실패하고 있습니다. 실패 이유: 통과하려면 이전 테스트 케이스의 종속성이 있으므로 현재 실행 중인 테스트 케이스만 통과됩니다.

이 경우,

  • 실행되는 첫 번째 방법은 오픈브라우저(). 종속성이 없기 때문에 통과되었습니다.
  • 두 번째로 실행되는 방법은 다음과 같습니다. FaceBookPageTitleVerification(); 검색 버튼을 클릭하고 브라우저 제목을 확인하는 중이므로 실패합니다.
  • 검색 활동이 처리되지 않은 경우 다른 단계를 어떻게 통과할 수 있는지 확인할 수 있습니다. 따라서 이것이 내 테스트 사례가 실패한 이유입니다.
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 "페이스북"			
    @Test (priority=3)		
    public void peformSeachAndClick1stLink() {				
        driver.findElement(By.xpath(".//*[@title='Search']")).sendKeys("페이스북");								
    }		

    // 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("페이스북 - Google Search"), true);				
    }		
}

설명 Code

각 테스트 케이스에 우선순위를 지정한 후, 아래 영상-2에서 보이는 것처럼 testNG를 사용하여 위 코드를 실행합니다.

여기서는 테스트 케이스의 우선순위가 높다는 것을 알 수 있습니다. 우선순위가 낮은 테스트 케이스가 먼저 실행됩니다. 즉, 이제 테스트 케이스의 우선순위에 따라 순차적으로 실행됩니다. 따라서 현재 모든 테스트 케이스가 통과되었습니다.

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인 메소드가 실행됩니다. 여기서는 우선순위를 제공했으므로 알파벳 순서 방법 이름은 고려되지 않습니다.

동일한 우선순위를 가진 메소드

메서드가 동일한 우선순위를 가질 가능성이 있습니다. 그런 경우, 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 가드

이 게시물을 요약하면 다음과 같습니다.