【问题标题】:phpunit2 "Not tests executed"phpunit2 "未执行测试"
【发布时间】:2014-06-16 11:31:37
【问题描述】:

嘿,我遇到了一个小问题。

如果我使用 phpunit,我可以运行我的测试,但如果我尝试使用 phpunit2,它就不起作用。

它似乎找不到测试。

我只收到消息:“未执行测试”。

我正在使用 xamppear 1.9.4,php 5.5.9 zend 2.5.0。

文件位于 phpunit.bat 所在的 xampp/php 文件夹中。

BankAccount.php

<?php
class BankAccount {
  private $fBalance = 0;

  public function getBalance() {
    return $this->fBalance;
  }

  public function setBalance($balance) {
    if ($balance >= 0) {
      $this->fBalance = $balance;
    }
  }

  public function depositMoney($amount) {
    if (is_numeric($amount) &&
      $amount >= 0) {
      $this->fBalance += $amount;
    }
  }

  public function withdrawMoney($amount) {
    if (is_numeric($amount) &&
      $amount >= 0 &&
      $this->fBalance >= $amount) {
      $this->fBalance -= $amount;
    }
  }
}
?>

BankAccountTest.php

<?php
require_once 'BankAccount.php'; // zu testende Klasse
require_once 'PHPUnit2/Framework/TestCase.php'; // Einbinden der Pear-Klasse

class BankAccountTest extends PHPUnit2_Framework_TestCase {
  public function testBalanceIsInitiallyZero() {
    $ba = new BankAccount;
    $this->assertEquals(0, $ba->getBalance());
  }

  public function testBalanceCannotBecomeNegative() {
    $ba = new BankAccount;
    $ba->withdrawMoney(1);
    $this->assertEquals(0, $ba->getBalance());

    $ba = new BankAccount;
    $ba->setBalance(-1);
    $this->assertEquals(0, $ba->getBalance());
  }
}
?>

非常感谢任何帮助!..

【问题讨论】:

  • 我不明白你为什么要扩展PHPUnit2_Framework_TestCase。您必须扩展 PHPUnit_Framework_TestCasephpunit.de/manual/current/en/writing-tests-for-phpunit.html
  • 我不使用 php4 的 unit 和 php5 的 unit2 还是我完全错了?我看到 phpunit2 我有多个示例,或者它们是旧版本,并且足以简单地使用 phpunit?
  • 试试PHPUnit_Framework_TestCase
  • 我之前已经测试过 PHPUnit_Framework_TestCase 并且有效。但是如果我不使用 phpunit2 是什么?

标签: php unit-testing xampp phpunit


【解决方案1】:

这是来自PHP Cookbook by Adam Trachtenberg and David Sklar, Page 619:的引用

PHPUnit2 仅适用于 PHP5,而 PHPUnit 将在 PHP5 或 PHP4.


总结:您可以使用PHPUnit2_Framework_TestCase 来测试您的代码。但是为了简单起见,并且由于您现在遇到的问题,请使用PHPUnit_Framework_TestCase

【讨论】:

    猜你喜欢
    • 2013-04-04
    • 2018-05-05
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 2015-06-15
    • 2022-01-25
    相关资源
    最近更新 更多