运行单元测试时,Play Framework会自动删除数据库中的所有记录

时间:2011-07-05 19:39:24

标签: unit-testing playframework

每当我在我的网站上运行自动测试时,所有表都会被清理为0行。这是设计的吗?我该如何预防?

更新:发现真正的“罪魁祸首” %test.jpa.ddl =创建

2 个答案:

答案 0 :(得分:6)

这是设计上的。单元测试不应对某些预先存在的状态(如持久数据)有任何外部依赖性。如果您需要用于测试目的的数据,则需要在@Before设置方法中进行设置。例如:

@Before
public void setUp() {
    // The following loads test data from the YAML file
    Fixtures.loadModels("test-data/users.yml");
}

@Test
public void someTest() {
    assertEquals(5, User.count()); // 5 User records exist due to @Before method
}

您应该查看您的conf / application.conf文件并注意您有一行内容:

%test.db=mem

这是默认设置 - 表示当应用程序在测试模式下运行时,请使用内存数据库。如果希望测试对持久数据(不推荐)起作用,则可以更改测试模式数据库设置。有关详细信息,请参阅Play test documentation

答案 1 :(得分:0)

在更新application.conf文件之前我遇到了同样的问题:

%test.jpa.ddl=none

https://playframework.com/documentation/1.2.4/configuration#jpa

相关问题