【问题标题】:phpunit @before @after not workingphpunit @before @after 不工作
【发布时间】:2014-10-21 13:18:47
【问题描述】:

我需要在每个测试方法之前运行一个方法。根据文档https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.before,我做了这个小测试:

/**
 * @before
 */
public function setupSomeFixtures()
{
    echo "setupSomeFixtures\n";
}

/**
 * @after
 */
public function tearDownSomeFixtures()
{
    echo "tearDownSomeFixtures\n";
}

public function testTruc()
{
    echo "un test\n";
}

输出:

PHPUnit 3.7.21 by Sebastian Bergmann.

Configuration read from <root>\phpunit.xml

.un test


Time: 5 seconds, Memory: 14.25Mb

OK (1 test, 0 assertions)

我错过了什么吗?

【问题讨论】:

  • 我从来没有用过这个,但是.. @before 不是在测试前运行吗?和@after 测试完成后?所以你的输出只会在测试之间输出结果? (只是随机猜测)
  • phpunit 文档说:The @before annotation can be used to specify methods that should be called before each test method in a test case class. 所以我理解得很好,@before 应该在每个测试方法之前运行,而不是在测试用例类之前运行

标签: php symfony phpunit


【解决方案1】:

Before 注释是在 phpunit 3.8 版中引入的,因此在您的 3.7 中不起作用。

这是我的输出:

PHPUnit 4.2.6 by Sebastian Bergmann.

Configuration read from /<omissis>/app/phpunit.xml.dist

The Xdebug extension is not loaded. No code coverage will be generated.

.setupSomeFixtures
tearDownSomeFixtures
.setupSomeFixtures
tearDownSomeFixtures
.setupSomeFixtures
tearDownSomeFixtures
IsetupSomeFixtures
tearDownSomeFixtures
.setupSomeFixtures
un test
tearDownSomeFixtures


Time: 451 ms, Memory: 23.00Mb

所以升级到phpunit 3.8或者major

【讨论】:

  • 3.*>3.7不稳定,我试试4.3(稳定)
  • 嗨@smarber,如果您觉得我的回答有用,请考虑支持我的回答
猜你喜欢
  • 2012-05-21
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 2011-10-23
  • 2014-10-08
  • 1970-01-01
  • 2021-08-21
  • 2013-04-16
相关资源
最近更新 更多