17
FUNCTIONAL TESTS IN SYMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years [email protected] http://sayed.justetc.net

F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years [email protected]

Embed Size (px)

Citation preview

Page 1: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

FUNCTIONAL TESTS IN SYMFONY

Sayed Ahmed

B.Sc. Eng. in Computer Science & EngineeringM. Sc. in Computer Science

Exploring Computing for 14+ years

[email protected]://sayed.justetc.net

Page 2: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

FUNCTIONAL TESTS to test your application from end to end from the request made by a browser to the response

sent by the server To test all the layers of an application:

the routing, the model the actions and the templates

Facilitate to test use cases Test scenarios (application usage scenarios)

Functional tests in Symfony provide a way to easily describe scenarios Each scenario can then be played automatically over and

over again by simulating the experience a user has in a browser

Page 3: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

AUTOMATED TESTING USING SELENIUM

http://seleniumhq.org/ “Selenium automates browsers. That's it. What

you do with that power is entirely up to you. Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.”

Page 4: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

THE SFBROWSER CLASS sfBrowser provides methods that simulates

navigation done in a classic browser: get() Gets a URL post() Posts to a URL call() Calls a URL (used for PUT and DELETE

methods) back() Goes back one page in the history forward() Goes forward one page in the history reload() Reloads the current page click() Clicks on a link or a button select() selects a radiobutton or checkbox deselect() deselects a radiobutton or checkbox restart() Restarts the browser

Page 5: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

THE SFBROWSER CLASS

setHttpHeader() Sets an HTTP header setAuth() Sets the basic authentication

credentials setCookie() Set a cookie removeCookie() Removes a cookie clearCookies() Clears all current cookies followRedirect() Follows a redirect

Page 6: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

SOME USAGE EXAMPLES OF THE SFBROWSER METHODS

Page 7: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

THE SFTESTFUNCTIONAL CLASS

all tasks that generate a module automatically create a basic functional test file:

Page 8: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

THE ABOVE CODE IS EQUIVALENT TO (USES: SFTESTFUNCTIONAL ):

Page 9: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

THE REQUEST TESTER (WITH('REQUEST')->BEGIN)

isParameter() Checks a request parameter value

isFormat() Checks the format of a request isMethod() Checks the method hasCookie() Checks whether the request has

a cookie with the given name isCookie() Checks the value of a cookie

Page 10: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

THE RESPONSE TESTER

checkElement() Checks if a response CSS selector match some criteria

checkForm() Checks an sfForm form object debug() Prints the response output to ease

debug matches() Tests a response against a regexp isHeader() Checks the value of a header isStatusCode() Checks the response status code isRedirected() Checks if the current response is a

redirect isValid() Checks if a response is well-formed XML

(you also validate the response again its document type be passing true as an argument)

Page 11: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

RUNNING FUNCTIONAL TESTS

Execute the test file $ php

test/functional/frontend/categoryActionsTest.php Use the test:functional Task

$ php symfony test:functional frontend categoryActions

Page 12: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

TEST DATA

include(dirname(__FILE__).'/../../bootstrap/functional.php');  

$browser = new sfTestFunctional(new sfBrowser());

Doctrine_Core::loadData(sfConfig::get('sf_test_dir').'/fixtures');

Page 13: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

WRITING FUNCTIONAL TESTS Writing functional tests is like playing a scenario in a

browser

Page 14: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

DEBUGGING FUNCTIONAL TESTS

$browser->with('response')->debug(); symfony provides the ~debug|Debug~()

method to output the response header and content:

Page 15: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

FUNCTIONAL TESTS HARNESS

The test:functional task can also be used to launch all functional tests for an application:

$ php symfony test:functional frontend

Page 16: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

TESTS HARNESS

there is also a task to launch all tests for a project (unit and functional): $ php symfony test:all

Page 17: F UNCTIONAL T ESTS IN S YMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net

REFERENCES http://

www.symfony-project.org/jobeet/1_4/Doctrine/en/09