testng: Can't use both groups and dataprovider at the same time

When I use dataprovider, it works fine, When I add a groups member, it all the sudden can’t find the dataprovider. Any idea what’s going wrong?

Header:

 @Test(dataProvider = "provide", groups = { "smoke" })
    public void testAllSidebarLinksExist(String item, Data.ItemTypes types)

Working XML:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SmokeTests" verbose="1" >
    <test name="CheckLinks">
        <classes>
            <class name="com.me.Content.ContentTests" />
        </classes>
    </test>
</suite>

Non-working XML:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SmokeTests" verbose="1" >
    <groups>
        <run>
            <include name="smoke"/>
        </run>
    </groups>
    <test name="CheckLinks">
        <classes>
            <class name="com.me.Content.ContentTests" />
        </classes>
    </test>
</suite>

Dump:

java.lang.NullPointerException

org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:143) 
at org.testng.internal.Parameters.handleParameters(Parameters.java:426) 
at org.testng.internal.Invoker.handleParameters(Invoker.java:1383) 
at org.testng.internal.Invoker.createParameters(Invoker.java:1075) 
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180) 
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
at org.testng.TestRunner.privateRun(TestRunner.java:767) 
at org.testng.TestRunner.run(TestRunner.java:617) 

About this issue

  • Original URL
  • State: closed
  • Created 14 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Cannot reproduce this on TestNG 7.3.0

Here’s a sample

import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestClassSample {

  public static final List<String> logs = new ArrayList<>();

  @BeforeGroups(groups = "smoke")
  public void beforeGroups() {
    logs.add("beforeGroups");
  }

  @AfterGroups(groups = "smoke")
  public void afterGroups() {
    logs.add("afterGroups");
  }

  @Test(dataProvider = "dp", groups = "smoke")
  public void testAddition(int a, int b) {
    logs.add("testAddition: " + a + "+" + b);
  }

  @DataProvider(name = "dp")
  public Object[][] getData() {
    return new Object[][]{
        {1, 2},
        {4, 5}
    };
  }
}

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.testng.Assert;
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlGroups;
import org.testng.xml.XmlRun;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class IssueTest {

  @Test
  public void runTest() {
    XmlSuite xmlSuite = new XmlSuite();
    xmlSuite.setName("suite_378");
    XmlTest xmlTest = new XmlTest(xmlSuite);
    xmlTest.setName("test_378");
    xmlTest.setXmlClasses(Collections.singletonList(new XmlClass(TestClassSample.class)));
    XmlRun xmlRun = new XmlRun();
    xmlRun.onInclude("smoke");
    XmlGroups xmlGroups = new XmlGroups();
    xmlGroups.setRun(xmlRun);
    xmlTest.setGroups(xmlGroups);
    System.err.println(xmlSuite.toXml());
    TestNG testng = new TestNG();
    testng.setXmlSuites(Collections.singletonList(xmlSuite));
    testng.run();
    System.err.println(TestClassSample.logs);
    List<String> expected = Arrays
        .asList("beforeGroups", "testAddition: 1+2", "testAddition: 4+5", "afterGroups");
    Assert.assertEquals(TestClassSample.logs, expected);
  }

}

Here’s the output

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="suite_378">
  <test thread-count="5" name="test_378">
    <groups>
      <run>
        <include name="smoke"/>
      </run>
    </groups>
    <classes>
      <class name="com.rationaleemotions.issue378.TestClassSample"/>
    </classes>
  </test> <!-- test_378 -->
</suite> <!-- suite_378 -->


===============================================
suite_378
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

[beforeGroups, testAddition: 1+2, testAddition: 4+5, afterGroups]


===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

Make sure your configuration methods belong to that group (or specify alwaysRun=true).

Cédric