socialengine/setup-test-db

在Laravel测试中高效地初始化数据库

2.0 2015-09-22 07:33 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:39:49 UTC


README

Latest Stable Version License

Laravel中的集成测试很棒,但常见的数据库维护方式是重新播种和重新迁移,这对每个测试都是一个巨大的时间消耗。

此命令和引导文件旨在消除不必要的重新播种和迁移(因为您已经在使用事务,对吧?),并为您的测试提供一个“干净”的迁移和播种数据库。

支持与 sqlite 和Eloquent支持的其他任何数据库。

安装

在Composer中需要此包

$ composer require socialengine/setup-test-db

更新Composer后,将ServiceProvider添加到app/config/app.php中的providers数组中。

'SocialEngine\TestDbSetup\ServiceProvider',

添加bootstrap/testing.php或从vendor/socialengine/setup-test-db/bootstrap/testing.php复制。

<?php

// bootstrap/testing.php
$testEnv = (getenv('APP_ENV')) ? : 'testing';

passthru("php " . __DIR__ . "/../artisan db:seed-test --env={$testEnv}");

require __DIR__ . '/autoload.php';

将您的phpunit(或其他任何框架)引导文件从bootstrap/autoload.php更改为bootstrap/testing.php

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="./bootstrap/testing.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
>

从您的TestCase.php中删除所有迁移内容。

最终,您的测试运行时间将缩短到原来的1/3。

您还可以发布配置文件以更改使用的播种类并启用截断。

$ php artisan config:publish socialengine/setup-test-db

进一步阅读和灵感

这部分内容大多基于Chris Duell在他的将PHP单元测试速度提高15倍博客文章中的工作。

我目前正在开发的应用程序代码覆盖率不到50%,测试完成需要超过35秒。

这对于我来说已经足够了,我不愿意持续运行测试,这几乎抵消了自测试代码的目的。直到我厌倦了花时间加快测试速度,将速度提高了15倍(并且几乎将内存使用量减半)。

许可证

MIT