Dependency testing is one of the main features that separates TestNG from all other test frameworks. After reading Next Generation Java Testing, I came across a section called "Dependent" Code and it mentioned some developers in the testing community are strongly opposed to any hint of dependencies in their tests.
One of their arguments stated that as soon as test methods depend on other test methods, it becomes hard to run those methods in isolation. That argument was true until TestNG.
Now, we are allowed to run test methods in isolation and part of a dependency. TestNG provides a way to clearly indicate those dependencies. Therefore, it has no problem considering the requirements for any test method we want to run in isolation.
Attributes are used to assist annotations with our dependency testing. All three of these attributes can be placed in the configuration and test annotations:
dependsOn
groups
dependsOnGroups
So what exactly is dependency testing? Dependency testing is when two or more actions, two or more tasks, or two or more functions depend on the order of their methods. For example, we must sign into the application before we can sign out of the application. Therefore, signing out depends on us first signing in. If we, reverse that order and execute, then the sign out method would fail.
We have seen a form of dependency testing in Chapter 3 with annotations. The test methods depend on the configuration annotations. If a configuration annotation fails then the test methods would not pass. Here’s an example of a failed configuration. Let’s run.
public class FAIL_ConfigurationAnnotation
{
@BeforeClass
public void setUp() {}
@Test
public void signIn() {}
@Test
public void userSearch() {}
@Test
public void userSignOut() {}
@AfterClass
public void tearDown() {}
}
We see one configuration failure and one skip. The @BeforeClass
configuration failed and the @AfterClass
configuration was skipped.
Then we see the test methods. There are three runs, three skips, and no failures. The test methods were skipped because the @BeforeClass
configuration failed. At the core, that is dependency testing - the test methods were skipped and did not fail. You can get more information about Dependencies on TestNG website.
Quiz:
Note: Chapter 7 has been divided into multiple sub-chapters. You'll find the Quiz on the last sub-chapter, Chapter 7.3