Functional Testing with Symfony2 - Symfony Live 2013, Portland

13 downloads 14112 Views 174KB Size Report
May 23, 2013 ... Symfony Functional Tests. ▷ Application as HTTP blackbox. ▷ Use a Browser ( Client) for interaction. ▷ Assertions on HTTP responses.
Functional Testing with Symfony2 Symfony Live 2013, Portland Benjamin Eberlei Qafoo GmbH 23th May 2013

Me I

Working at Qafoo

Helping people to create high quality web applications.

http://qafoo.com I

Doctrine Developer

I

Symfony Contributor (usually some days before feature freeze)

I

Twitter @beberlei and @qafoo

Testing in Symfony

I

Unit-Tests

I

Integration/Functional Tests

I

Acceptance Tests

I

System Tests (End-To-End)

Unit Testing

Use Multiple Feedback Cycles

Use Multiple Feedback Cycles

I

Long cycles, slow tests

I

Short cycles, fast tests

I

Find a good mix between them

Symfony Functional Tests

I

Application as HTTP blackbox

I

Use a Browser (Client) for interaction

I

Assertions on HTTP responses

I

WebTestCase integration into PHPUnit

I

Symfony2 Extension for Behat

Example: WebTestCase 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

< p h p u n i t> < ! −− . . −−> < s e r v e r name= ” KERNEL DIR ” v a l u e = ” app / ” / > < / php> < t e s t s u i t e name= ” MyProject T e s t s u i t e ” > < d i r e c t o r y > s r c / ∗ / ∗ Bundle / Tests < / d i r e c t o r y > < d i r e c t o r y > s r c / ∗ / Bundle / ∗ Bundle / Tests < / d i r e c t o r y > < ! −− . . −−> < / p h p u n i t>

Symfony Component: BrowserKit I

Symfony\Component\BrowserKit\Client object

I

API to simulate a browser session

I

Handles Cookies, Session and History Symfony HttpKernel implementation

I

I I I

I

wrapped around your application kernel Stimulate HttpKernelInterface::handle @igorw’s session ”The HttpKernelInterface is a lie”

Goutte I I

Testing ”over the wire” using Guzzle HTTP client Time-Travel back to @mtdowling’s session yesterday

Client#request()

Parameter

$method $uri $parameters $files $server $content

Description GET, POST, ... Relative Uri to Base Path with Query String POST Parameters Uploaded Files Headers and Environment POST Body String

Symfony Component: DomCrawler

I

Simple API for HTML/XML traversal

I

Similar to jQuery API Two traversal languages I XPath with filterXpath($xPathSelector) I CSS Selector with filter($cssSelector)

I

I

Client::request() returns a Crawler instance

Filter XPath Example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19