icanhazstring / phpunit-seed
phpunit seeding 扩展,集成 fzaninotto/Faker
v0.4
2018-09-05 15:51 UTC
Requires
- php: ^7.1
- fzaninotto/faker: ^1.7
- phpunit/phpunit: ^5.0 || ^6.0 || ^7.0
Requires (Dev)
README
已废弃,建议使用 icanhazstring/phpunit-faker-extension
phpunit 扩展,集成 fzaninotto/Faker
安装
要安装此扩展,只需使用 composer
。
$ composer require --dev icanhazstring/phpunit-seed
使用方法
要使用此扩展,请确保您的测试从 PHPUnitSeed\Framework\TestCase
继承,而不是默认的 PHPUnit
。
这将向您的测试套件中添加一个新的函数 fake()
。
class SuperTest extends PHPUnitSeed\Framework\Testcase { public function testAwesome() { $name = $this->fake()->name(); $this->assertEquals($name, $this->fake()->name()); // should work } }
使用种子运行测试
要使用提供的二进制文件执行所有测试并使用 random
或 specific
种子,请使用。
$ vendor/bin/phpunit-seed [--seed=<int>]
输出结果类似于此
$ vendor/bin/phpunit-seed -c phpunit.xml
PHPUnit 6.4.3 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 128 ms, Memory: 6.00MB
OK (1 test, 2 assertions)
Randomized with seed: 1234
在最后一条输出行中,您将看到用于此执行的种子编号。如果您想执行相同的测试,并使用相同的 random
值,请使用二进制的 --seed=<int>
参数。
更改 Faker 区域设置
您可以通过两种方式更改 'Faker' 的区域设置
- 更改测试类中的
static FAKE_LOCALE
class MyTestCase extends PHPUnitSeed\Framework\Testcase { protected static FAKE_LOCALE = 'de_DE'; public function itShouldTestWithDELocale() { $name = $this->fake()->name; // will be name from de_DE provider } }
- 使用您希望的区域设置调用
fake
方法class MyTestCase extends PHPUnitSeed\Framework\Testcase { public function itShouldTestWithDELocale() { $name = $this->fake('de_DE')->name; // will be name from de_DE provider } }
Faker 文档
有关 Faker
的完整用法,请参阅 fzaninotto/Faker