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

查看:25
本文介绍了使用数据库进行 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'm missing and don't yet understand how to do.

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

I have a user module which 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 will 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?

推荐答案

如果你想测试你的业务逻辑: Mock掉数据库类并返回假数据

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

如果你想测试触发 SQL 语句的类(恕我直言,你也可以测试它,因为我有点想知道我的代码是否可以在后端的真实数据库中正常工作)它会得到有点复杂,但有一些方法可以做到:

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() 为您的数据获得一致的状态是(恕我直言)编写数据库驱动单元测试的好方法.不过,手动编写大量自定义 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.

如果您真的想深入了解 Unittesting 数据库交互,关于该主题的最佳读物是(恕我直言)Sebastian Bergmanns phpqa 书.

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.

您的应用程序是否可以允许自定义数据库名称和所有表的自动设置,也可以使用大量测试数据设置一次数据库并在所有测试中使用该数据.尽管一个测试不依赖于另一个测试编写的数据,但您可以小心.

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天全站免登陆