JUnit 测试套件:在测试开始运行之前首先创建数据集的方法

发布于 2024-10-31 12:12:29 字数 457 浏览 0 评论 0原文

我想在任何测试开始运行之前为整个测试套件设置数据。我知道 Maven 是一个接一个地运行测试,而不是一个套件,所以我不能使用 @SuiteClasses。另外,我不想通过 dbunit-maven-plugin 创建数据集,必须通过 REST 创建数据集。有没有一种方法可以让我在 Maven 集成前测试和集成后测试中运行特定的类来进行设置和清理?

例如,

public class TestInit
{
    public void setUp()
    {
       //Data setup
    }

    public void tearDown()
    {
       //Data clean up
    }
}

在测试套件启动之前运行安装程序,并在测试套件结束后运行拆卸。或者我可以运行 2 个单独的类,例如 TestInitSetup 和 TestInitTearDown 吗?

I want to setup data for my entire test suite before any of the tests start running. I understand maven runs the test one by one and not a suite, so I cannot use @SuiteClasses. Also I dont want to create the dataset through dbunit-maven-plugin, the dataset has to be created over REST. Is there a way where I can run specific classes as part of maven pre-integration-test and post-integration-test to setup and clean?

For example

public class TestInit
{
    public void setUp()
    {
       //Data setup
    }

    public void tearDown()
    {
       //Data clean up
    }
}

make setup run before test suite starts and tearDown after it ends. Or can I run 2 separate classes like, TestInitSetup and TestInitTearDown?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2024-11-07 12:12:29

这里是一个基于规则的解决方案。它可能有用。

语法如下:

public class SimpleWayToUseDataSetTest {
    @Rule
    public DataSetRule rule = new DataSetRule(); // <-- this is used to access to the testVectors from inside the tests

    public static class MyDataSet extends SimpleTestVectors {
        @Override
        protected Object[][] generateTestVectors() {
            return new Object[][] {
                    {true,  "alpha", new CustomProductionClass()}, // <-- this is a testVector
                    {true,  "bravo", new CustomProductionClass()},
                    {false, "alpha", new CustomProductionClass()},
                    {false, "bravo", new CustomProductionClass() }
            };
        }
    }

    @Test
    @DataSet(testData = MyDataSet.class) // <-- annotate the test with the dataset
    public void testFirst() throws InvalidDataSetException { // <-- any access to testData may result in Exception
        boolean myTextFixture = rule.getBoolean(0); // <-- this is how you access an element of the testVector. Indexing starts with 0
        String myAssertMessage = rule.getString(1); // <-- there are a couple of typed parameter getters
        CustomProductionClass myCustomObject = (CustomProductionClass) rule.getParameter(2); // <-- for other classes you need to cast
        Assert.assertTrue(myAssertMessage, true);
    }
}

Here is a Rule based solution. It may be useful.

The syntax looks like this:

public class SimpleWayToUseDataSetTest {
    @Rule
    public DataSetRule rule = new DataSetRule(); // <-- this is used to access to the testVectors from inside the tests

    public static class MyDataSet extends SimpleTestVectors {
        @Override
        protected Object[][] generateTestVectors() {
            return new Object[][] {
                    {true,  "alpha", new CustomProductionClass()}, // <-- this is a testVector
                    {true,  "bravo", new CustomProductionClass()},
                    {false, "alpha", new CustomProductionClass()},
                    {false, "bravo", new CustomProductionClass() }
            };
        }
    }

    @Test
    @DataSet(testData = MyDataSet.class) // <-- annotate the test with the dataset
    public void testFirst() throws InvalidDataSetException { // <-- any access to testData may result in Exception
        boolean myTextFixture = rule.getBoolean(0); // <-- this is how you access an element of the testVector. Indexing starts with 0
        String myAssertMessage = rule.getString(1); // <-- there are a couple of typed parameter getters
        CustomProductionClass myCustomObject = (CustomProductionClass) rule.getParameter(2); // <-- for other classes you need to cast
        Assert.assertTrue(myAssertMessage, true);
    }
}
俯瞰星空 2024-11-07 12:12:29

如果您在 JUnit 中找不到解决方案,TestNG 支持 @BeforeSuite 和 @AfterSuite,它们似乎可以满足您的要求。

If you can't find a solution in JUnit, TestNG supports @BeforeSuite and @AfterSuite, which seem to do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文