无法更改 PHPUnit 中的方法名称


Cannot Change Method Name in PHPUnit

我在本地 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单元:

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;
        }
    }
    ?>

。这是我的动物.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 发出的警告表示它在测试类中找不到任何测试方法。

1) Warning
No tests found in class "CatAgeTest".

PHPUnit 将接受以 test*() 开头且具有public可见性的方法作为测试方法。 也可以使用 @test docblock 属性声明该命名约定之外的方法,以将其指定为测试方法。

此处记录了这些方法命名和说明约定

测试是名为 test* 的公共方法。

或者,您可以使用方法的文档块中的@test注释将其标记为测试方法。

鉴于此要求,您尝试的方法名称testCatAgeBetweenFiveAndTen()应该是 PHPUnit 的有效测试方法。

您的其他声明命名尝试不符合 PHPUnit 的要求,除非您也使用 @test 属性注释它们,否则不会找到 (catAgeTest(), catAgeShouldBeBetweenFiveAndTen()):

/**
 * @test
 */
public function catAgeTest(){
    $kittyAgeTest = new Cat("steak");
    $result = $kittyAgeTest->getAge() >=5 && $kittyAgeTest->getAge() <= 10;
    $this->assertTrue($result);
}