hyperia/codecept-unittest-generator
Composer 安装命令:
composer require hyperia/codecept-unittest-generator
包简介
Unit test generator from annotations.
README 文档
README
This package allows you to generate PHPUnit tests from annotations, which you can write in your methods documentation.
- Basic usage of this package is to generate your tests skeleton and basic tests.
- You must check and complete all tests you generate, including the most basic methods.
- Files to parse must declare one class, abstract class, trait or interface to be accepted.
Installation
The preferred way to install this extension is through composer.
Either run
composer require hyperia/codecept-unittest-generator:"^1.0"
or add
"hyperia/codecept-unittest-generator": "^1.0"
to the require section of your composer.json.
Configuration
enable UnitGenerator extension in base /codeception.yml config file:
extensions: enabled: - Codeception\Extension\RunFailed commands: - Codeception\Command\UnitGenerator unitgenerator: config: # erase old target files with new one overwrite: true # if you want to generate tests for Interface too interface: false # automaticaly generate tests for getter / setter method auto: true # ignore errors that can be ignored ignore: true # regex (/.*config.php/ for example) that files must not match to have a tests generation exclude: '/.*Trait.*/' # regex (/.*.php/ for example) that files should match to have a tests generation include: '/.*.php/' dirs: # source directory => target directory - common/models: tests/unit/unitgen/common/models/ - console/models: tests/unit/unitgen/console/models/
Usage
./vendor/bin/codecept generate:unit
Annotations
/** * @PHPUnitGen\<phpunit_assertion_method>(<expectation>:{<parameters...>}) */
This annotation use some parameters:
-
phpunit_assertion_method: It is the PHPUnit assertion method you want ot use (like assertEquals, assertInstanceOf, assertTrue ...).
-
expectation: The method return expected value. Some PHPUnit methods does not need it (like assertTrue), so in those cases, it can be null.
-
parameters: The method parameters.
See this example, we want to auto generate some simple test for this method:
<?php // The class to test content /** @PHPUnitGen\AssertEquals('expected string':{1, 2, 'a string'}) */ /** @PHPUnitGen\AssertTrue(:{4, 5, 'another'}) */ /** @PHPUnitGen\AssertEquals(null) */ /** @PHPUnitGen\AssertNull() */ public function method(int $arg1 = 0, int $arg2 = 0, string $arg3 = 'default') {}
Those annotations will create basic PHPUnit tests:
<?php // The generated test for "method" in tests class $this->assertEquals('expectation string', $this->method(1, 2, 'a string')); $this->assertTrue($this->method(4, 5, 'another')); $this->AssertEquals(null, $this->method()); $this->assertNull($this->method());
Getter and setter annotation
<?php // The class to test content /** @PHPUnitGen\Get() */ /** @PHPUnitGen\Set() */
Those two annotations will allow you to auto-generate tests for simple getter / setter. Your getter / setter needs to be named like the following:
get<MyProperty>() {}
set<MyProperty>() {}
PHPUnit Generator has an "auto" option: If you activate it, it will search for the property when it find a method beginning with "get" or "set", and if the property exists, it will generate tests.
Private or protected method
If the method to test is private or protected, PHPUnit Generator will access the method with PHP ReflectionClass.
Requirements
UnitGenerator needs the following components to run:
- Codeception UnitGenerator is a module for Codeception. It will need a running version of this tool.
- Phpunit-generator package.
hyperia/codecept-unittest-generator 适用场景与选型建议
hyperia/codecept-unittest-generator 是一款 基于 HTML 开发的 Composer 扩展包,目前已累计 2.24k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2017 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hyperia/codecept-unittest-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hyperia/codecept-unittest-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-12-08
