无法更改PHPUnit中的方法名称
我正在本地Composer安装中运行PHPUnit 4.2.2。我还运行PHP CodeSniffer 2.2.0 我编写了这个单元测试:无法更改PHPUnit中的方法名称,php,methods,phpunit,naming,Php,Methods,Phpunit,Naming,我正在本地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(){ $kittyAgeTe
<?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);
}
}
?>
。。。等等
以下是我的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>
这是我的composer.json文件(以防万一):
任何帮助都将不胜感激
编辑:
这是我的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;
}
}
?>
<?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;
}
}
?>
PHPUnit发出的警告表示它在测试类中未找到任何测试方法
1) Warning
No tests found in class "CatAgeTest".
PHPUnit将接受以test*()
开头并具有public
可见性的方法作为测试方法。还可以使用@test
docblock属性声明该命名约定之外的方法,以将其指定为测试方法
这些方法包括命名和描述约定
这些测试是名为test*的公共方法
或者,您可以在方法的docblock中使用@test注释将其标记为测试方法
鉴于此要求,您尝试的方法名称
testCatAgeBetweenFiveAndTen()
应该是PHPUnit的有效测试方法
您声明的其他命名尝试不符合PHPUnit的要求,并且将找不到(catAgeTest(),CatagesHouldbeenFiveAndTen()
),除非您还使用@test
属性对其进行注释:
/**
* @test
*/
public function catAgeTest(){
$kittyAgeTest = new Cat("steak");
$result = $kittyAgeTest->getAge() >=5 && $kittyAgeTest->getAge() <= 10;
$this->assertTrue($result);
}
/**
*@测试
*/
公共功能分类测试(){
$kittyAgeTest=新猫(“牛排”);
$result=$kittyAgeTest->getAge()>=5&&$kittyAgeTest->getAge()资产true($result);
}
提供更改名称后收到的错误消息。我已将错误添加到帖子中。谢谢…testCatAgeBetweenFiveAndTen()
应该是有效的测试名称。其他方法会像预期的那样失败,因为它们不是解决这个问题的名为test*
的公共方法!非常感谢你!!!!请正确回答,这样我就可以给你信用了!
<?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;
}
}
?>
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.
1) Warning
No tests found in class "CatAgeTest".
/**
* @test
*/
public function catAgeTest(){
$kittyAgeTest = new Cat("steak");
$result = $kittyAgeTest->getAge() >=5 && $kittyAgeTest->getAge() <= 10;
$this->assertTrue($result);
}