//
you're reading...
TestNG

Working with @TestInstance annotation

Sometimes one would need to gain access to the test class instance to which an @Test annotated test method belongs to, from within a data provider. This could be particularly useful in cases, wherein the data provider may need to behave differently based on the type of the class to which the @Test method belongs to.

This is where the @TestInstance annotation from TestNG comes into picture. This annotation is typically used on the parameter of a data provider method.

Here’s a sample data provider.

public static class LocalDataProvider {
    @DataProvider (name = "data-provider")
    public static Object[][] getData(@TestInstance Object object) {
        System.err.println("The instance passed in was " + object.getClass().getCanonicalName());
        return new Object[][] {
            {1},
            {2}
        };
    }
}

Here are two test classes that make use of the above cited data provider.

First test class

public static class FirstTestClass {
    @Test (dataProvider = "data-provider", dataProviderClass = LocalDataProvider.class)
    public void testmethod(int number) {
        System.err.println("Value = " + number);
    }
}

Second test class

public static class SecondTestClass {
    @Test (dataProvider = "data-provider", dataProviderClass = LocalDataProvider.class)
    public void testmethod(int number) {
        System.err.println("Value = " + number);
    }
}

Now when you run both the above mentioned test classes, you should see an output as below :

[TestNG] Running:
  /Users/krmahadevan/Library/Caches/IntelliJIdea2016.1/temp-testng-customsuite.xml
The instance passed in was organized.chaos.TestClassContainer.FirstTestClass
Value = 1
Value = 2

The instance passed in was organized.chaos.TestClassContainer.SecondTestClass
Value = 1
Value = 2

===============================================
Default Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================

Advertisements

Discussion

2 thoughts on “Working with @TestInstance annotation

  1. Hi Krishnan,

    It’s awesome that you spend your valuable time sharing the knowledge with others. I am a regular visitor of your blog and learnt a lot from your posts. Also i should mention that you have helped me many times answering my questions in the webdriver/testng forums. 🙂

    I didn’t understand this post “Working with @TestInstance annotation” – *This could be particularly useful in cases, wherein the data provider may need to behave differently based on the type of the class to which the @Test method belongs to.*, Can you give a real time example for this?

    Thanks for all your help!

    Posted by Jyothsna | July 6, 2016, 2:38 am
    • Well I have also not thought of an exact example.. But here’s one usecase that springs up in my mind. Lets imagine that you would like to build a generic Data provider which would resort to producing different type of objects based on the Test Class instance from which it was invoked from. In those cases, you could basically resort to this approach. The other example would be, lets say you have a common .xls file but the sheet name from which data is to be read depends on the Test class instance from which the call is being made. In that case also this would work.

      Posted by Confusions Personified | July 6, 2016, 7:50 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: