Going beyond unit tests - WordCamp London 2015

60
COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012 TimNash.co.uk @tnash Because we all love to test right? Going beyond unit tests…

Transcript of Going beyond unit tests - WordCamp London 2015

Page 1: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Because we all love to test right?

Going beyond unit tests…

Page 2: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Once upon a time…

Page 3: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

MEWebsite Twitter

Page 4: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Most people don’t write tests

Page 5: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Testing should be fun?

Otherwise we won’t test our side projects

Page 6: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Unit tests scare folks…

Page 7: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

So let’s not talk about them…

Page 8: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

This is not a talk about BDD, DDD, ATDD or TDD Though you should check them out

Page 9: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Tools, Concepts not methodology

Page 10: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

So what’s Testing then?

Page 11: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Dear client… To Test: Go to /form/ Enter Form Details Click Submit & the form should submit !

Any problems let me know!

Page 12: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Dear Developer…

It doesn’t work!

Page 13: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Dear client…

Did you try to clear your cache?

Page 14: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

So what’s Testing then really?

Page 15: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Codeception BDD style testing framework

Page 16: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Unit Testing, Mocking, Functional, Acceptance Testing, Regression Testing and other stuff

Page 17: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Acceptance Testing

Page 18: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Acceptance TestingCodebase independent

Tests what client expectsBreaks things like a client

Easy to read and writeCan start straight away

Slow to runNeeds “live” end points

Infrastructure dependent

Page 19: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Dear client… To Test: Go to /form/ Enter Form Details Click Submit & the form should submit !

Any problems let me know!

Page 20: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php $I = new AcceptanceTester($scenario); $I->wantTo('Fill in a form'); $I->amOnPage('/form/'); $I->see('This is a form'); $I->fillField('event', 'WordCamp London'); $I->fillField('date', '22 March 2015'); $I->fillField('where', 'London Metropolitan University'); $I->click('Save'); $I->see('Form Submitted');

Page 21: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Dear client… To Test login into the WordPress Dashboard: Go to /wp-login.php Enter your username and your password Click Login & you should be redirected to the dashboard !

Page 22: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php $I = new AcceptanceTester($scenario); $I->wantTo('Login to WordPress Admin'); $I->amOnPage('/wp-login.php'); $I->fillField('Username', 'admin'); $I->fillField('Password','password'); $I->click('Log In'); $I->see('Dashboard');

Page 23: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Trying to Login to WordPress Admin (LoginCept) Scenario: * I am on page "/wp-login.php" * I fill field "Username","admin" * I fill field "Password","password" * I click "Log In" * I see "Dashboard" * I click "Posts" PASSED

php codecept.phar run acceptance --steps

Page 24: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Reporting in console, html, xml

Page 25: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

WebDriver, Selenium, PhatomJS

Page 26: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Webdriver Pretty fast Headless CURL based solution Doesn’t support javascript User agent not always recognised Often caught in WAFs Enabled by default

Page 27: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Selenium Browser Automation Supports Firefox and Chrome Used in multiple frameworks Can operate headless with virtual frame buffer Bit of a pig to set up Codeception support a Docker container to ease setup

Page 28: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

PhantomJS Headless Supports Javascript User agent not always recognised Often caught in WAFs

Page 29: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Functional Testing

Page 30: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Functional Testing Like Acceptance Testing Push data into end point Directly call AJAX end points No browser emulation so simple web driver Unlike unit testing not done in isolation

Page 31: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Test REST like APIs…WP-API Endpoints

Page 32: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php $I = new ApiTester($scenario); $I->wantTo(‘Return all posts’); $I->sendGet(‘posts’); $I->seeResponseCodeIs(200); $I->seeResponseIsJson(); $I->seeResponseContains(“content”);

Page 33: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Test REST like APIs…RSS, ATOM Feeds

Page 34: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php $I = new ApiTester($scenario); $I->wantTo(‘Return full Feed’); $I->sendGet(‘feed’); $I->seeResponseCodeIs(200); $I->seeResponseIsXml(); $I->seeResponseContains(“channel”);

Page 35: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Test cli…WP-CLI Commands

Page 36: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php $I = new CliTester($scenario); $I->runShellCommand(‘wp example'); $I->seeInShellOutput(‘Example List');

Page 37: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Test Email…wp_mail();

Page 38: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

MailCatcher + APITester<?php $I = new ApiTester($scenario); $I->wantTo(‘Return all emails’); $I->sendGet(‘messages’); $I->seeResponseCodeIs(200); $I->seeResponseIsJson();

Page 39: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Test XML-RPC…Really???Well yes you can!

Page 40: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php $I = new ApiTester($scenario); $I->wantTo(‘Return XML-RPC ’); $I->sendXMLRPCMethodCall(‘posts’); $I->seeResponseIsXMLRPC(); $I->seeResponseContains(“posts”);

Page 41: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

DBs, Factories and tearing down

Page 42: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

WP-Browserhttps://github.com/lucatume/wp-browser

Page 43: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php $I = new AcceptanceTester($scenario); $I->wantTo('create and check for a user'); // create a subscriber user with user login and id $I->haveUserInDatabase('rincewind', 100); // check for created user $I->seeUserInDatabase(array('user_login' => 'rincewind', 'ID' => 100));

Page 44: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

WP-CLI is your friend

Page 45: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Wait this is all PHP…

Page 46: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Class based Test Suites :)

Page 47: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php class exampleCEST { /** * @before addPost * @after checkFrontpage */ public function checkLogin(\AcceptanceTester $I) { … } }

Page 48: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

PageObjects & StepObjects

Page 49: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php class loginPage { public static $URL = ‘/wp-login.php’; public static $usernameField = "Username"; public static $passwordField = "Password"; public static $submitButton = “Login in"; }

Page 50: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php $I = new AcceptanceTester($scenario); $I->wantTo('Login to WordPress Admin'); $I->amOnPage(loginPage::URL); $I->fillField(loginPage::usernameField, 'admin'); $I->fillField(loginPage::passwordField,'password'); $I->click(loginPage::submitButton); $I->see('Dashboard');

Page 51: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Helpers, modules & Addons

Page 52: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

<?php namespace Codeception\Module; use Codeception\Module; class MailCatcher extends Module { protected $mailcatcher; protected $config = array('url', 'port'); !

public function _initialize() { $url = trim($this->config['url'], '/') . ':' . $this->config['port']; $this->mailcatcher = new \Guzzle\Http\Client($url); } } ....

.... /** * Messages * * Get an array of all the message objects * * @return array * @author Jordan Eldredge <[email protected]> **/ protected function messages() { $response = $this->mailcatcher->get('/messages')->send(); $messages = $response->json(); usort($messages, array($this, 'messageSortCompare')); return $messages;

MailCatcher Module

Page 53: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Debuging

Page 54: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

codeception run --debug !

$u = $foobard->haveUserInDatabase('rincewind', 100); $this->debugSection(‘User Creation', $u);

Page 55: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Alternatives?

Page 56: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Multiple Test solutions out there

PHPUnit + Selenium Behat & Mink Mocha Fitness

!

many many more…

Page 57: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Testing is not just for development

Page 58: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

Automated Testing of Backups

Page 59: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

So about those unit tests…

Page 60: Going beyond unit tests -  WordCamp London 2015

COMPANY NAME PRESENTATION TITLE 12 - 12 - 2012TimNash.co.uk @tnash

My Stuff aka Plugs: My Site: timnash.co.uk My Patreon Page: patreon.com/tnash