无法更改 PHPUnit 中的方法名称
Cannot Change Method Name in PHPUnit
我在本地 Composer 安装中 运行ning 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 单元:
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;
}
}
?>
编辑2:
这是我得到的错误:
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.
PHPUnit 发出的警告表明它在您的测试中找不到测试方法class。
1) Warning
No tests found in class "CatAgeTest".
PHPUnit 将接受以 test*()
开头并具有 public
可见性的方法作为测试方法。也可以使用 @test
文档块属性在该命名约定之外声明一个方法,以将其指定为测试方法。
这些方法的命名和描述约定are documented here
The tests are public methods that are named test*.
Alternatively, you can use the @test annotation in a method's docblock to mark it as a test method.
鉴于此要求,您尝试的方法名称 testCatAgeBetweenFiveAndTen()
应该是 PHPUnit 的有效测试方法。
您声明的其他命名尝试不符合 PHPUnit 的要求,并且不会被发现 (catAgeTest(), catAgeShouldBeBetweenFiveAndTen()
),除非您还使用 @test
属性对其进行注释:
/**
* @test
*/
public function catAgeTest(){
$kittyAgeTest = new Cat("steak");
$result = $kittyAgeTest->getAge() >=5 && $kittyAgeTest->getAge() <= 10;
$this->assertTrue($result);
}
我在本地 Composer 安装中 运行ning 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 单元:
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;
}
}
?>
编辑2:
这是我得到的错误:
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.
PHPUnit 发出的警告表明它在您的测试中找不到测试方法class。
1) Warning
No tests found in class "CatAgeTest".
PHPUnit 将接受以 test*()
开头并具有 public
可见性的方法作为测试方法。也可以使用 @test
文档块属性在该命名约定之外声明一个方法,以将其指定为测试方法。
这些方法的命名和描述约定are documented here
The tests are public methods that are named test*.
Alternatively, you can use the @test annotation in a method's docblock to mark it as a test method.
鉴于此要求,您尝试的方法名称 testCatAgeBetweenFiveAndTen()
应该是 PHPUnit 的有效测试方法。
您声明的其他命名尝试不符合 PHPUnit 的要求,并且不会被发现 (catAgeTest(), catAgeShouldBeBetweenFiveAndTen()
),除非您还使用 @test
属性对其进行注释:
/**
* @test
*/
public function catAgeTest(){
$kittyAgeTest = new Cat("steak");
$result = $kittyAgeTest->getAge() >=5 && $kittyAgeTest->getAge() <= 10;
$this->assertTrue($result);
}