用数据库进行Phpunit测试 [英] Phpunit testing with database

查看:72
本文介绍了用数据库进行Phpunit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将重点放在使用PHPunit的单元测试上.

I am trying to focus a bit on unit testing using PHPunit.

我在这里找到了很好的教程 http://blog .nickbelhomme.com/php/phpunit-training-course-for-free_282

I have found a very good tutorial over here http://blog.nickbelhomme.com/php/phpunit-training-course-for-free_282

但是我有一些想念的东西,但还不知道该怎么做.

But there is something I miss and don't yet understand how to do.

我有一个用户模块,该模块维护有关用户的所有信息.并且有一个功能保存,可以将用户保存在数据库中.所以我有一个testFunction

I have a user module wich maintains all information about users. And There is a function save which saves the user in the database. So I have a testFunction

public function testCanCreateUser()
{
    $userData = array(
        'userName'  =>  'User1',
        'firstName' =>  'Joey',
        'lastName'  =>  'Hendricks',
        'email'     =>  'Joey@hendricks.com',
        'password'  =>  'f$tfe8F'

    ); 
    $user = new Model_User($userData);
    $user->save();

}

我第一次运行测试会成功.由于数据库为空.但是,当我第二次运行测试时,它将无法正常工作,因为我的系统不允许同一用户两次进入数据库.因此,为了做到这一点,我必须在运行测试之前每次都重新创建测试数据库.做这个的最好方式是什么? 还是要以其他方式解决这个问题?

The first time when I will run my test this wil work. Since the database is empty. But When I run my tests for the second time it won't work since my system doesn't allow the same user twice in the db. So In order to do this I have to recreate my testdatabase every time before I run my tests. What is the best way to do this? Or is this problem to be solved on a different way?

Tnx.

推荐答案

如果要测试业务逻辑:模拟数据库类并返回假数据

If you want to test your business logic: Mock away the Database class and return fake data

如果您想测试触发SQL语句的类(恕我直言,您也可以进行测试,因为我有点想知道我的代码是否可以在后端的真实db上正常工作)有点复杂,但是有一些方法可以做到:

If you want to test the class that fires the SQL statements (and imho you could test that too since I kinda wanna know if my code works fine with a real db in the backend) it gets a little complicated but there are ways to do it:

  • 在运行测试之前,使用setUp()和tearDown()获得数据的一致状态是(imho)编写数据库驱动的单元测试的好方法.手工编写很多自定义sql会很烦人.

  • Using setUp() and tearDown() to get a consistent state for your data before running your tests is (imho) a fine way to write db-driven unittests. It can get annoying to write lots of custom sql by hand though.

为使您的生活更轻松,您可以查看 DbUnit扩展,然后查看是否适用于您的应用程序.

To make your life a little easier you can look into the DbUnit extension and see if that works for your Application.

如果您真的想深入了解单元测试数据库交互,那么最好的阅读方法是(imho)

If you really want to dive into Unittesting database interactions the best read on the subject is (imho) the chapter on db-unittesting in Sebastian Bergmanns phpqa book.

您的应用程序可以允许使用自定义数据库名称和所有表的自动设置吗,也可以使用大量testdata一次设置数据库并将其用于所有测试中.您可能要格外小心,尽管一个测试不依赖于另一个测试编写的数据.

Could your application allow for a custom database name and automated setup of all tables it may also be possible to set the db up once with a lot of testdata and use that data in all your tests. You could be carefull so though that one test doesn't rely on data written by another one.

这篇关于用数据库进行Phpunit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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