2. 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.
3. TERMINOLOGY
Test A block of code that tries to do
something in your application
Assertion A single check that always
evaluates to true or false
PHPUnit Testing framework for PHP
Selenium Server that drives a local browser
4. TWO DIFFERENT TYPES
BROWSER Testing
Runs in a real web browser
Assertions check DOM
elements
SOFTWARE Testing
Runs purely in-code
Assertions check PHP
variables
5. BROWSER TESTING
Programmed 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
6. BROWSER TESTING
Baker 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
7. SOFTWARE TESTING
Programmed tests run directly on encapsulated objects.
Deep-tests software by checking every single component in
PHP OOP code. Extremely thorough and effective.
8. SOFTWARE TESTING
Baker Project
/tests/MyTest.class.php
Console
or Jenkins
or SVN
or other tool
Calls Assertion
Encapsulated
PHP Object
9. TEST CLASS ANATOMY
require_once(TEST_RESOURCES_ROOT.'vendor/facebook/webdriver/lib/__init__.php');
10. TEST CLASS ANATOMY
require_once(TEST_RESOURCES_ROOT.'vendor/facebook/webdriver/lib/__init__.php');
class MyGearboxTest extends PHPUnit_Framework_TestCase
{}
11. TEST CLASS ANATOMY
require_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);
}
}
12. TEST CLASS ANATOMY
require_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();
}
}
13. TEST CLASS ANATOMY
require_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.
15. TEST FUNCTION ANATOMY
public function testOne()
{
// OPEN webpage
$this->webDriver->get($this->url);
}
16. TEST FUNCTION ANATOMY
public 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'));
}
17. TEST FUNCTION ANATOMY
public 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();
}
18. TEST FUNCTION ANATOMY
public 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.');
}
19. TESTING GEARBOX
TestingResources 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.
20. TESTING GEARBOX
Our 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.
21. TESTING GEARBOX
We 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.