简体   繁体   English

使用@BeforeClass方法中设置的不同参数执行TestNG测试

[英]Executing TestNG tests with different parameters set in @BeforeClass method

I want to test some code with different persistence units. 我想用不同的持久性单元测试一些代码。 Therefor I have written two identical TestNG test classes that only differ in the name of the persistence unit that I pass to Persistence#createEntityManagerFactory to get the correct factory. 因此,我编写了两个相同的TestNG测试类,只是它们的区别在于传递给Persistence#createEntityManagerFactory的持久性单元的名称,以获取正确的工厂。

This call is in a setup method annotated with `@BeforeClass`` 该调用在带有@ BeforeClass``的设置方法中

@BeforeClass
public void setupClass() {
    emf = Persistence.createEntityManagerFactory("test-eclipselink-h2");
    em = emf.createEntityManager();
    // init with some dummy data

    // ... some more initialization
}

What are the options to execute this test class with different persistence-units? 使用不同的持久性单元执行此测试类有哪些选择? It would be sufficient to have the names hardcoded in test classes, there is no need to specify them externally. 将名称硬编码在测试类中就足够了,而无需在外部指定它们。

public class A {
    private String s;

    @DataProvider
    public Object[][] dp() {
        return new Object[][] {
          new Object[] { "test-eclipselink-h1" },
          new Object[] { "test-eclipselink-h2" }
        };
      }

    @Factory(dataProvider = "dp")
    public A(String s) {
        System.out.println("Creating test class with " + s);
        this.s = s;
    }

    @Test
    public void test() {
        System.out.println("Test " + s);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM