简体   繁体   English

如何记录 PHPUnit 测试

[英]How to document PHPUnit tests

I'm writing a lot of unit tests and I'm afraid one day I'll come back to read the test codes and not be able to understand what's being tested.我正在编写很多单元测试,我担心有一天我会回来阅读测试代码并且无法理解正在测试的内容。

The question is: How do i document PHPUnit tests using PHPDoc ?问题是:如何使用PHPDoc记录PHPUnit测试?

Use the @covers annotation (it's specific to PHPUnit, not a documentation tag used by phpDocumentor) to highlight what the test case is supposed to exercise.使用@covers注释(它特定于 PHPUnit,而不是 phpDocumentor 使用的文档标签)来突出测试用例应该执行的内容。 By having it in the docblock, you tell the code reader what code is targeted by the test.通过将它放在 docblock 中,您可以告诉代码阅读器测试的目标代码是什么。 If you have phpDocumentor also generating docs for your test cases, then it should treat the annotation as a "custom tag", and show it as generic information.如果您有 phpDocumentor 也为您的测试用例生成文档,那么它应该将注释视为“自定义标签”,并将其显示为通用信息。 Note, though, that the point of @covers is to limit the code coverage measurements done by PHPUnit.但请注意, @covers是限制 PHPUnit 完成的代码覆盖率测量。 Its use as doc info is incidental, but useful.它用作文档信息是偶然的,但很有用。

If you want some kind of document linking between the test case and the tested code, also put the @uses tag in the test case's docblock.如果您希望在测试用例和测试代码之间建立某种文档链接,还可以将@uses标记放在测试用例的@uses中。 That should result in @used-by tag automatically appearing in the documentation for the tested method/function.这应该会导致@used-by标签自动出现在测试方法/函数的文档中。

One way as suggested is to use the test function name but this can end up too abbreviated and cryptic.建议的一种方法是使用测试函数名称,但这最终可能过于简短和晦涩。 In this case put some text in the optional $message parameter to explain what the test is doing.在这种情况下,在可选的 $message 参数中放入一些文本来解释测试正在做什么。

assertSame(mixed $expected, mixed $actual[, string $message = ''])

I finds this helps, particularly if you are used to writing JavaScript tests with something like Jasmine where you put a human readable sentence to explain what is being tested for each test.我发现这很有帮助,特别是如果您习惯于使用 Jasmine 之类的东西编写 JavaScript 测试,您可以在其中放置一个人类可读的句子来解释每个测试要测试的内容。

Here is a simple example.这是一个简单的例子。 If you put the test description as the default value for a function argument it will be documented.如果您将测试描述作为函数参数的默认值,它将被记录下来。 If you put just one test per function (ie single responsibility principle) then when you look back in a few years time maybe the tests will make more sense than having multiple tests per function.如果你对每个功能只进行一个测试(即单一职责原则),那么当你几年后回顾时,这些测试可能比每个功能有多个测试更有意义。

<?php
use PHPUnit\Framework\TestCase;

final class ArrayPushTest extends TestCase
{
    public function testPushStringToEmptyArray(string $description = 
        'A string is pushed into an empty array at array index 0.'
        ) : void
    {
        $a = [];
        array_push($a, 'zero');
        $this->assertSame('zero', $a[0], $description);
    }
}

And this is what it looks like in the docs with phpDocumentor:这就是 phpDocumentor 文档中的样子:

phpDocumentor output phpDocumentor 输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM