【问题标题】:Magento phpunit assertion - assertEquals(true, false)Magento phpunit 断言 - assertEquals(true, false)
【发布时间】:2013-07-26 06:21:01
【问题描述】:

我正在尝试使用 EcomDev_PHPUnit 包在 Magento 上进行单元测试,但在配置它时遇到了一些问题。我已经在这里发布了对我有用的问题和解决方案 -

MAGENTO.stackexchange.com-Pointers to write unit test cases using EcomDev_PHPUnit

现在,我有一个非常笼统的问题,

class Webservice_Clientservice_Test_Model_ClientserviceimplTest extends EcomDev_PHPUnit_Test_Case{

    public function testBasicFunctionality(){
        try{
            //Mage::log("testBasicFunctinality");
            $this->assertSame(true,false);
        }catch(Exception $e){
            Mage::logException($e);
        }
    }

}

当我使用

运行这个测试时
phpunit --group Webservice_Clientservice

我得到以下信息,

phpunit --group Webservice_Clientservice
PHPUnit 3.7.22 by Sebastian Bergmann.

Configuration read from /mnt/www/dev.magento.com/phpunit.xml.dist

..

Time: 3 seconds, Memory: 22.25Mb

OK (2 tests, 2 assertions)

我原以为断言会失败,而测试用例最终会失败……它怎么会通过?确实有问题... True 不能等于 false :( 而且,测试用例也运行了两次?我不知道为什么....

【问题讨论】:

  • 您找到解决方案了吗?
  • @group 注释在哪里?

标签: php magento phpunit assert assertions


【解决方案1】:

如果您使用 try catch 块包装测试,您的测试将不会失败。

// failing test
public function testFail() {
    $this->assertSame(true, false);
}

// successful test
public function testSuccess() {
    try {
        $this->assertSame(true, false);
    } catch (Exception $e) {
        echo "go on";
    }
}

如果您想强制测试失败,您可以使用fail 方法:

public function testForceFail() {
    $this->fail('Failed Yeah');
}

【讨论】:

    猜你喜欢
    • 2016-09-14
    • 1970-01-01
    • 2020-02-16
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多