如何 PHPUnit 测试没有返回值的方法


How to PHPUnit test a method with no return value?

我正在尝试测试我编写的以下类中的方法(函数比显示的要多,基本上,每个is_*()方法都有一个函数):

class Validate {
  private static $initialized = false;
  /**
  * Construct won't be called inside this class and is uncallable from the outside. This prevents
  * instantiating this class. This is by purpose, because we want a static class.
  */
  private function __construct() {}
  /**
  * If needed, allows the class to initialize itself
  */
  private static function initialize()
  {
    if(self::$initialized) {
      return;
    } else {
      self::$initialized = true;
      //Set any other class static variables here
    }
  }
  ...
  public static function isString($string) {
    self::initialize();
    if(!is_string($string)) throw new InvalidArgumentException('Expected a string but found ' . gettype($string));
  }
  ...
}

当我测试方法是否在无效输入上抛出异常时,效果很好!但是,当我测试该方法是否按预期工作时,PHPUnit 抱怨因为我在测试中没有断言。具体错误为:

# RISKY This test did not perform any assertions

但是,我没有任何价值可以断言,所以我不确定如何克服这一点。

我已经阅读了一些关于测试静态方法的内容,但这似乎主要涵盖了静态方法之间的依赖关系。此外,即使是非静态方法也可能没有返回值,那么,如何解决这个问题呢?

作为参考,我的测试代码:

class ValidateTest extends PHPUnit_Framework_TestCase {
  /**
  * @covers ../data/objects/Validate::isString
  * @expectedException InvalidArgumentException
  */
  public function testIsStringThrowsExceptionArgumentInvalid() {
    Validate::isString(NULL);
  }
  /**
  * @covers ../data/objects/Validate::isString
  */
  public function testIsStringNoExceptionArgumentValid() {
    Validate::isString("I am a string.");
  }
}

使用 assertNull 测试 void 函数:

    /**
     * @covers ../data/objects/Validate::isString
     */
    public function testIsStringNoExceptionArgumentValid() {
         $this->assertNull( Validate::isString("I am a string.") );
    }

要防止有关断言的警告,您可以使用 @doesNotPerformAssertions 注释,如文档中所述:https://phpunit.de/manual/current/en/appendixes.annotations.html#idp1585440

或者,如果您更喜欢代码而不是注释: $this->doesNotPerformAssertions();

我遇到的一个解决方案如下,基于 PHPUnit 第 2 章的示例 2.12。这对我来说感觉有点笨拙,但这是我迄今为止发现的最好的。此外,基于这个PHPUnit Gitub问题讨论,似乎还有其他几个人想要这个功能,但没有计划实现它。

testIsStringNoExceptionArgumentValid()更改为以下内容:

  /**
  * @covers ../data/objects/Validate::isString
  */
  public function testIsStringNoExceptionArgumentValid() {
    try {
      Validate::isString("I am a string.");
    } catch (InvalidArgumentException $notExpected) {
      $this->fail();
    }
    $this->assertTrue(TRUE);
  }

如果你想测试一个 void 函数,你只需要运行它,没有任何断言。如果有任何问题,它将抛出异常并且测试将失败。无需放置 $this->assertTrue(TRUE);,因为您没有运行断言,并且不需要断言来测试代码。

你会收到一条消息,比如


Time: 7.39 seconds, Memory: 16.00 MB
OK (1 test, 0 assertions)
Process finished with exit code 0