【问题标题】:how to use DbUnit with TestNG如何在 TestNG 中使用 DbUnit
【发布时间】:2011-07-11 07:23:06
【问题描述】:

我需要将 DbUnit 与 TestNG 集成。

1) 是否可以将 DbUnit 与 TestNG 一起使用,因为 DbUnit 基本上是 JUnit 的扩展。
2) 如果是怎么办?

【问题讨论】:

    标签: junit testng dbunit


    【解决方案1】:

    我终于找到了一种将 DbUnit 与 TestNG 一起使用的方法!

    使用 IDatabaseTester 的实例工作,

    但另一种解决方法是: 扩展 AbstractDatabaseTester 并实现 getConnection 并覆盖必要的功能。 但一件重要的事情是在测试前后调用 onSetup() 和 onTeardown()。

    希望这会有所帮助...

    【讨论】:

      【解决方案2】:

      不确定您到底要做什么,但也许Unitils 会有所帮助。它类似于 dbunit 扩展但不限于此,并且支持与 TestNg 的集成(通过为您的测试用例扩展 UnitilsTestNG 类)。

      【讨论】:

      • 也许您可以提供更具体的场景来说明您想要做什么。对我来说,dbunit 用于准备测试数据库状态,而 Unitils 用于管理和简化特定于 dbunit 的配置(例如每个测试的数据集),无论使用 JUnit 还是 TestNg 都应该有效。如果您询问的是直接将 DbUnit 与 TestNG 一起使用而不是其他问题,您可能需要注意以下问题,例如:sourceforge.net/support/tracker.php?aid=1897627 并准备好 DbUnit 将在内部使用 JUnit事情
      • 我需要在 TestNG 中使用 DbUnit,因为 DbUnit 扩展了 JUnit,我不知道该怎么做。目前我正在使用 IDatabaseTester 实例而不是扩展类 DbTestCase,这是在 TestNG 中使用 DbUnit 的正确方法吗?
      • 我不能对此发表太多评论,因为我自己不使用 TestNg(并通过 Unitils 管理大部分 dbunit 配置),但环顾四周后,我意识到 dbunit.org/howto.html#noextend 已经解决了这个问题:你只需要找出要扩展的TestNg测试类是什么,TestNg的before和after方法,以及IDatabaseTester的必要接线槽(所示示例使用JdbcDatabaseTester)
      【解决方案3】:

      这是执行所需功能的简单类。

      public class SampleDBUnitTest {
      
          IDatabaseTester databaseTester;
          IDataSet dataSet;
      
          @BeforeMethod
          public void setUp() throws Exception {
              // These could come as parematers from TestNG 
              final String driverClass = "org.postgresql.Driver";
              final String databaseUrl = "jdbc:postgresql://localhost:5432/database";
              final String username = "username";
              final String password = "password";
      
              dataSet = new FlatXmlDataSet(Thread.currentThread().getContextClassLoader().getResourceAsStream("dataset.xml"));
              databaseTester = new JdbcDatabaseTester(driverClass, databaseUrl, username, password);
              databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
              databaseTester.setDataSet(dataSet);
              databaseTester.setTearDownOperation(DatabaseOperation.NONE);
              databaseTester.setDataSet(dataSet);
      
              databaseTester.onSetup();
          }
      
          @AfterMethod
          public void tearDown() throws Exception {
              databaseTester.onTearDown();
          }
      
          @Test
          public void t() throws Exception {
              // Testing, testing
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-10
        • 1970-01-01
        • 2015-05-05
        • 2017-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多