1

I'm wondering what some other options are for accessing the test automation framework dependencies from my test classes.

Right now I have a layered architecture:

  1. Framework layer (contains all shared code for accessing deployed services)
  2. Harness layer (builds the classes and dependencies within the framework. Ie handles DI)
  3. Test layer (controls the flow of the tests via the objects created in the harness)

I've got a test base class in the harness layer that has access to the various API, database, and UI classes needed to run our tests. So using inheritance to provide access to these dependencies.

A sample test class would look like:

public class MyTestClass : TestBase
{
    [Test]
    public void sample_test()
    {
        APIs.MyAPI.GetSampleEndpoint(); // APIs is an inherited field in test base
    }
}

I feel inheritance is the best way to go about the issue of dependencies within a test class. The downside is managing the wrapping class that the "APIs" field is attached to.

Are there better options to go about managing a large list of dependencies in a clean way?

1
  • Yes, there are. Have you considered composition instead of inheritance?
    – dzieciou
    Commented Mar 16 at 11:09

1 Answer 1

0

TestNG allows you to specify dependencies either with −

a.Using attribute dependsOnMethods in @Test annotations, OR.

b.Using attribute dependsOnGroups in @Test annotations.

Example Using dependsOnMethods

  • Create a java class to be tested, say, MessageUtil.java

  • Create a java test class, say, DependencyTestUsingAnnotation.java in /work/testng/src.

  • Add test methods, testPrintMessage() and testSalutationMessage(), and initEnvironmentTest(), to your test class.

  • Add attribute dependsOnMethods = {"initEnvironmentTest"} to the @Test annotation of testSalutationMessage() method.

  • Create testng.xml

=>Compile the MessageUtil, Test case classes using javac.

=>Now, run the testng.xml, which will run the testSalutationMessage() method only after the execution of initEnvironmentTest() method.

Example Using dependsOnGroups

  • Create a java class to be tested, say, MessageUtil.java

  • Create a java test class, say, DependencyTestUsingAnnotation.java.

  • Add test methods, testPrintMessage() testSalutationMessage(), and initEnvironmentTest() to your test class, and add them to the group "init".

  • Add the attribute dependsOnGroups = {"init.*"} to the @Test annotation of testSalutationMessage() method.

  • Create testng.xml

=>Compile the MessageUtil, Test case classes using javac.

=>Now, run the testng.xml, which will run the testSalutationMessage() method only after the execution of initEnvironmentTest() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.