PHPUnit什么都不做,没有输出

2022-01-25 00:00:00 unit-testing php phpunit

我已经编写了一些测试用例,并想用 PHPUnit 进行尝试.但是,它不起作用.如果我运行 phpunit CategoryTest 它输出:

I have written some test cases and want to try them out with PHPUnit. However, it does not work. If I run phpunit CategoryTest it outputs:

PHPUnit 3.7.14 by Sebastian Bergmann.

如果我执行 phpunit --log-json error.log CategoryTest,error.log 文件会显示:

If I do phpunit --log-json error.log CategoryTest, error.log file displays:

{"event":"suiteStart","suite":"CategoryTest","tests":5}  
{"event":"testStart","suite":"CategoryTest","test":"CategoryTest::test__construct"}

因此,它发现文件中有 5 个测试,开始执行第一个测试,并且无缘无故停止.是否有任何日志可以找到它无法继续执行的原因?另外,如果我在其他文件上运行测试,比如 phpunit --log-json error.log UserTest,shell 不会显示任何输出,error.log 文件也不会显示.

So, it finds that there are 5 tests in the file, starts doing the first one and for no reason stops. Is there any log where I could find a reason why it would not continue execution? Also, if I run test on some other file, say phpunit --log-json error.log UserTest, the shell does not display any output and neither does error.log file.

我尝试重新安装它,正如其他类似问题之一中所建议的那样,但它没有做任何事情.

I tried reinstalling it, as it was suggested in one of the other similar questions, but it didn't do anything.

有什么办法可以解决吗?

Any ideas how I could fix it?

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文件.

Variable $mdb2 comes from the db_connect.php file.

我想通了.问题是我包含了一个类外部的变量.

推荐答案

您不应该在您的 TestCase 中覆盖 __construct() 方法.构造函数为测试设置了模拟生成器和其他一些强制性的东西,所以如果你覆盖构造函数,你会得到很多奇怪的行为和不需要的副作用.setUp() 方法是您应该用来初始化测试的特殊方法.

You shouldn't overwrite the __construct() method in your TestCase. The constructor sets up the mock generator and some other mandatory thing for the test, so you get a lot of strange behaviour and unwanted side effects if you overwrite the constructor. The setUp() method is the special method you should use to initialize your test.

相关文章