我的 Yii 应用程序在 Controller 、模型、组件、模块中获得 phpunit 覆盖,但没有任何 View 。问题是 phpunit 中的代码覆盖加载器在准备覆盖报告期间包含 View 文件。带有表单的 View 调用 $this->beginWidget
,这会导致崩溃,因为没有 $this 上下文。
View 实际上并没有重要的代码或逻辑,但它们仍然有一些条件甚至循环来调用 renderPartial
,因此最好也包含一个 View 代码。
这个问题有解决办法吗?
最佳答案
您是否尝试过扩展 CWebTestCase ?通常,在编写单元测试时,您有固定装置和其他东西来提供必要的数据——但是对于 View 测试和“功能”测试,对于网络应用程序来说,模仿浏览器并让它在网络应用程序上执行操作通常是最简单的如果用户实际使用它。目前,这种模仿最容易用 Selenium 完成(在我看来)。
Yii 指南 Functional Testing是一个很好的起点以及 Selenium Documentation .还有 this book使用 selenium(我不确定最新版本是否支持,但我知道发布日期为 2010 年 8 月 11 日的先前版本支持),以及 Larry Ullman 的 Yii Book完成该章后,将有关于测试和 Selenium 在功能测试中的使用的章节。
希望这对您有所帮助!
更新进一步解释CWebTestCase
CWebTestCase 扩展PHPUnit_Extensions_SeleniumTestCase它直接实现了一个功能测试基础,您可以在 phpunit 中使用它来测试 View 、测试 View 内的小部件创建、断言文本存在、“单击”链接等。这些测试仍然从命令行运行,尽管它们要求在运行测试时启动 Selenium-RC 服务器,并且需要配置有效的浏览器。一个有效的浏览器可以用像下面这样放在 setUp()
函数中的代码来配置:
$this->setBrowser('*firefox /usr/lib/firefox/firefox-bin');
声明 CWebTestCase 无法提供代码覆盖率是不正确的,因为 CWebTestCase 扩展了 PHPUnit_Extensions_SeleniumTestCase,它提供了引用自 the documentation 的以下内容:
PHPUnit_Extensions_SeleniumTestCase can collect code coverage information for tests run through Selenium:
Copy
PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php
into your webserver's document root directory. In your webserver's php.ini configuration file, configure
PHPUnit/Extensions/SeleniumTestCase/prepend.php
andPHPUnit/Extensions/SeleniumTestCase/append.php
as the
auto_prepend_file
andauto_append_file
, respectively. In your test case class that extendsPHPUnit_Extensions_SeleniumTestCase
, use
protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';
to configure the URL for the
phpunit_coverage.php
script.
关于php - yii phpunit 测试覆盖率 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15432001/