PHPUnit:测试完成后,如何从数据库中删除测试数据? [英] PHPUnit: how can I remove test data from my database after the tests are complete?

查看:109
本文介绍了PHPUnit:测试完成后,如何从数据库中删除测试数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Doctrine 2的Zend Framework应用程序(版本1.11)。我已经设置了PHPUnit,可以在我的模型和表单上运行测试,而没有其他功能。测试效果很好,但是存在一个问题:完成测试后,它们会将测试数据保留在数据库中。这是我的一项测试的基本示例:

I have a Zend Framework application (version 1.11) that uses Doctrine 2. I've got PHPUnit set up to run tests on my models and forms and whatnot. The tests work great, but there's one problem: they leave the test data in the database once they are done. Here's a basic sample of one of my tests:

class VisaTypeEntityTest extends ModelTestCase
{
    public function testCanSaveAndRetrieveVisaType()
    {
        $addVisaType = new \Entities\VisaTypes();
        $addVisaType->setEnglishName('Test Visa Type')
            ->setJapaneseName('試し')
            ->setKanaName('タメシ')
            ->setDescription('Description of the test visa type');

        $this->em->persist($addVisaType);
        $this->em->flush();

        $getVisaType = $this->em->getRepository('\Entities\VisaTypes')
            ->findOneByEnglishName('Test Visa Type');

        $this->assertEquals('Test Visa Type', $getVisaType->getEnglishName());
    }
}

显然,必须将数据实际输入数据库为了确保一切都是洁净的。但是我不希望每次运行测试时所有的测试数据都使数据库混乱,也不想去手动删除它。

Obviously the data has to actually be entered into the database in order to make sure everything is kosher. But I don't want all the test data gumming up the database every time I run a test, nor do I want to go and manually remove it.

是否有东西我可以这样做,例如在测试完成后使用tearDown()方法摆脱测试数据吗?如果是这样,是否可以将表的ID字段中的自动增量回滚到之前的水平?我知道id之间是否有间隙真的没关系,但是如果有某种方法可以使Doctrine重置自动增量值就好了。

Is there something I can do, such as using the tearDown() method to get rid of the test data once the test is complete? And if so, is it possible to "roll back" the auto increments in the tables' id fields to what they were beforehand? I know it really shouldn't matter if there are gaps between ids, but if there is some way to get Doctrine to reset the auto increment value that would be great.

推荐答案

1。关于清理:

由于使用的是InnoDB,因此可以使用事务将数据库恢复到测试开始之前的相同状态:

Because you are using InnoDB you can use transactions to restore the DB to the same state as it was before the test started:

因此在 setUp()中,您将添加 $ this-> em-> ; getConnection()-> beginTransaction(); tearDown() $ this-> em-> ; getConnection()-> rollback(); 会将数据库还原到以前的状态。也可以在 InnoDB事务中查看MySQL手册。建模并锁定,以确保它不会干扰应用程序中的任何其他数据(关键字:隔离级别)。

So in setUp() you would add $this->em->getConnection()->beginTransaction(); and in tearDown() $this->em->getConnection()->rollback(); This restores the database to the previous state. Also have a look at MySQL handbook on "The InnoDB Transaction Model and Locking" to make sure that this does not interfere with any other data in your application (keyword: isolation level).

2。关于回滚自动增量ID:

据我所知,这是(轻松)可行的。 在SO上有一个关于它的线程,其中指出您的应用程序不应该在意自动增量ID之间是否有间隔。如果您使用的是测试数据库(建议高度使用),那么这个问题就更少了。

As far as I know this is not (easily) possible. There is a thread about it on SO in which it is stated that your application should not care if there are gaps between the auto increment ids. If you are using a test database (which is highly recommended) this should be even a lesser concern.

这篇关于PHPUnit:测试完成后,如何从数据库中删除测试数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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