howtomakeaturn/easy-coverage
Composer 安装命令:
composer require howtomakeaturn/easy-coverage
包简介
A simple package to encourage/force developers to write tests.
README 文档
README
A simple package to encourage/force developers to write tests.
Why I built this package?
I'm working with multiple junior developers in multiple projects.
I always want to find a way to encourage/force them to write tests.
And I want this process to be integrated into the CI/CD and pre-push workflow.
Installation
composer require "howtomakeaturn/easy-coverage:^0.1"
Usage
Let's say you think App\Services namespace is very important, and you want to force all developers to write tests for every public method under this namespace.
Here is the basic example:
<?php namespace Tests\Coverage; use PHPUnit\Framework\TestCase; use Howtomakeaturn\EasyCoverage\EasyCoverage; class CoverageTest extends TestCase { public function testCoverage() { $coverage = new EasyCoverage(); $coverage->includeNamespaces([ 'App\Services', ]); $coverage->scan(); if ($coverage->result()) { $this->assertTrue(true); } else { $num = count($coverage->missingMethods()); $str = implode(', ', $coverage->missingMethods()); $msg = "You need to write tests for these $num methods: $str"; $this->fail($msg); } } }
Anytime you create a new test for the related methods, use PHP 8 Attribute Syntax to label them:
<?php namespace Tests\Unit; use PHPUnit\Framework\TestCase; use Howtomakeaturn\EasyCoverage\Target; class SimpleTaskOneTest extends TestCase { #[Target('App\Services\SimpleTaskOne@doSomething')] public function testBasicTest() { $task = new \App\Services\SimpleTaskOne(); $task->doSomething(); $this->assertTrue(true); } }
And that's it!
Anyone who creates new public methods in any classes under App\Services have to write tests!
If you want to ignore some methods, you can use
$coverage->alwaysIgnoreMethods([ '__construct', ]);
If you want to ignore some classes, you can use
$coverage->ignoreClasses([ 'App\Services\LargeLegacyTask', ]);
If you want to ignore certain methods in classes, you can use
$coverage->ignoreClassMethods([ 'App\Services\SimpleTaskTwo@doSomething', ]);
Recommended workflow
-
You specify multiple namespaces that you think writing tests are necessary.
-
Run EasyCoverage in PHPUnit & CI/CD & pre-push workflow.
-
Anyone who writes new public methods under these namespaces will be forced to write tests!
How does this work behind the scenes?
-
EasyCoverage finds out all the
public&non-staticmethods under the namesapces you specify. -
EasyCoverage finds out all the class methods you labeled with
Howtomakeaturn\EasyCoverage\Targetattribute under theTestsnamespace. -
EasyCoverage compares two array values from above and then return the result.
License
The EasyCoverage is open-sourced software licensed under the MIT license.
howtomakeaturn/easy-coverage 适用场景与选型建议
howtomakeaturn/easy-coverage 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 07 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 howtomakeaturn/easy-coverage 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 howtomakeaturn/easy-coverage 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-17