コメントの罠
PHPUnit でテストを作成していたときのこと、なぜかテストケースとして認識されないテストケースがあった。そのコードを模したコードと、実行結果を以下に載せる。
<?php require_once 'PHPUnit/Autoload.php'; class FooTest extends PHPUnit_Framework_TestCase { /* * @test * @group foo */ public function foo001() { $this->assertTrue(true); } }
% phpunit unit.php PHPUnit 3.6.8 by Sebastian Bergmann. F Time: 0 seconds, Memory: 4.25Mb There was 1 failure: 1) Warning No tests found in class "FooTest". FAILURES! Tests: 1, Assertions: 0, Failures: 1.
綴りが間違っていないか、よけいな空白があったらだめなのでは、@test アノテーションが使えないバージョンなのか、とか悩むこと 1 時間。
解決の糸口は、テストケースと認識されている別のテストケースからドキュメントブロックで、今困っているテストケースのドキュメントブロッグを置き換えたら、テストケースとして認識されたこと。
二つのドキュメントブロックを比べてみると、コメントの開始部分が違っていた。認識される方は "/**" で始まっているのに対し、認識されない方は "/*" だった。"*" が少ない。
そういえばコメントに関してこんな記事があったなと思い出した。 http://d.hatena.ne.jp/narusase/20120816/p1
PHPUnit のコードを grep してみると、複数出てきた。なるほど、テストとして認識されない訳だ。
% grep -h getDocComment /usr/share/php/PHPUnit/*/*.php return strpos($method->getDocComment(), '@test') !== FALSE || strpos($method->getDocComment(), '@scenario') !== FALSE; $docComment = $reflector->getDocComment(); $docComment = $reflector->getDocComment(); $docComment = $reflector->getDocComment(); self::$annotationCache[$className] = self::parseAnnotations($class->getDocComment()); self::$annotationCache[$className . '::' . $methodName] = self::parseAnnotations($method->getDocComment());