better testing with php unit

23
BETTER TESTING WITH PHPUNIT SiteCrafting, Inc. October 2014

Upload: sitecrafting

Post on 06-Jul-2015

207 views

Category:

Software


4 download

DESCRIPTION

Improving our process through automated browser testing

TRANSCRIPT

Page 1: Better Testing With PHP Unit

BETTER TESTINGWITH PHPUNITSiteCrafting, Inc. October 2014

Page 2: Better Testing With PHP Unit

ADVANTAGES● QA personnel time is EXPENSIVE. Unit tests

will catch bugs before they hit QA.● Unit tests are fast and accurate.● Once a unit test is programmed, it can be run

on-demand.● Unit tests can hook into commits and

deployments to promote code quality.

Page 3: Better Testing With PHP Unit

TERMINOLOGYTest A block of code that tries to do something in your applicationAssertion A single check that always

evaluates to true or falsePHPUnit Testing framework for PHP Selenium Server that drives a local browser

Page 4: Better Testing With PHP Unit

TWO DIFFERENT TYPESBROWSER TestingRuns in a real web browser

Assertions check DOM elements

SOFTWARE TestingRuns purely in-code

Assertions check PHP variables

Page 5: Better Testing With PHP Unit

BROWSER TESTINGProgrammed test runs in a real browser. Uses Selenium WebDriver functions to...● Target DOM elements by id or css class● Click elements● Send keystrokes

And then…● Read DOM content to make assertions

Page 6: Better Testing With PHP Unit

BROWSER TESTINGBaker Project

Projects will come with their own tests.

(e.g. MyTest.class.php)

Selenium Server

Drives browsers and returns assertion results

Console

or Jenkins

or SVN

or other tool

Page 7: Better Testing With PHP Unit

SOFTWARE TESTINGProgrammed tests run directly on encapsulated objects.

Deep-tests software by checking every single component in PHP OOP code. Extremely thorough and effective.

Page 8: Better Testing With PHP Unit

SOFTWARE TESTINGBaker Project

/tests/MyTest.class.php

Console

or Jenkins

or SVN

or other tool

EncapsulatedPHP Object

Calls Assertion

Page 9: Better Testing With PHP Unit

TEST CLASS ANATOMYrequire_once(TEST_RESOURCES_ROOT.'vendor/facebook/webdriver/lib/__init__.php');

Page 10: Better Testing With PHP Unit

TEST CLASS ANATOMYrequire_once(TEST_RESOURCES_ROOT.'vendor/facebook/webdriver/lib/__init__.php');

class MyGearboxTest extends PHPUnit_Framework_TestCase{}

Page 11: Better Testing With PHP Unit

TEST CLASS ANATOMYrequire_once(TEST_RESOURCES_ROOT.'vendor/facebook/webdriver/lib/__init__.php');

class MyGearboxTest extends PHPUnit_Framework_TestCase{ public function setUp() { $capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox'); $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities, 1000); }}

Page 12: Better Testing With PHP Unit

TEST CLASS ANATOMYrequire_once(TEST_RESOURCES_ROOT.'vendor/facebook/webdriver/lib/__init__.php');

class MyGearboxTest extends PHPUnit_Framework_TestCase{ public function setUp() { $capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox'); $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities, 1000); }

public function tearDown() { $this->webDriver->close(); }}

Page 13: Better Testing With PHP Unit

TEST CLASS ANATOMYrequire_once(TEST_RESOURCES_ROOT.'vendor/facebook/webdriver/lib/__init__.php');

class MyGearboxTest extends PHPUnit_Framework_TestCase{ public function setUp() { $capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox'); $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities, 1000); }

public function testOne() { … } public function testTwo() { … } public function testThree() { … } public function testFour() { … }

public function tearDown() { $this->webDriver->close(); }}

Any function named test* will be detected and run by the

PHPUnit test framework.

Page 14: Better Testing With PHP Unit

TEST FUNCTION ANATOMYpublic function testOne(){}

Page 15: Better Testing With PHP Unit

TEST FUNCTION ANATOMYpublic function testOne(){ // OPEN webpage $this->webDriver->get($this->url);}

Page 16: Better Testing With PHP Unit

TEST FUNCTION ANATOMYpublic function testOne(){ // OPEN webpage $this->webDriver->get($this->url); // GET elements $txtUsername = $this->webDriver->findElement(WebDriverBy::id('login_un')); $txtPassword = $this->webDriver->findElement(WebDriverBy::id('login_pw')); $btnSubmit = $this->webDriver->findElement(WebDriverBy::cssSelector('.buttons.med'));}

Page 17: Better Testing With PHP Unit

TEST FUNCTION ANATOMYpublic function testOne(){ // OPEN webpage $this->webDriver->get($this->url); // GET elements $txtUsername = $this->webDriver->findElement(WebDriverBy::id('login_un')); $txtPassword = $this->webDriver->findElement(WebDriverBy::id('login_pw')); $btnSubmit = $this->webDriver->findElement(WebDriverBy::cssSelector('.buttons.med'));

// LOGIN $txtUsername->click(); $this->webDriver->getKeyboard()->sendKeys('validuser'); $txtPassword->click(); $this->webDriver->getKeyboard()->sendKeys('validpass');

$btnSubmit->click();}

Page 18: Better Testing With PHP Unit

TEST FUNCTION ANATOMYpublic function testOne(){ // OPEN webpage $this->webDriver->get($this->url); // GET elements $txtUsername = $this->webDriver->findElement(WebDriverBy::id('login_un')); $txtPassword = $this->webDriver->findElement(WebDriverBy::id('login_pw')); $btnSubmit = $this->webDriver->findElement(WebDriverBy::cssSelector('.buttons.med'));

// LOGIN $txtUsername->click(); $this->webDriver->getKeyboard()->sendKeys('validuser'); $txtPassword->click(); $this->webDriver->getKeyboard()->sendKeys('validpass');

$btnSubmit->click();

// ASSERT that page title contains string ‘Dashboard’ $this->assertContains('Dashboard', $this->webDriver->getTitle(), 'Valid user/pass failed login.');}

Page 19: Better Testing With PHP Unit

TESTING GEARBOXTestingResources folder will be required on all dev servers, whether shared or local. This contains the PHPUnit+Selenium+WebDriver testing framework.

Tests in project repository work by extending a class in TestingResources.

Gearbox 1.7 will deploy with browser tests for every module.

Page 20: Better Testing With PHP Unit

TESTING GEARBOXOur system admins will set up a Selenium browser test server that all browser tests will connect to. Concurrent connections are supported.

Today, tests are triggered through console. In near-future, tests will be triggered through admin center or CI process.

Project repositories must pass all tests prior to deployment.

Page 21: Better Testing With PHP Unit

TESTING GEARBOXWe are 100% responsible for the quality of our tests. All software is different and there is no magic bullet. GIGO.

Standard module tests will be written by Software Architect.Additional tests will be written by Senior QA Engineer.

In the future, software tests will be written by originating developer.

Page 22: Better Testing With PHP Unit

QUESTIONS?

Page 23: Better Testing With PHP Unit

THANKS