【发布时间】:2013-02-07 14:24:29
【问题描述】:
我已经编写了一些测试用例,并想用 PHPUnit 进行尝试。但是,它不起作用。
如果我运行phpunit CategoryTest,它会输出:
PHPUnit 3.7.14 by Sebastian Bergmann.
如果我执行phpunit --log-json error.log CategoryTest,则会显示error.log 文件:
{"event":"suiteStart","suite":"CategoryTest","tests":5}
{"event":"testStart","suite":"CategoryTest","test":"CategoryTest::test__construct"}
因此,它发现文件中有 5 个测试,开始执行第一个测试,并且无缘无故停止。是否有任何日志可以找到它无法继续执行的原因?
另外,如果我在其他文件上运行测试,比如phpunit --log-json error.log UserTest,shell 不会显示任何输出,error.log 文件也不会显示。
我尝试重新安装它,正如其他类似问题之一中所建议的那样,但它没有做任何事情。
有什么办法可以解决吗?
require_once '../Category.class.php';
require_once '../../db_connect.php';
require_once 'PHPUnit/Framework/TestCase.php';
class CategoryTest extends PHPUnit_Framework_TestCase {
private $Category;
protected function setUp() {
parent::setUp ();
$this->Category = new Category(0, $mdb2);
}
protected function tearDown() {
$this->Category = null;
parent::tearDown ();
}
public function __construct() {
}
public function test__construct() {
$this->markTestIncomplete ( "__construct test not implemented" );
$cat = $this->Category->__construct(0, $mdb2);
$this->assertInstanceOf('Category', $cat);
}
public function testReturnID() {
$this->markTestIncomplete ( "returnID test not implemented" );
$id = $this->Category->returnID();
$this->assertEquals(0, $id);
}
...
}
变量$mdb2 来自db_connect.php 文件。
我想通了。问题是我包含了一个类外部的变量。
【问题讨论】:
-
你能给我们看一些代码吗?就像您的 php 测试类或测试的类一样,我们可以提供帮助并查看类型错误
-
检查 /var/log 文件中的 error.log。
tail -f /var/log/error.log在系统或网站上的另一个规则中。 -
刚刚添加了一些代码。我跑了
tail -f /var/log/error.log,但没有这样的文件 -
您不应该在您的 TestCase 中覆盖
__construct()方法。构造函数设置了模拟生成器,所以如果你覆盖构造函数,你会得到很多奇怪的行为和不需要的副作用。setUp()方法是您应该用来初始化测试的特殊方法。 -
感谢您的建议,它的行为很奇怪。
标签: php unit-testing phpunit