- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码中有一个断言。像这样的东西:
assert('is_string($var)');
如果我为 PHPUnit 编写的测试导致此断言失败并显示消息,警告:assert():Assertion "is_string($var)"failed in/path/to/file.php on line # ##
而且,我的测试也失败了。我尝试将 @expectedException PHPUnit_Framework_Error_Warning
添加到文档 block according to the documentation ,但这无济于事。我需要做什么才能让我的测试预期此断言会失败?
最佳答案
来自 php.net/assert :
Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be TRUE and that indicate some programming errors if not or to check for the presence of certain features like extension functions or certain system limits and features.
Assertions should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated.
因此对于正常的代码逻辑,使用 bool 值或一些预定义的常量。对于异常逻辑,使用普通的 if
语句并为无效输入抛出 Exception
。
如果您真的热衷于保留断言,您可以定义一个断言回调,它会抛出一个您可以在 PHPUnit 中捕获的Exception
。
// PHP 5.3 Anonymous function as callback
// code is untested
assert_options(ASSERT_CALLBACK, function($file, $line, $code) {
throw new Exception('Assert failed in $file on line $line');
});
关于php - 如何使用 PHPUnit 处理失败的 PHP 断言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7488004/
我可以在所有 PHPUnit 测试完成后自动运行 PHP 脚本吗? 我想在所有测试完成后报告一些非致命问题(即正确但次优的测试结果)。 最佳答案 假设您可以检测到此类次优结果,PHPUnit(v9 及
假设我要测试三种状态下的数据库一致性: 插入数据后 - 我想确保已将一定数量的行插入到数据库表中。 当数据更新后-我想重复行数量测试(数量必须与插入后相同) 删除数据时 - 我想确保所有数据都已删除。
在 pear.phpunit.de/PHPUnit 中安装 PHPUnit 未知 channel pear.phpunit.de 时出错 无效的包名称/包文件 "Debian-4 操作系统上的 pea
谁能给我解释一下使用名为 phpunit.xml.dist 或 phpunit.xml 的 PHPunit 配置文件有什么区别。 official documentation提到两个名字: PHPUn
如果我只想使用来自数据提供者的一个数据运行测试,我该怎么做? 我在这个线程上尝试了解决方案,但没有奏效 Cannot run single test with data provider in PHP
如何编写自定义断言,例如 assertFoo($expected, $actual) ,它的行为类似于关于错误“堆栈跟踪”的内置断言? 我目前定义了以下方法(在扩展 PHPUnit_Framework
我想在我的 Windows 服务器上安装 PHPUnit 3.7。我遵循了各种说明 here并以 PHPUnit 3.4.1 结束。当我尝试使用以下方法再次安装它时: pear update chan
我尝试在 PhpUnit 的 WebTestCase 中发送原始数据,但它不起作用: $jsonEvent = '{ "type": "invoice.payment_succeeded"
我收到了 PHPUnit 错误的代码覆盖率报告,我相信这是 XDebug 的一个错误。 如何配置 PHPUnit 以使用 one of its other drivers ,即PHPDBG? (我使用
我正在我的第一个项目中使用 PHPUnit 进行单元测试。事情进展顺利;但是,我已经开始通过最近的测试获得以下输出: .........................................
我刚刚学习如何使用 PHPUnit。我从命令行运行它,在结果中我得到OK,但是有问题!测试:5,断言:15,警告:2,弃用:5。 我不知道如何让它解释导致弃用和警告计数的原因。 如何找出测试代码中导致
是否有比 $this->assertTrue(false) 更官方的方法来强制 phpunit 失败? 最佳答案 我相信这应该在测试用例中起作用: $this->fail('Message'); 关于
我正在尝试为我的 Silex 应用程序编写一些测试,但遇到了问题。 我有以下 phpunit.xml 文件 ./
目前,我正在尝试使用命令行 phpunittests/Unit/ExampleTest 在 Laravel 项目上使用 phpunit 运行一些简单的单元测试。但它只运行第一种方法。有人有解决办法吗?
我的一些测试用例使用自定义测试库。而且这些测试用例非常慢。所以我想只在构建服务器中运行它们,而不是在我的本地服务器中运行它们。我想在本地运行其他测试。 以下是目录结构。 slow 目录中的那些是应该排
我有以下测试/文件夹: tests/ ClassOne.test.php ClassTwo.test.php ClassThree.test.php 我必须将以下 setUp() 和
我使用 phpunit 的测试套件功能来组织我的测试。我这样做是为了以后能够并行运行测试。 对于不同的目录,这是相对直接的。例如,我可以按捆绑拆分测试套件。 tests/BundleOne
我必须用内部数组测试一个数组。 我的数组如下所示。 $testdata=Array ( [0] => Array ( [
问题:为什么 PHPUnit 似乎在严格模式下运行? 问题: PHPUnit 4.3.1 by Sebastian Bergmann. Configuration read from /full/pa
我已经通过 Composer 安装了 PHPUnit,并将其设置为通过设置运行单元测试。 但是当我运行测试时,测试报告器出现以下错误(tets 执行得很好): Unable to attach tes
我是一名优秀的程序员,十分优秀!