Transcripted Summary

# Executing without dependsOnMethods Attribute

In this example, we have seven test methods.


public class DependsOnMethods_No
{
	@Test
	public void test1_SetUpChrome() {}
	
	@Test
	public void test2_OpenOrangeHRM() {}
	
	@Test
	public void test3_SignIn() {}
	
	@Test
	public void test4_SearchUser() {}
	
	@Test
	public void test5_SearchEmployee() {}
	
	@Test
	public void test6_SearchCandidate() {}

	@Test
	public void test7_SignOut() {}
}

Let's add code to set up Chrome and then also code to open the application. However, we'll use an invalid URL for the application so that test2_OpenOrangeHRM fails.


public class DependsOnMethods_No
{
	WebDriver driver;

	@Test
	public void test1_SetUpChrome() 
	{
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\Rex Allen Jones II\\Downloads\\Drivers\\chromedriver.exe");
		driver = new ChromeDriver ();
		
		System.out.println("1. Set Up Chrome");
	}
	
	@Test
	public void test2_OpenOrangeHRM() 
	{
		//Invalid URL
		driver.get("https://opensource-demo.orangehrmlive1234.com/");
			
		Assert.assertEquals(false, true, "Could Not Access OrangeHRM");
		System.out.println("2. Open OrangeHRM");
	}
	
	@Test
	public void test3_SignIn() {}
	
	@Test
	public void test4_SearchUser() {}
	
	@Test
	public void test5_SearchEmployee() {}
	
	@Test
	public void test6_SearchCandidate() {}

	@Test
	public void test7_SignOut() {}
}

Let’s run our test script and see what happens. As expected, the site cannot be reached.

Console Output

Tests run: 7, Failures: 6, Skips: 0

On the console, we see there's a cascade of failures - that's when one failure forces the remaining tests in the suite to fail. We see 7 runs and 6 of those runs are failures.

This is not correct. Ideally, we should have 1 pass (test1_SetUpChrome), 1 failure (test2_OpenOrangeHRM), and the remaining 5 tests should be skipped.

To solve this problem, we can use the dependsOnMethods attribute.


# Execute with dependsOnMethods Attribute

If we go to the annotations package, we can find the dependsOnMethods attribute in the @Test annotation along with the groups and dependsOnGroups attributes. There are several others, but we'll only focus on these three within this course.


test attributes

To add the dependsOnMethod attribute to a test, we add a parentheses after the @Test annotation, write in "dependsOnMethod =" and then paste the name of the method that this test depends on.


	@Test
	public void test1_SetUpChrome ()
	{
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\Rex Allen Jones II\\Downloads\\Drivers\\chromedriver.exe");
		driver = new ChromeDriver ();
		
		System.out.println("1. Set Up Chrome");
	}
	
	//Invalid URL
	@Test (dependsOnMethods = "test1_SetUpChrome")
	public void test2_OpenOrangeHRM ()
	{		
		driver.get("https://opensource-demo.orangehrmlive1234.com/");
			
		Assert.assertEquals(false, true, "Could Not Open OrangeHRM");
		System.out.println("2. Open OrangeHRM");
	}

Here we have specified that test2_OpenOrangeHRM depends on test 1_SetUpChrome.

Let's add additional dependencies for the other methods.


	@Test
	public void test1_SetUpChrome() {}
	
	@Test (dependsOnMethods = "test1_SetUpChrome")
	public void test2_OpenOrangeHRM() {}
	
	@Test (dependsOnMethods = "test2_OpenOrangeHRM")
	public void test3_SignIn() {}
	
	@Test (dependsOnMethods = "test3_SignIn")
	public void test4_SearchUser() {}
	
	@Test (dependsOnMethods = { "test2_OpenOrangeHRM", "test3_SignIn" } )
	public void test5_SearchEmployee() {}
	
	@Test (dependsOnMethods = { "test2_OpenOrangeHRM", "test3_SignIn" } )
	public void test6_SearchCandidate() {}

	@Test (dependsOnMethods = { "test2_OpenOrangeHRM", "test3_SignIn" } )
	public void test7_SignOut() {}
}

There are some cases where a test needs to depend on more than one test method. This is the case for test5_SearchEmployee, test6_SearchCandidate, and test7_SignOut. In those cases, noticed we have used curly braces ({}) to enclose the methods that the test depends on.

If we run this now, we'll see 1 pass (test1_SetUpChrome), 1 fail(test2_OpenOrangeHRM) and the remaining 5 tests as skipped.


# Exclude Test Method via XML File

I've converted our class into this XML file:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test Automation University Suite">
  <test thread-count="5" name="Depends On Methods Attribute Test">
    <classes>
      <class name="com.testautomationu.chp6dependencytesting.DependsOnMethods_PASS">
    		<methods>
      			<exclude name = "test4_SearchUser"/>
      		</methods>
   		</class>
    </classes>
  </test> <!-- Depends On Methods Attribute Test -->
</suite> <!-- Test Automation University Suite -->


By default, a TestNG XML file does not include a <methods> tag because it’s optional. For our example, I added an opening and closing <methods> tag then used it to exclude a test method. That test method is test4_SearchUser.

Let’s imagine, everyone in the organization knows that searching for a user is not working. The defect has been reported and we will add it back to our test suite once the defect has been resolved. In the meantime, we don’t have to keep executing that test. We can exclude it for now.

If we run this, we'll see there are no failures or skips, and also that test4_SearchUser was not executed.



Resources



Quiz:

Note: Chapter 7 has been divided into multiple sub-chapters. You'll find the Quiz on the last sub-chapter, Chapter 7.3



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR