Background I: *.phpt in phpunit
I recently read an article about *.phpt
support in PhpUnit:
https://www.moxio.com/blog/32/start-testing-with-phpt-tests-in-phpunit
The big advantages here:
- Supported out of the box with phpunit, no need for custom code to discover and deal with those files.
- Common format that everybody seems to agree upon.
- One can directly specify a specific *.phpt as a cli parameter.
A typical *.phpt file would look like this:
--TEST--
Basic arithmetic - addition
--FILE--
<?php
var_dump(42 + 1);
?>
--EXPECT--
int(43)
Background II: Self-updating fixtures
Earlier than that, I read about unit tests that can update (= overwrite) their own fixture files if a cli variable is present:
https://tomasvotruba.com/blog/2020/07/20/how-to-update-hundreds-of-test-fixtures-with-single-phpunit-run/
The following cli command would then overwrite the "expected" part in fixtures for failing tests:
$ UPDATE_TESTS=1 ./vendor/bin/phpunit
One would then review the git diff, and either accept the changes as desired functionality change, or fix the behavior.
The format of fixture files proposed in this article is very similar to the *.phpt
file format, but in the end the format can be completely arbitrary and project-specific.
Benefits:
- Start with empty "--EXPECT--" section, and have this part generated automatically.
- Automatically update tests after behavior change, e.g. after a bug was fixed, or when BC-breaking changes were introduced.
- Frequently update fixtures during early development, when behavior is not really stable yet.
- No more copy/paste from cli diff output.
Disadvantages:
- No built-in support in phpunit. Requires custom code. This does not scale well, if I want to do it in multiple projects.
- One cannot target the *.phpt file as a cli parameter.
Question
Is there a built-in way to auto-update those *.phpt test files with phpunit?
Or should I perhaps use something other than phpunit?