10

My Yii app getting phpunit coverage in controllers, models, components, modules but not any views. The problem is that code coverage loader in phpunit includes the view file during preparing a coverage report. Views with forms have calls $this->beginWidget which causes a crash since there is no $this context.

Views dont really have important code or logic but still they have some conditions and even loops to call renderPartial so it would be good to get a view code also covered.

Is there a solution to this problem?

1
  • Great question! I wish more emphasis was placed on unit and functional testing in the framework guides and books rather than the standard boilerplate functionality that you can learn in 10 minutes.
    – AndrewPK
    Mar 25, 2013 at 18:15

1 Answer 1

4
+25

Have you tried extending CWebTestCase? Generally when writing unit tests, you have fixtures and things to provide the necessary data - but with tests on views and 'functional' tests, for web-apps, it's generally easiest to mimic a browser and have it perform actions on the web app as if a user was actually using it. Currently, this mimicking is most easily done with Selenium (in my opinion).

The Yii Guide on Functional Testing is a good place to start as well as the Selenium Documentation. There's also this book that goes over using selenium (I'm not sure if the newest edition does, but I know the previous release with Publication Date: August 11, 2010 did), and Larry Ullman's Yii Book will have chapters on testing and the usage of Selenium in functional tests when he's completed that chapter.

Hope this helps!

Update to further explain CWebTestCase

CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase which directly implements a functional testing foundation that you can use within phpunit in order to test views, test widget creation inside of views, assert that text exists, 'click' on links, etc. These tests are still run from the command line though they require that Selenium-RC server be started upon the test being run and they require a valid browser being configured. A valid browser can be configured with as little code as the following placed inside of the setUp() function:

$this->setBrowser('*firefox /usr/lib/firefox/firefox-bin');

Stating that code coverage cannot be provided by CWebTestCase is not true, as CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase, which provides the following as quoted from 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 and PHPUnit/Extensions/SeleniumTestCase/append.php

as the auto_prepend_file and auto_append_file, respectively. In your test case class that extends PHPUnit_Extensions_SeleniumTestCase, use

protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';

to configure the URL for the phpunit_coverage.php script.

2
  • My testing is console based checking models, and actions and even doing Xpath asserts on html output. The code coverage report on views done by phpunit is the issue. Selenium too wont code cover the lines of php in views.
    – thevikas
    Mar 26, 2013 at 6:40
  • Please see me updated answer below - I believe CWebTestCase provides exactly what you're looking for due to its extending of PHPUnit_Extensions_SeleniumTestCase. If it does not, I don't understand your question enough and I'm sorry I couldn't be of more help.
    – AndrewPK
    Mar 26, 2013 at 14:26

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.