better testing with php unit

Post on 06-Jul-2015

202 Views

Category:

Software

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

Improving our process through automated browser testing

TRANSCRIPT

BETTER TESTINGWITH PHPUNITSiteCrafting, Inc. October 2014

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.

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

TWO DIFFERENT TYPESBROWSER TestingRuns in a real web browser

Assertions check DOM elements

SOFTWARE TestingRuns purely in-code

Assertions check PHP variables

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

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

SOFTWARE TESTINGProgrammed tests run directly on encapsulated objects.

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

SOFTWARE TESTINGBaker Project

/tests/MyTest.class.php

Console

or Jenkins

or SVN

or other tool

EncapsulatedPHP Object

Calls Assertion

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

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

class MyGearboxTest extends PHPUnit_Framework_TestCase{}

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); }}

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(); }}

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.

TEST FUNCTION ANATOMYpublic function testOne(){}

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

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'));}

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();}

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.');}

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.

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.

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.

QUESTIONS?

THANKS

top related