什么是使用数据库创建单元测试功能的标准/最佳实践? [英] What are standard/best practices for creating unit tests for functionality using databases?

查看:80
本文介绍了什么是使用数据库创建单元测试功能的标准/最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到单元测试背后的想法,但我试图想一个干净的简单的方法做它需要数据库功能。例如,我有一个基于数据库选择查询返回结果的函数。数据库必须保持不变,以便我正确地看到,只有正确的结果被返回。在需要数据库不活动(无论是读,写,更新还是删除)时,执行单元测试(在PHP中)的最佳方法是什么?

I get the idea behind unit testing however am trying to think of a clean simple way do it when it requires database functionality. For instance I have a function that return result based off a database select query. Would the database alway have to remain the same for me to properly see that only the correct results are being returned. What is the best way to perform unit testing (in PHP) when it requires database inactivity (whether it be read, write, update, or delete)?

推荐答案

PHPUnit手册中有一整章:

There is a whole chapter on that in the PHPUnit manual:

  • http://www.phpunit.de/manual/current/en/database.html and also worth reading
  • http://matthewturland.com/2010/01/04/database-testing-with-phpunit-and-mysql/

这就像单元测试的一切。创建一个已知的状态,并测试你的代码,看看它是否返回预期的结果。

It's like with everything else when Unit-Testing. Create a known state and test your code against it to see if it returns the expected results.

这篇关于什么是使用数据库创建单元测试功能的标准/最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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