【问题标题】:Cannot Change Method Name in PHPUnit无法更改 PHPUnit 中的方法名称
【发布时间】:2023-03-17 09:10:02
【问题描述】:

我在本地 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;

        }
    }

    ?>

...这是我的 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.

【问题讨论】:

  • 提供更改名称后收到的错误消息。
  • 我在帖子中添加了错误。谢谢...
  • testCatAgeBetweenFiveAndTen() 应该是有效的测试名称。其他人将按预期失败,因为它们不是名为 test* phpunit.de/manual/current/en/writing-tests-for-phpunit.html 的公共方法
  • 解决了!太感谢了!!!!请正确回答,以便我给你功劳!

标签: php methods phpunit naming


【解决方案1】:

PHPUnit 发出的警告表明它在您的测试类中找不到测试方法。

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

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

这些方法命名和描述约定are documented here

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

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

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

除非您还使用 @test 属性对其进行注释,否则您的其他命名尝试不符合 PHPUnit 的要求并且不会被发现 (catAgeTest(), catAgeShouldBeBetweenFiveAndTen()):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-17
    • 2020-10-22
    • 1970-01-01
    • 2022-08-23
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多