albreis / phptest
Requires
- php: >=7.2
README
PHPTest
PHPUnit 的替代品
如何工作
此包用于从代码注释生成测试。PHPUnit 的简化替代品。
想法是减少创建测试这项繁琐工作的负担。
如何使用
您可以通过克隆存储库或使用 composer 安装。
composer require albreis/phptest
要使用此测试套件,只需使用以下模式
@test [执行代码的 PHP 代码,包含一个 "return"]
或
@test [执行代码的 PHP 代码,包含一个 "return"] @expect return [先前测试的预期返回值]
对于异常测试,不需要返回,因为返回是自动使用异常消息完成的。
异常测试示例
<?php namespace Albreis\Kurin; use Albreis\Kurin\Interfaces\IEvent; /** @package Albreis\Kurin */ /** * @test new Albreis\Kurin\Event * @expect return 'Cannot instantiate abstract class Albreis\Kurin\Event'; */ abstract class Event implements IEvent { private ?array $callbacks = []; private $message; ...
执行上述测试将返回,因为抽象类不能实例化,因此我们期望这个返回消息。
File: /home/webprodutora/everhost.net.br/kurin/src/Event.php Line: 8 Test: new Albreis\Kurin\Event Return: string(53) "Cannot instantiate abstract class Albreis\Kurin\Event" Expect: Cannot instantiate abstract class Albreis\Kurin\Event Status: Success Congratulations! All tests are passed.
返回值需要是 true 或 false
以下是一些使用此工具的示例
示例 1
<?php namespace Albreis; class Calculadora { /** * @test return (new Albreis\Calculadora)->soma(1, 2) == 3 * @test return (new Albreis\Calculadora)->soma(1, 2) == 4 */ public function soma($num1, $num2) { return ($num1 + $num2); } }
将上面的文件保存到一个文件夹中,然后运行以下命令,指定包含测试文件的文件夹
php vendor/bin/phptest {diretorio}
或
php vendor/albreis/test/phptest {diretorio}
示例 2
<?php namespace Albreis; class Anime { /** * @test return isset(json_decode((new Albreis\Anime)->getRandom())->anime) * @test return !isset(json_decode((new Albreis\Anime)->getRandom())->anime) */ public function getRandom() { return file_get_contents('https://animechan.vercel.app/api/random'); } }
php vendor/albreis/test/phptest {diretorio}
在外部文件中编写测试
当测试非常复杂且需要多行时,最好将它们写在外部文件中。
只需使用以下模式:@test_using [外部文件的相对路径]
测试文件:SomaTest.php
<?php return (new Albreis\Soma)->soma(2, 3) == 5;
待测试的类
<?php namespace Albreis; class Soma { /** * @test return (new Albreis\Soma)->soma(9,9) == 89 * @test_using SomaTest.php */ public function soma($num1, $num2) { return ($num1 + $num2); } }
测试代码块
/** * @test return ($num1 + $num2) == 4 */ $num1 = 1; $num2 = 3;
将返回
File: /home/public_html/bin/examples/tests.php Line: 4 Test: return ($num1 + $num2) == 4 Return: bool(true) Status: Success Congratulations! All tests are passed.
测试函数
/** * @test return soma(1, 1) == 2 */ function soma($n, $n2) { return ($n + $n2); }
将返回
File: /home/public_html/bin/examples/tests.php Line: 10 Test: return soma(1, 1) ==2 Return: bool(true) Status: Success Congratulations! All tests are passed.
从测试中排除文件或目录
如果您不想测试某些特定的文件或目录,可以使用 --exclude 或 --exclude_regex 选项,以下是一个示例
忽略 vendor 目录
php vendor/bin/phptest --exclude-regex=vendor/* .
忽略 index.php 文件
php vendor/bin/phptest --exclude-regex=vendor/* .
忽略 index.php 文件和 vendor 目录
php vendor/bin/phptest --exclude-regex=vendor/* --exclude=index.php .
日志
测试执行后,将在项目根目录下保存一个名为 phptest.logs 的日志文件,其中包含所有测试的完整反馈。
辅助工具
PHPTest 拥有一些辅助函数,可以帮助执行某些测试。
get_http_status_code(string $url)
返回 HTTP 状态代码
贡献
克隆 Develop 分支,创建一个具有您功能的分支,进行更改,然后创建一个 PR(pull request),我将评估您所做的工作,如果批准,我将将其合并到 Main 分支。
捐赠
目前我完全独立维护此项目,因此捐赠是受欢迎的! :) 您可以通过 PIX 12454995727 或通过 paypal(contato@everaldoreis.com.br)进行捐赠
支持
WhatsApp: https://wa.me/554898523084