我在本地Composer安装中运行PHPUnit 4.2.2。我也在运行PHP CodeSniffer 2.2.0。
我写过这个单元测试:
<?php
include_once 'animals/Cat.php';
class CatAgeTest extends PHPUnit_Framework_TestCase{
public function testTrueIsTrue(){
$kittyAgeTest = new Cat("steak");
$result = $kittyAgeTest->getAge() >=5 && $kittyAgeTest->getAge() <= 10;
$this->assertTrue($result);
}
}
?>
它通过,直到我更改方法名称。这是我唯一改变的东西,我尝试过很多不同的名字。以下是几个例子:
testCatAgeBetweenFiveAndTen
catAgeTest
catAgeShouldBeBetweenFiveAndTen
......等等。
这是我的phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
我在同一台计算机上有几个其他单元测试 - 在不同的项目中,具有相同的配置。我试过改变他们的名字,但这不会导致他们失败。
我已经使用这些命令来运行php unit:
vendor/bin/phpunit
vendor/bin/phpunit tests/CatAgeTest.php
这是我的composer.json文件(以防它有帮助):
{
"require": {
},
"require-dev": {
"phpunit/phpunit": "4.2.2",
"squizlabs/php_codesniffer": "2.2.0"
},
"autoload": {
"psr-0": {
"animals": ""
}
}
}
非常感谢任何帮助!
编辑:
这是我的Cat.php文件:
<?php
include_once("Animal.php");
class Cat extends Animal{
protected $name;
protected $species;
protected $speakCount;
protected $age;
public function __construct($name = "MewMew") {
$this->name = $name;
$this->age = rand(5, 10); // Random age
$this->species = "Feline"; // For testing in the DB
$this->namesHist = array(); // Names history
array_push($this->namesHist, $name);
}
public function getAge(){
return $this->age;
}
public function getSpecies(){ // For testing in the DB
return $this->species;
}
public function speak($word = "Meow.") { // Spoken word and word count
$this->speakCount += 1;
$this->speakCount % 5 == 0 ? $this->age += 1 : $this->age;
return $word;
}
public function getSpeakCount() {
return $this->speakCount;
}
public function setName ($newName) {
$this->name = $newName;
array_push($this->namesHist, $newName);
}
public function getNames() {
return $this->namesHist;
}
public function getAverageNameLength() {
$nameLen=0;
foreach($this->namesHist as $i) {
$nameLen += strlen($i);
}
$avgNL = round($nameLen/(count($this->namesHist)), 2);
return "Average Name Length: " . $avgNL;
}
}
?>
...这是我的Animal.php文件:
<?php
Class Animal {
protected $name;
protected $age;
protected $favoriteFood;
public function __construct($name, $age, $favoriteFood) {
$this->name = $name;
$this->age = $age;
$this->favoriteFood = $favoriteFood;
}
public function getName() {
return $this->name;
}
public function getAge() {
return $this->age;
}
public function getFavoriteFood() {
return $this->favoriteFood;
}
public function setName ($newName) {
$this->name = $newName;
}
public function setAge ($newAge) {
$this->age = $newAge;
}
public function setFavoriteFood($newFavoriteFood) {
$this->favoriteFood = $newFavoriteFood;
}
}
?>
EDIT2:
这是我得到的错误:
PHPUnit 4.2.2 by Sebastian Bergmann.
Configuration read from /var/www/php/misc/ap/phpunit.xml
F
Time: 15 ms, Memory: 2.75Mb
There was 1 failure:
1) Warning
No tests found in class "CatAgeTest".
FAILURES!
Tests: 1, Assertions: 0, Failures: 1.
答案 0 :(得分:2)
PHPUnit发出的警告表明它在测试类中没有找到测试方法。
1) Warning
No tests found in class "CatAgeTest".
PHPUnit将接受名为以test*()
开头且具有public
可见性的方法作为测试方法。也可以使用@test
docblock属性声明该命名约定之外的方法,以将其指定为测试方法。
这些方法命名和描述约定are documented here
测试是公共方法,名为test *。
或者,您可以在方法的docblock中使用@test注释将其标记为测试方法。
鉴于此要求,您尝试的方法名称testCatAgeBetweenFiveAndTen()
应该是PHPUnit的有效测试方法。
除非您使用catAgeTest(), catAgeShouldBeBetweenFiveAndTen()
属性对其进行注释,否
@test