我有一个 jUnit 4/5 项目,我通过测试套件运行我的测试类。
我在测试套件中使用com.github.peterwippermann.junit4.parameterizedsuite.ParameterizedSuite.class 运行器作为注释“RunWith()” ,但我的测试类使用与JUnitPlatform.class 运行器相同的注释。
出于这个原因,无法在我的测试类中读取 - 在测试套件中 - 声明的参数(@Parameters)。如果我在我的测试类中使用 @RunWith(Parameterized.class) 运行器,它工作正常,但我的目标是在我的测试类中使用 junit 5 JUnitPlatform.class 运行器和 jUnit 4 ParameterizedSuite.class 运行器来参数化我的测试套房。
例如我的测试套件:
package TestSuite.Dummy;
//...some imports...
@RunWith(ParameterizedSuite.class)
@SuiteClasses({
T5.class
})
public class TestSuite_Dummy_01
{
public static Object[] parameters = new Object[] {"A", "B", "C"};
@Parameters(name = "Parameter of suite are {0}")
public static Object[] params() throws NoSuchMethodException, SecurityException
{
System.out.println(parameters.length);
System.out.println(parameters.getClass().getTypeName());
return parameters;
}
@Parameter(0)
public static String myStringParameter;
public static XYZQA QAE = null;
public static XYZQA getXYZQA()
{
// Create a new XYZQA object
try
{
QAE = new XYZQA();
System.out.println("QAE created");
return QAE;
}
catch(MalformedURLException e)
{
e.printStackTrace();
return null;
}
}
public static String getParameter()
{
System.out.println("Parameter available?" + ParameterContext.isParameterSet());
return myStringParameter;
}
}
我的示例测试类:
package Main.Demo;
//...some imports...
@RunWith(JUnitPlatform.class) //is necessary to run jUnit 5 tests with non-junit5-implemented IDEs
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(ClientDescriptionParameterResolver.class)
public class T5
{
public static XYZQA QAE;
public static String parameters;
public T5()
{
//...
}
@BeforeAll
static void setUpBeforeClass() throws Exception
{
QAE = TestSuite_Dummy_01.getXYZQA();
parameters = TestSuite_Dummy_01.getParameter();
System.out.println("Parameters from Suite: " + parameters);
}
@Test
public void test01(ClientDescription client)
{
//...
}
//...
}
问题:
以下代码行:
QAE = TestSuite_Dummy_01.getXYZQA(); //将测试套件类的QAE对象导入/使用到当前测试类中 Blockquote
对我来说很好,但它被固定定义为一个测试套件类“TestSuite_Dummy_01”。但是我所有的测试套件都应该与我的测试类一起使用,而不仅仅是这个测试套件。我的想法是使用测试类的正在运行的测试套件类(运行器)的当前实例,而不是固定的 TestSuite_Dummy_01.class。
如何让测试类的当前实例运行并在测试类中获取对相应测试套件类的引用?