【发布时间】:2014-07-11 14:33:49
【问题描述】:
我正在使用 phpunit 为我的代码编写单元测试。代码类有以下内容:
<?
// Dependencies
//
require_once($_SERVER['DOCUMENT_ROOT']."/includes/configs.php");
class xyz{
..
...
..
}
?>
测试类如下:
<?php
require_once("xyz.php");
class testxyz extends PHPUnit_Framework_TestCase{
public function setUp(){
$this->xyz= new xyz();
}
public function testEmail(){
$this->xyz->SetEmail('abc');
$name = $this->xyz->GetEmail();
$this->assertEquals($name == 'abc');
}
}
当我在测试类上运行 php 单元时,出现以下错误:
Fatal Error: Class xyz not found
通过在我的代码类顶部给出<?php instead of <? 解决了这个错误。
其次,在解决第一个错误后,我收到以下错误:
Fatal error: require_once(): Failed opening required '/includes/configs.php'.
这意味着 $_SERVER['DOCUMENT_ROOT'] 未被 php 单元识别。
既然无法更改代码类,如何在不更改代码类的情况下成功运行 php 单元。请建议任何解决方法,以便 <? 和 $_SERVER['DOCUMENT_ROOT'] 开始使用 php 单元。
【问题讨论】:
标签: php unit-testing phpunit