从 SimpleTest 转换为 PHPUnit $this->UnitTestCase('message .....');

发布于 2024-09-15 09:57:38 字数 268 浏览 5 评论 0原文

PHPUnit 中的哪个操作/方法等于 simpleTest 中的:

$this->UnitTestCase('message .....')

编辑:抱歉我的错误


我认为我所问的问题不直接存在于简单测试中,它只是我们的扩展类。

但是这个方法在测试开始时显示消息 - 它是如何使用 PHPUnit 完成的?

谢谢

Which action/method is in PHPUnit equal to in simpleTest:

$this->UnitTestCase('message .....')

Edit: Sorry for my mistake


I think what I asking about not exist in simple test directly its just our extended class.

But this method display message in begining of test- how its done with PHPUnit?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2024-09-22 09:57:38

我不是 SimpleTest 专家,但据我所知,这是 UnitTestCase 类的构造函数。 PHPUnit 中的等效项是 PHPUnit_Framework_TestCase;您可以通过子类化并定义测试方法来创建自己的测试。请参阅 PHPUnit 文档,了解如何编写测试快速指南和更多信息,但简单地说,这是一个完整的 PHPUnit 测试:

class MyTest extends PHPUnit_Framework_TestCase {
    public function testSomething {
        $this->assertTrue(MyClass::getSomethingTrue());
    }
}

更新: 回答修改后的问题,在 PHPUnit 中显示消息的主要方式是断言失败。每个 assert* 函数末尾都带有一个可选的 $message 参数,您可以使用该参数在断言失败时显示自定义消息。

如果您希望始终显示消息,而不必使断言失败,那么您可以尝试使用简单的print语句。它将散布在测试输出中,因此这可能不是完成您想要的事情的最佳(或最好看)方式,但它肯定会将文本转储到控制台,这正是您所要求的。

如果您正在单元测试期间寻找一些高级调试,您可能还需要考虑某种日志记录框架(甚至只是一个打开文件、打印到它并再次关闭文件的自定义函数)。这样,您可以保留测试输出的完整性,但仍然可以在测试期间随时随地获得额外的自定义消息。

I'm not a SimpleTest expert, but as far as I can tell that's the constructor for the UnitTestCase class. The equivalent in PHPUnit is PHPUnit_Framework_TestCase; you create your own tests by subclassing that and defining test methods. See the PHPUnit docs on writing tests for a quick howto and more info, but briefly, this is a complete PHPUnit test:

class MyTest extends PHPUnit_Framework_TestCase {
    public function testSomething {
        $this->assertTrue(MyClass::getSomethingTrue());
    }
}

Update: to answer the revised question, the primary way to display messages in PHPUnit is on assertion failure. Every assert* function comes with an optional $message argument at the end, which you can use to display a custom message when that assertion fails.

If you want to always display a message, without having to fail an assertion, then you might try a straightforward print statement. It'll be interspersed with the test output, so this may not be the best (or nicest-looking) way of accomplishing what you want, but it'll certainly dump text to a console, which is what you seem to be asking.

If you're looking for some advanced debugging during unit-testing, you may also want to consider a logging framework of some kind (or even just a custom function that opens a file, prints to it, and closes the file again). That way, you preserve the integrity of the test output, but still get extra custom messages wherever you want them during your tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文