pretzlaw/phpunit-docgen

通过使用文档注释生成测试证据或文档。

3.0.0 2019-06-10 11:43 UTC

This package is not auto-updated.

Last update: 2024-09-24 13:17:56 UTC


README

使用方法

需要的包

composer require pretzlaw/phpunit-docgen

在您的 phpunit.xml 中写入以下内容

<listeners>
    <listener class="Pretzlaw\PHPUnit\DocGen\TestCaseListener">
        <arguments>
            <string>test-evidence.md</string>
        </arguments>
    </listener>
</listeners>

它将输出所有文档注释,以漂亮的Markdown格式。在运行集成测试时,这将为单元测试提供良好的测试证据或某种文档。

示例 / 最佳用法

假设您需要描述一个API或向客户交付由单元测试/集成测试保证的测试证据...

  • tests/
    • FooController/
      • CreateTest.php
      • ShowTest.php
      • ...
    • FooControllerTest.php

只需几个 文档注释 就可以生成这些文档。以下是由这些测试的文档注释生成的(Tests\FooControllerTest 执行了此操作)

# Foo Fighters

This is an API for managing Foo. It allows:

- Create
- Read

Tests\FooController\CreateTest 添加了以下内容

## How to create a new one?

Creating is easy as long as you stick to the range.

### Request

Do `POST /foo` with some data.

Tests\FooController\ShowTest 添加了以下内容

## Read out all foo

### Restrictions

You can't see more than five. This is because ...

At least one will be at the drums. Because ...

标题和文本

FooControllerTest.php 中,我们有

namespace Tests;

/**
 * Foo Fighters
 *
 * This is an API for managing Foo. It allows:
 *
 * - Create
 * - Read
 *
 * @since 1994
 */
class FooControllerTest {}

这就是第一个部分的内容(参见上面的示例文档)。它简单地输出没有 @ 属性的文档注释。

深度 / 结构

子部分将通过解析命名空间以及在之后解析测试包含的方法来生成

namespace Tests\FooController;

/**
 * How to create a new one?
 * 
 * Creating is easy as long as you stick to the range.
 *
 */
class CreateTest {
  /**
   * Request
   *
   * Do `POST /foo` with some data.
   *
   */
  function testBar() {
    $this->assertTrue( (new FooController)->create() );
  }
}

这就是第二个部分的创建方式(参见上面)。

将内容附加到另一个标题

当您在一个测试中时,意味着您正在测试一个上下文,有时您希望向某些标题添加文档。只需再次使用所有这些测试的标题即可。

/**
 * Read out all foo
 *
 */
class ShowTest {

  /**
   * Restrictions
   *
   * You can't see more than five. This is because ...
   */
  function testBar() {}
  
  
  /**
   * Restrictions
   *
   * At least one will be at the drums. Because ...
   */
  function testBaz() {}
}

忽略一个方法或类 / 不在文档中打印

  • @internal 标记它。
  • 没有标题或内容的注释也将被忽略。

谢谢!