19

I am currently setting up a development environment for my new project and I am thinking hard about one thing. I plan on using Yii. I want to have functional tests automated, I already have gerrit and Jenkins in place, working.

I read this note and found out that selenium has something like html suite. I never heard of it before, I always wrote my tests to be run via PHPUnit. I used default Yii functional tests environment as described here.

So my question is, what are pros and cons of both approaches? I already can think of one pro and con of html suite. Pro is, that writing tests is really easy - you just "click" them in Selenium IDE. The con is, that I probably would have to run db fixtures before testing and I couldn't change them, or run them like - load fixtures, run test, load fixture, run test, but I guess that would be make the tests much sloppier. What do you think/what is your experience in that matter?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Nebril
  • 3,153
  • 1
  • 33
  • 50
  • 2
    Selenium functional tests take much time, you still need to keep using unit tests as they are extremely fast to run and valuable for your app. Selenium should mainly be used for Javascript testing, though some other flows can be tested as it covers user interaction better. – arkoak Feb 01 '13 at 13:27
  • 1
    https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-php-formatters/ Add this plugin to firefox, and you can then generate tests in Selenium IDE, and then export them into php, where all you need to do is modify the class name and you can run them as a functional test in Yii, via phpunit. This includes being able to set whatever db fixtures you want. – Willem Renzema Feb 05 '13 at 19:03
  • Maybe you should checkout behat for these kind of tests instead of selenium. It is more readable than selenium tests and serves as specification. There is an integration module for yii available (did not try) [YiiExtension](https://github.com/Behat/YiiExtension) as for the behat [behat](http://behat.org/). As for fixtures it is easy to load them and implement using '@loadSomeFeatureFixture' tags in the scenario descriptions and implementing in behat context class – Mantas Jan 30 '14 at 14:50

2 Answers2

2

there is an easy way to write functional tests. you can downlaod an addon for selenium IDE and then add the php formatter for this addon, and you can make and export functional tests very eeeasy.

then you should configure your protected/config/test.php and protected/tests/bootstrap.php

you should probably edit protected/tests/phpunit.xml and remove any browser, other than fire fox.

then do like Willem Renzema said and change the test made by this addon and change the name of the class to WebTestCase and remove the setUp()

then you are ready to run tests!

cheers

Developerium
  • 7,155
  • 5
  • 36
  • 56
-1

The two packages that are well integrated with Yii, is PhPUnit and Selenium Remote Control. So these two are more common between Yii developers.

Regarding, deploying functional test, I'm not sure how deep you want to go, but as far as I used it, it was pretty easy. Functional tests, are literally php classes, which are extended from CWebTestCase. Yii CWebTestCase class description

Naming convention is you have to name you class ending with Test like ExampleTest.php, and store the files under protected/tests/functional. You need to set up everything including, both packages and also changing Yii configuration file and your default browser. If everything goes well, a sample test can be like this:

class ExampleTest extends WebTestCase
{

public function testContact()
{
    $this->open('?r=site/contact');
    $this->assertTextPresent('A string on contact page');
    $this->assertElementPresent('name=ContactForm[name]');
    $this->type('name=ContactForm[name]','tester');
    $this->type('name=ContactForm[email]','tester@example.com');
    $this->type('name=ContactForm[subject]','test subject');
    $this->click("//input[@value='Submit']");
    $this->waitForTextPresent('Body cannot be blank.');
}
}

That's all I knew about it and I hope it helped.

HesamDadafarin
  • 571
  • 4
  • 9
  • 2
    Thanks, but you didn't really write anything that isn't present in the link I provided: http://www.yiiframework.com/doc/guide/1.1/en/test.functional – Nebril Feb 03 '13 at 20:19