bancer/paratest-databases-factory
Composer 安装命令:
composer require bancer/paratest-databases-factory
包简介
Creates test databases for running PHP unit tests in parallel
README 文档
README
This library provides a convinient way to automate the creation of test databases and is supposed to be used when running PHP unit tests in parallel. It is compatible with paratest library - https://github.com/paratestphp/paratest.
How to install
Execute composer require bancer/paratest-databases-factory or composer require bancer/paratest-databases-factory --update-no-dev to install by composer.
How to use
Create test databases in bootstrap.php:
use Bancer\ParatestDatabasesFactory\DatabasesFactory; ... (new DatabasesFactory()) ->setDsn(getenv('pf_dsn')) ->setUsername(getenv('pf_user')) ->setPassword(getenv('pf_pass')) ->createDatabase('pf_test');
This will create pf_test1, pf_test2 etc. test databases when unit tests are started. The database name and credentials are just an example here. The requirement is that the user has permissions to connect to the database and has been granted the privilege to create databases.
Load the bootstrap file in phpunit.xml file if not already done,
f.ex.: <phpunit ... bootstrap="./bootstrap.php"/>.
Create the database user and grant privileges to create databases:
CREATE USER 'pf_user'@'%' IDENTIFIED BY 'your_password'; GRANT CREATE ON *.* TO 'pf_user'@'%'; CREATE DATABASE pf_test; GRANT SELECT ON `pf_test`.* TO 'pf_user'@'%';
Add the database credentials to phpunit.xml:
<env name="pf_dsn" value="mysql:dbname=pf_test;host=localhost"/> <env name="pf_user" value="pf_user"/> <env name="pf_pass" value="your_password"/>
Adjust your database configuration by appending TEST_TOKEN environment variable value.
Something like this:
if (getenv('TEST_TOKEN') !== false) { // Using paratest $databaseName .= getenv('TEST_TOKEN'); }
There are two ways how to run phpunit tests in parallel:
-
By using plain phpunit.
- Pros:
- No dependencies to third party libraries.
- Can be implemented with any version of PHP and PHPUnit.
- Cons:
- Complex setup.
- Howto:
- Split your unit tests into two or more testsuites that take approximately the same time to run. The example below is for two test suites that are named ci-1 and ci-2.
- Ensure that shell's job control is enabled or execute
set -min the console. - Execute
TEST_TOKEN=1 Vendor/bin/phpunit --testsuite ci-1 &> /tmp/ci-1.out & TEST_TOKEN=2 Vendor/bin/phpunit --testsuite ci-2 & fgor similar command. This setsTEST_TOKENenvironment variable, starts the first test suite in the background process and writes its output to a file in tmp folder, starts the second test suite and brings it output to the foreground. This way two test suites are run in parallel. Run the slowest test suite the last. - Execute
cat /tmp/ci-1.outto print the results of the first test suite to the console. - Execute
grep "OK" /tmp/ci-1.outor similar command to check that the first test suite successfully finished.
- Pros:
-
By using paratest library.
- Pros:
- Simple setup.
- Cons:
- Dependency to third party library.
- Paratest is actively supported only for the latest PHP version therefore not all projects can use it.
- Howto:
- Install paratest and follow their instructions - https://github.com/paratestphp/paratest.
- Pros:
bancer/paratest-databases-factory 适用场景与选型建议
bancer/paratest-databases-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.78k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bancer/paratest-databases-factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bancer/paratest-databases-factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-04