问题描述
我是 PHPUnit(以及一般的单元测试)的新手.我想开发一个测试套件,开发人员可以在本地运行,但也可以在我们的集成系统(Codeship)中运行.我知道可以使用内存数据库,但这似乎依赖于我们没有使用的迁移(似乎不能很好地处理视图、存储过程、函数、触发器等?).
I'm a newcomer to PHPUnit (and unit testing in general). I want to work on a test suite that developers can run locally, but can also be run in our integration system (Codeship). I understand that it is possible to use an in-memory database, but it seems like that relies on the migrations, which we are not using (doesn't seem to handle views, stored procedures, functions, triggers, etc very well?).
1)在内存中创建一个数据库并使用默认数据为数据库播种(用于所有测试)的最佳方法是什么(放置在 Laravel 中)?
What's the best way (place in Laravel) to 1) create a database in memory and seed the database with default data (to be used for ALL test)?
推荐答案
你可以使用SQLite.
You can use SQLite.
来自文档:
SQLite 数据库通常存储在单个普通磁盘文件中.但是,在某些情况下,数据库可能存储在内存中.
An SQLite database is normally stored in a single ordinary disk file. However, in certain circumstances, the database might be stored in memory.
将此添加到 config/database.php
文件中:
Add this to the config/database.php
file:
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
在 phpunit.xml
文件的 <php>
节点下:
On the phpunit.xml
file, under <php>
node:
<env name="DB_CONNECTION" value="sqlite_testing" />
阅读更多这里.
其他解决方案
在您的 storage/
文件夹上创建一个测试数据库,名称为 database.sqlite
或者如果您想要其他名称或其他位置,您必须更改config/database.php 文件,这些是默认配置:
Create a testing database on your storage/
folder, with the name database.sqlite
or if you want another name or another location you have to change the configs on the config/database.php file, these are the default configs:
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
使用此命令运行您的迁移:
Use this command to run your migrations:
php artisan migrate --database=sqlite
或者将此行添加到 .env
文件中:
Or add this line to the .env
file:
DB_CONNECTION=sqlite
您的应用程序正在为 phpunit 使用 sqlite.
Your application is using sqlite for phpunit.
现在您可以运行迁移和测试了.之后,只需将 DB_CONNECTION
更改为您用于项目的数据库.
Now you can run your migrations and test. After that, just change the DB_CONNECTION
to the database you are using for your project.
这篇关于如何为 PHPUnit 测试创建内存数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!