php - 插入单元测试后清理数据的最佳做法是什么

标签 php mysql unit-testing

<分区>

正如负责人所说,在单元测试函数中将一些行(仅用于测试)插入数据库后,我目前在函数末尾放置一个查询以手动删除这些行,但我相信这不是最好的做法。 那是什么?

最佳答案

嗯,当处理数据夹具时,最好的选择可能是实现 setUp()tearDown() 方法。
如果您已经分配了文件、数据库或套接字等外部资源,那么 tearDown() 方法似乎是最好的方法。

来自 PHPUnit 文档:

setUp() and tearDown() are nicely symmetrical in theory but not in practice. In practice, you only need to implement tearDown() if you have allocated external resources like files or sockets in setUp(). If your setUp() just creates plain PHP objects, you can generally ignore tearDown(). However, if you create many objects in your setUp(), you might want to unset() the variables pointing to those objects in your tearDown() so they can be garbage collected. The garbage collection of test case objects is not predictable.

您可以在 Chapter 4. Fixtures 中获取更多数据夹具信息:

关于php - 插入单元测试后清理数据的最佳做法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36943334/

相关文章:

php - 用户可以在他们的个人资料描述中使用 html、css、javascript、php 等

java - sql "between statement"不适用于 spring

php - 阻止从地址栏访问仪表板部分

mysql - 每 5 分钟将 MSSQL 数据复制到 MySQL DB?

php - 订单执行后?

python-3.x - Nose 单元测试发现在 python 3.8 上被破坏

javascript - 如何将外部库包含到单元测试中::Angular2-CLI

在 git push 上运行单元测试,在 pull request 上运行集成测试

Mac OSX 上的 Java 进程在通过网络服务器调用时似乎为 "hang",但在命令行中正常

javascript - 检测用户点击 HTML 表格 TD,并获取值