Symfony2 中的数据库测试实践?如何隔离?

编程入门 行业动态 更新时间:2024-10-18 14:22:03
本文介绍了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 中的数据库测试实践?如何隔离?

本文发布于:2023-11-14 09:42:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!