charm / testing
1.1.13
2022-06-08 22:54 UTC
Requires
- charm/terminal: >=1.0 <2.0
- charm/util-composerpath: ^1.0
- charm/util-phpencode: ^1.0
- neilime/php-css-lint: ^3.0
- seld/jsonlint: ^1.9
This package is auto-updated.
Last update: 2022-11-27 19:28:47 UTC
README
一个命令行工具,用于快速编写回归和单元测试,无需任何模板代码。
编写测试
在项目根目录下创建一个名为 charm-tests/
的文件夹。测试包含简单的PHP文件,返回类似这样的数组
<?php return [
/**
* First array item is a string that describes the test.
*/
"Test description",
/**
* Second item is a function which performs the testing. The function
* must return data which will be compared to the expected result.
*/
function($data) {
// perform your test and return a value
return strtoupper($args);
},
/**
* The next items are arrays of testing criteria and their expected result
*/
[ "hello world", "HELLO WORLD" ], // succeeds
[ "blåbærsyltetøy", "BLåBæRSYLTETøY" ], // succeeds
[ "blåbærsyltetøy", "BLÅBÆRSYLTEDØY" ], // fails, but is the correct result
];
The test report
---------------
The test report contains the date and the result of all the tests with a nice
side-by-side diff highlighting the difference between the expected result and
the actual result.
Running the tests
-----------------
Run the function `vendor/bin/charm-testing` and you will see the tests being performed.
The output is a markdown file that you can save for future reference.
Simple Automation Example
-------------------------
The `charm-testing` command can easily be automated in various contexts, because it
provides an exit code which indicates the percentage of failed tests rounded up.
* If no tests failed, the exit code is `0`.
* If all tests failed, the exit code is `100`.
* If 1 test failed, the exit code is `1` (or `100` if you only have
one test. ;-)
Invoking the tests from PHP:
exec('exec /usr/bin/env vendor/bin/charm-testing', $output, $exitCode); if ($exitCode > 0) {
// some tests failed!
}
Handling failed tests from the command line:
./vendor/bin/charm-testing || echo "Some tests failed!"
or
if [[ ! $(./vendor/bin/charm-testing) ]]; then
echo "Some tests failed"
fi