Way to check which group is running

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Way to check which group is running

André Santos
Hello,

I've been trying to find a solution to understand which group I'm running at a certain point. I want to use this to dynamically change my data provider based on the group running. So far, I've found only solutions that will show me all groups running, like:

public Object[][] testDataProvider(ITestContext context) {
    List<String> includedGroups = Arrays.asList(context.getIncludedGroups());

But if I'm running two groups at the same time, I'll never be able to distinguish this in a fashion manner.

Is there any way to find this out?

Thanks,
André

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Way to check which group is running

Krishnan Mahadevan

Groups is only a mechanism to decide what to run and what not to. The data provider at the end of the day is only supposed to be worried about what @Test method is asking for data from it.

With that in mind, here’s a simple generic data provider, that basically providers either Strings (or) integers based on a meta annotation (custom one ofcourse)

 

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface WhatType {
    String value()
default "";
}

 

 

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class Sample {
   
@WhatType("int")
   
@Test(dataProvider = "dp")
   
public void testMethod(int i) {
        System.
err.println(i);
    }

   
@Test(dataProvider = "dp")
   
public void anotherTest(String i) {
        System.
err.println(i);
    }

   
@DataProvider(name = "dp")
   
public Object[][] getData(Method method) {
        Object[][] data;
       
WhatType type = method.getAnnotation(WhatType.class);
       
if (type == null || type.value().trim().equalsIgnoreCase("string")) {
            data =
new Object[][]{
                    {
"TestNG"},
                    {
"Selenium"}
            };
        }
else {
            data =
new Object[][]{
                    {
1},
                    {
2}
            };
        }
       
return data;
    }
}

 

You should be building something on these lines, instead of trying to figure out what is the current group that is being executed (which TestNG cannot provide you at all). The max you will get is, what are all the groups that were considered in order to determine which methods need to be executed for the current execution.

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

From: <[hidden email]> on behalf of André Santos <[hidden email]>
Reply-To: <[hidden email]>
Date: Monday, January 22, 2018 at 6:42 PM
To: testng-users <[hidden email]>
Subject: [testng-users] Way to check which group is running

 

Hello,

I've been trying to find a solution to understand which group I'm running at a certain point. I want to use this to dynamically change my data provider based on the group running. So far, I've found only solutions that will show me all groups running, like:

public Object[][] testDataProvider(ITestContext context) {
    List<String> includedGroups = Arrays.asList(context.getIncludedGroups());


But if I'm running two groups at the same time, I'll never be able to distinguish this in a fashion manner.

Is there any way to find this out?

Thanks,
André

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.