testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.98k stars 1.02k forks source link

Cannot use a @Factory(dataProvider) to generate tests in classes with @Test annotation #105

Closed briangoetz closed 13 years ago

briangoetz commented 13 years ago

I'm trying to set up a test case which belongs to a group, but whose class body consists of a factory instead of test methods.

The "obvious' thing to do is:

@Test(groups={ "groupName' }) public class Foo { @Factory(dataProvider="d') public Object[] makeInstances(ArgType arg) { ... }

@DataProvider(name = 'd')
Object[][] fact() { ... }

}

But, TestNG complains in a confusing way:

test: [testng] [TestNG] [ERROR] [testng] Method makeInstances requires 1 parameters but 0 were supplied in the @Test annotation. [testng] The tests failed.

Here, it seems to be getting confused that makeInstances() should be a test method, even though it is marked as a factory. I can make the tests run by removing the @Test on the class, but then I can't put the test in a group.

I'm trying to do this all without using an XML file.

cbeust commented 13 years ago

Initial testing shows things working for me, can you email me a full example? I'm probably missing something you're doing.

By the way, note that you can use the shorter "factory + data provider + constructor" approach:

public class A { private Integer m_n;

@Factory(dataProvider = "d") public A(Integer n) { m_n = n; }

public A() {}

@DataProvider public Object[][] d() { return new Object[][] { { new Integer(42) }, { new Integer(43) } }; }

@Test public void a() { System.out.println("a() " + m_n); } }

briangoetz commented 13 years ago

By the way, note that you can use the shorter "factor + data provider + constructor" approach:

That occurred to me only later. Originally I thought I couldn't do it at all, because I wanted to create a variable multiple @Tests per row emitted by the data provider, but the shorter formulation is attractive enough that I can refactor into that.

Examples attached. package java.util;

import org.testng.annotations.Test;

import java.util.functions.Block;

/**