taeluf / tester
v0.3.x-dev
2024-09-09 15:18 UTC
Requires
- taeluf/cli: v0.1.x-dev
- taeluf/util: v0.1.x-dev
Requires (Dev)
- taeluf/code-scrawl: v0.6.x-dev
This package is auto-updated.
Last update: 2024-09-09 20:19:02 UTC
README
Tester - Php 的单元测试
单元测试库。 phpunit 拥有更多功能和社区。
安装
composer require taeluf/tester v0.3.x-dev
或者在你的 composer.json
{"require":{ "taeluf/tester": "v0.3.x-dev"}}
运行测试 | 命令行界面
从你的项目根目录调用 phptest 或 vendor/bin/phptest。
全局安装的 phptest 会执行存在的 vendor/bin/phptest 复制。
phptest # simply run tests
phptest init # Create test dir & example test files
phptest server # launch test server using default settings
phptest server server_name # launch a configured server
phptest server -host.override "https://example.com" # typically server tests will use a localhost address. This overrides the host in all tests, even if you have a multi-server setup.
phptest -test TestName # Run one test; display all output for the named test
phptest -test TestName -test Another Test # Run all named tests
phptest -class ClassName # Run all tests for a class. Do not include namespace. Can include multiple classes.
phptest -custom_key SomeValue # For any tests that allow custom input from CLI.
其他选项
-class ClassName -test TestName # Only run TestName for the given ClassName
-prettyPrintArray true # Not sure if this works. To print array comparisons on multiple lines. By default output is condensed.
-set_error_handler false # Not sure if this works. Enable the built-in error handler that causes all warnings, etc, to throw an exception.
-bench.threshold 0.001 # Only print benchmark for tests taking longer than 1 ms to run. `0.001` is configurable.
示例测试类
查看下面的可用断言
<?php
namespace Tlf\Tester\Test\Runner;
class NestedTest extends \Tlf\Tester {
/** called before tests are run */
public function prepare(){}
/** Test methods must prefix with `test` */
public function testAnything(){
$this->compare(true,true);
}
}
示例配置
test/config.json 是可选的,但推荐。不在你的 config.json 中的任何值都将由下面的默认值填充。
默认 config.json
{
"dir.test":["test/run"],
"dir.exclude":[],
"file.require":[],
"//file.log_to":"test/tests.log",
"results.writeHtml":false,
"bench.threshold": 0.0001,
"server.main": "main",
"server":{
"main": {
"dir":"test/Server",
"bootstrap":"bootstrap.php",
"deliver":"deliver.php",
"host": "https://"
}
}
}
断言
对于文档/详情,请参阅 src/Tester/Assertions.php
isInstanceOf($object, $shouldBe)
is_object($object)
is_false($value)
is_true($value)
str_contains($str, $target, ...$strings)
str_not_contains(string $str, $target, ...$strings)
compare($target, $actual,$strict=false)
compare_raw($target, $actual, $strict=false)
compare_arrays($target, $actual, $strict=false)
compare_objects($target, $actual, $strict=false)
compare_json($target, $actual, $strict=false)
str_contains_lines($str, $target)
compare_lines($target, $actual)
compare_dump($target, $actual)
也存在一个神奇的 __call(),它将调用现有的 PHP 函数并使用其返回值作为断言。
例如,$this->in_array('value', $array_to_test); 如果 PHP 的 in_array() 方法返回 true,则 pass。
替代安装
这也需要 code-scrawl 进行开发,该脚本不处理此。
将以下内容复制粘贴到 bash 终端
pwd="$(pwd)";
command="phptest"
downloadDir=~/.gitclone
mkdir -p "$downloadDir"
cd "$downloadDir"
git clone https://gitlab.com/taeluf/php/php-tests.git ${command}
echo "alias ${command}=\"${downloadDir}/${command}/code/phptest\"" >> ~/.bashrc
chmod ug+x "${downloadDir}/${command}/code/phptest"
cd "$pwd";
source ~/.bashrc
echo "You can now run \`${command}\`"