在类中使用@covers

ner*_*ell 5 php phpunit unit-testing

我正在使用 php 来测试我的代码。这是一个例子:

/**
 * @covers Calculator::
 */
class CalculatorTest extends PHPUnit_Framework_TestCase {
    protected function setUp() { /* ... */ }
    /**
     * @covers Calculator::add
     */
    public function testAddTwoIntegers() { /* ... */ }
    /**
     * @covers Calculator::multiply
     */
    public function testMultiplyTwoIntegers() { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)

但是,我的代码很复杂,我想摆脱单个测试方法上的@covers。php 在生成覆盖率报告时如何处理以下类:

/**
 * @covers Calculator
 */
class CalculatorTest extends PHPUnit_Framework_TestCase {
    protected function setUp() { /* ... */ }

    public function testAddTwoIntegers() { /* ... */ }

    public function testMultiplyTwoIntegers() { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)

请注意,类上的 @covers 注释仍然存在,但我已将其从每个方法中删除。

我也可以使用这种方法获得覆盖率报告,但我没有在任何地方看到它的示例,所以我想知道这是否不是正确的用法。

Code Coverage Report:
  2016-01-18 08:57:50

 Summary:
  Classes: 17.67% (56/317)
  Methods:  0.33% (5/1520)
  Lines:    0.60% (109/18094)

Class1:
  Methods:  66.67% ( 2/ 3)   Lines:  95.45% ( 21/ 22)
Class2:
  Methods:  50.00% ( 3/ 6)   Lines:  96.70% ( 88/ 91)
Run Code Online (Sandbox Code Playgroud)

Ste*_*ott 0

为什么不简单地运行测试用例并查看更改的覆盖范围?当你在没有尝试任何事情的情况下提出问题时,你可能会面临被否决的风险。

来自PHPUnit 手册

The @covers annotation (see Table B.1) can be used in the test code to specify
which method(s) a test method wants to test. If provided, only the code 
coverage information for the specified method(s) will be considered. Example 
11.2 shows an example.
Run Code Online (Sandbox Code Playgroud)

上面引用的表 B1