Symfony2 中的数据库测试实践?如何隔离? [英] Practices for database testing in Symfony2? How to isolate?

查看:24
本文介绍了Symfony2 中的数据库测试实践?如何隔离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前测试数据库与 Symfony2 交互的最佳实践是什么?我有一个简单的 CRUD 设置,我想确保我的测试正常.现在,我有 4 个测试,每个测试都确保创建、更新、删除和列出操作正常进行.

What are the current best practices for testing database interaction with Symfony2? I have a simple CRUD setup and i want to make sure my testing is OK. Right now, i have 4 tests, each one making sure that create, update, delete and list actions are occurring ok.

我的测试用例中有两个魔术方法,__construct 和 __destruct.在它们内部,我使用 'php app/console ...' 调用 exec() 以创建数据库,创建架构,然后删除数据库.然而,这实在是太慢了,当我有多个测试用例时,它总是会发生.

I have two magic methods, __construct and __destruct, on my test case. Inside them, i call exec() with 'php app/console ...' in order to create the database, create the schema and later on drop the database. However, this is SLOW as hell and it happens all the time when i have more than one test case.

当涉及到数据库测试和隔离此类测试时,我应该如何进行?

How should i proceed when it comes to database testing and isolating such tests?

推荐答案

数据库测试总是很慢,因为您需要在每次测试之前/之后创建/删除架构.为避免不必要的操作,您可以:

Database testing is always slow as you need to create/drop your schema before/after each test. To avoid unnecessary operations, you could:

  • 在setUpBeforeClass"方法中创建架构;
  • 确保使用@depend"注释在一个线程中启动 4 个测试;
  • 在 'tearDownAfterClass' 方法中删除架构.

模式只会为您的测试用例创建/删除一次.

The schema will be created/droped only once for your tests case.

您还可以设置 Dot 使用内存中的 sqlite 数据库(速度非常快):

You can also setup doctrine to use an inmemory sqlite database (which is very fast):

doctrine:
    dbal:
        driver: pdo_sqlite
        path: :memory:
        memory: true

无论如何,'_construct' 和 '_destruct' 不应该在 phpunit 测试用例中使用,而应该使用 'setUp' 和 'tearDown'.

Anyway, '_construct' and '_destruct' should never be used in phpunit test cases, instead you should use 'setUp' and 'tearDown'.

这篇关于Symfony2 中的数据库测试实践?如何隔离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆