continuous testing in php

108
Continuous Testing in PHP True North PHP 2014 Eric Hogue @ehogue erichogue.ca

Upload: eric-hogue

Post on 06-Jul-2015

277 views

Category:

Software


5 download

DESCRIPTION

Talk I've given at True North PHP 2014. TDD (Test Driven Development) is getting more and more popular. But what can you do to take it to the next level? What if you could know if your tests are passing every time you save a file without taking your hands off the keyboard. This is what continuous testing gives you. In this session, we will cover how you can continuously test your PHP application. We will cover Installation and configuration Running the tests Running static analysers Executing tools that can make your life easier

TRANSCRIPT

Page 1: Continuous testing In PHP

Continuous Testing in PHPTrue North PHP 2014

Eric Hogue@ehogue

erichogue.ca

Page 2: Continuous testing In PHP

Continuous Testing

Page 3: Continuous testing In PHP

Wikipedia

a software development practice that extends test-driven development by means of automatic test execution in the background.

http://en.wikipedia.org/wiki/Continuous_test-driven_development

Page 4: Continuous testing In PHP

How I Discovered it

Page 5: Continuous testing In PHP

Test Driven Development

Page 6: Continuous testing In PHP

Repetitive ...

Page 7: Continuous testing In PHP

Code Kata

Page 8: Continuous testing In PHP

Eureka!

Page 9: Continuous testing In PHP

Automate the Automated Tests

Page 10: Continuous testing In PHP

Searching

Page 11: Continuous testing In PHP

autotest

Page 12: Continuous testing In PHP

AutoPHPUnit

Page 13: Continuous testing In PHP

Watchr

Page 14: Continuous testing In PHP

Guard

Page 15: Continuous testing In PHP

Plugins

Page 16: Continuous testing In PHP

Installation - Settings

Page 17: Continuous testing In PHP

Ruby

Page 18: Continuous testing In PHP

Install Ruby With RVM

$ curl -sSL https://get.rvm.io | bash -s stable --ruby

$ ruby -v

ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

Page 19: Continuous testing In PHP

RubyGems + Bundler

Page 20: Continuous testing In PHP

Gemfile

source 'https://rubygems.org'

group :development do

gem 'guard'

end

Page 21: Continuous testing In PHP

bundle install

$ bundle install

Fetching gem metadata from https://rubygems.org/...........

...

Installing guard 2.6.1

Your bundle is complete!

Page 22: Continuous testing In PHP

Running Guard

$ bundle exec guard

00:53:09 - ERROR - No Guardfile found, please create one with `guard init`.

Page 23: Continuous testing In PHP

Guardfile

Page 24: Continuous testing In PHP

Project root - Guardfile

Page 25: Continuous testing In PHP

Home folder - .Guardfile

Page 26: Continuous testing In PHP

Home folder - .guard.rb

Page 27: Continuous testing In PHP

notification :off

Guardfile

Page 28: Continuous testing In PHP

notification :off

guard 'guardname' do

end

Guardfile

Page 29: Continuous testing In PHP

notification :off

guard 'guardname', :option => value do

end

Guardfile

Page 30: Continuous testing In PHP

notification :off

guard 'guardname', :option => value do

watch(%r{^regex$})

end

Guardfile

Page 31: Continuous testing In PHP

Notifications

Page 32: Continuous testing In PHP

System Notification

Page 33: Continuous testing In PHP

group :development do

gem 'libnotify' #Linux

gem 'growl' #Mac OS

gem 'rb-notifu' #Windows

end

Gemfile

Page 34: Continuous testing In PHP

notification :libnotify #Linux

notification :growl #Mac OS

notification :notifu #Windows

Guardfile

Page 35: Continuous testing In PHP

Terminal Title

Page 36: Continuous testing In PHP

notification :terminal_title

Guardfile

Page 37: Continuous testing In PHP

tmux

Page 38: Continuous testing In PHP

notification :tmux,

display_message: true

Guardfile

Page 39: Continuous testing In PHP

notification :off

No Notifications

Page 40: Continuous testing In PHP

PHP Guards

Page 41: Continuous testing In PHP

Guard::PHPUnit2

Page 42: Continuous testing In PHP

Gemfile

group :development do

...

gem 'guard-phpunit2', :git => "https://github.com/EricHogue/guard-phpunit2.git"

end

Page 43: Continuous testing In PHP

Guardfile

guard 'phpunit2', :cli => '--colors', :tests_path => 'tests' do

watch(%r{^tests/.+Test\.php$})

end

Page 44: Continuous testing In PHP

Guardfile

guard 'phpunit2', :cli => '--colors', :tests_path => 'tests' do

watch(%r{^tests/.+Test\.php$})

end

Page 45: Continuous testing In PHP

Guardfile

guard 'phpunit2', :cli => '--colors', :tests_path => 'tests' do

watch(%r{^tests/.+Test\.php$})

end

Page 46: Continuous testing In PHP

Guardfile

guard 'phpunit2', :cli => '--colors', :tests_path => 'tests' do

watch(%r{^tests/.+Test\.php$})

end

Page 47: Continuous testing In PHP

bundle exec guard

Page 48: Continuous testing In PHP

Guardfile

…watch(%r{^src/(.+)\.php$}) { |m|

"tests/#{m[1]}Test.php"

}

Page 49: Continuous testing In PHP

%r{^src/(.+)\.php$}

src/TDD/Factorial.php

"tests/#{m[1]}Test.php"

phpunit tests/TDD/FactorialTest.php

Page 50: Continuous testing In PHP
Page 51: Continuous testing In PHP

:all_on_start

guard 'phpunit2', :all_on_start =>

true do

end

default => true

Page 52: Continuous testing In PHP

:tests_path

guard 'phpunit2', :tests_path =>

'path/to/tests' do

end

default => pwd

Page 53: Continuous testing In PHP

:all_after_pass

guard 'phpunit2', :all_after_pass =>

true do

end

default => true

Page 54: Continuous testing In PHP
Page 55: Continuous testing In PHP

:keep_failed

guard 'phpunit2', :keep_failed =>

true do

end

default => true

Page 56: Continuous testing In PHP
Page 57: Continuous testing In PHP

:command

guard 'phpunit2', :command =>

'./vendor/bin/phpunit' do

end

default => phpunit

Page 58: Continuous testing In PHP

:cli

guard 'phpunit2',

:cli => '--colors' do

end

default => nil

Page 59: Continuous testing In PHP

Guard::PHPCS

Page 60: Continuous testing In PHP

Gemfile

group :development do

...

gem 'guard-phpcs'

end

Page 61: Continuous testing In PHP

Guardfile

guard 'phpcs', :standard => 'PSR2',

:executable => "./vendor/bin/phpcs" do

watch(%r{.*\.php$})

end

Page 62: Continuous testing In PHP

Guardfile

guard 'phpcs', :standard => 'PSR2',

:executable => "./vendor/bin/phpcs" do

watch(%r{.*\.php$})

end

Page 63: Continuous testing In PHP

Guardfile

guard 'phpcs', :standard => 'PSR2',

:executable => "./vendor/bin/phpcs" do

watch(%r{.*\.php$})

end

Page 64: Continuous testing In PHP

Guardfile

guard 'phpcs', :standard => 'PSR2',

:executable => "./vendor/bin/phpcs" do

watch(%r{.*\.php$})

end

Page 65: Continuous testing In PHP

PHPCS - PSR2

Page 66: Continuous testing In PHP

Guard::PHPMD

Page 67: Continuous testing In PHP

Gemfile

group :development do

...

gem 'guard-phpmd'

end

Page 68: Continuous testing In PHP

Guardfile

guard 'phpmd',

:executable => './vendor/bin/phpmd',

:rules => 'pmd-rules.xml' do

watch(%r{.*\.php$})

end

Page 69: Continuous testing In PHP

Guardfile

guard 'phpmd',

:executable => './vendor/bin/phpmd',

:rules => 'pmd-rules.xml' do

watch(%r{.*\.php$})

end

Page 70: Continuous testing In PHP

Guardfile

guard 'phpmd',

:executable => './vendor/bin/phpmd',

:rules => 'pmd-rules.xml' do

watch(%r{.*\.php$})

end

Page 71: Continuous testing In PHP

pmd-rules.xml

...

<rule ref="rulesets/codesize.xml" />

<rule ref="rulesets/design.xml" />

<rule ref="rulesets/naming.xml" />

<rule ref="rulesets/unusedcode.xml" />

<rule ref="rulesets/controversial.xml" />

...

Page 72: Continuous testing In PHP

Guardfile

guard 'phpmd',

:executable => './vendor/bin/phpmd',

:rules => 'pmd-rules.xml' do

watch(%r{.*\.php$})

end

Page 73: Continuous testing In PHP

PHPMD

Page 74: Continuous testing In PHP

Other Guards

Page 75: Continuous testing In PHP

More

More than 200 plugins

Page 76: Continuous testing In PHP

JavaScript

Page 77: Continuous testing In PHP

Gemfile

group :development do

...

gem 'guard-jasmine'

end

Page 78: Continuous testing In PHP

Guardfile

guard 'jasmine', :jasmine_url => 'http://localhost:8000/SpecRunner.html'

...

end

Page 79: Continuous testing In PHP

Guardfile

guard 'jasmine', :jasmine_url => '',

:server => :none do

watch(%r{spec/.+Spec\.js$})

end

Page 80: Continuous testing In PHP

Guardfile

guard 'jasmine', :jasmine_url => '',

:server => :none do

watch(%r{spec/.+Spec\.js$})

end

Page 81: Continuous testing In PHP

Guard::Jasmine

Page 82: Continuous testing In PHP

LiveReload

Page 83: Continuous testing In PHP

LiveReload

Page 84: Continuous testing In PHP

Gemfile

group :development do

...

gem 'guard-livereload'

end

Page 85: Continuous testing In PHP

Guardfile

guard 'livereload' do

watch(%r{public/.+\.(css|js|html)})

end

Page 86: Continuous testing In PHP
Page 87: Continuous testing In PHP

Guard::Bundler

Page 88: Continuous testing In PHP

Gemfile

group :development do

...

gem 'guard-bundler'

end

Page 89: Continuous testing In PHP

Guardfile

guard :bundler do

watch('Gemfile')

end

Page 90: Continuous testing In PHP

Guard::Bundler

Page 91: Continuous testing In PHP

Guard::Shell

Page 92: Continuous testing In PHP

Gemfile

group :development do

...

gem 'guard-shell'

end

Page 93: Continuous testing In PHP

Guardfile

guard 'shell' do

watch(%r{^.+\.php$}) do |m|

`php -l #{m[0]}`

true

end

end

Page 94: Continuous testing In PHP

Lint check

Page 95: Continuous testing In PHP

Guardfile

`php -l #{m[0]}`

if ($?.success?)

n "#{m[0]} correct",'Syntax',:success

else

n "#{m[0]} incorrect",'Syntax',:failed

end

Page 96: Continuous testing In PHP

Composer

Page 97: Continuous testing In PHP

Guardfile

guard 'shell' do

watch("composer.lock") do |m|

p "Running composer install"

`composer install`

…end

end

Page 98: Continuous testing In PHP

Composer

Page 99: Continuous testing In PHP

Inline Guard

Page 100: Continuous testing In PHP

Guardfile

require 'guard/plugin'

module ::Guard

class BehatGuard < ::Guard::Plugin

end

end

Page 101: Continuous testing In PHP

Guardfile

def start

puts 'Run all Behat tests'

puts `./vendor/bin/behat`

end

Page 102: Continuous testing In PHP

Guardfile

def run_on_changes(paths)

paths.each do |file|

puts `./vendor/bin/behat #{file}`

end

end

Page 103: Continuous testing In PHP

Guardfile

guard 'BehatGuard' do

watch(%r{^features/.+\.feature$})

end

Page 104: Continuous testing In PHP

Behat

Page 105: Continuous testing In PHP

PHPUnit

PHPCS

PHPMD

php -l

composer install

Jasmine

LiveReload

Behat

Page 106: Continuous testing In PHP

Automate Your Automated Test

Page 107: Continuous testing In PHP

Questions?

PHP Mentoring: http://phpmentoring.org/

Comments: https://joind.in/12709twitter: @ehogueBlog: http://erichogue.ca/

Page 108: Continuous testing In PHP

EndlessTunnel.jpg - Trey Ratcliff - https://www.flickr.com/photos/stuckincustoms/4539732832

History.jpg - Morten Diesen - http://www.flickr.com/photos/mortendiesen/8091682612

StreetLights.jpg - William Warby - http://www.flickr.com/photos/wwarby/2460655511

FadeToGrey.jpg - Andreas Levers - https://www.flickr.com/photos/96dpi/2571056264

Kata.jpg - ser... ser... - http://www.flickr.com/photos/el_ser_lomo/3267627038

Archimedes.jpg - Sputnik Beanburger III - https://www.flickr.com/photos/sputnikbeanburgeriii/4690475562/in/photostream/

GearWork2.jpg - Curious Expeditions - https://www.flickr.com/photos/curiousexpeditions/489992128

SARHelicopter.jpg - UK Ministry of Defence - https://www.flickr.com/photos/defenceimages/8695434365

RelayBox.jpg - Ed Hunsinger - https://www.flickr.com/photos/edrabbit/4698481573

Ruby.jpg - Joren De Groof - https://www.flickr.com/photos/jorendegroof/4470431763

RubyGems.png - Linux Screenshots - https://www.flickr.com/photos/xmodulo/14652484443

Files.jpg - Travis Wise - https://www.flickr.com/photos/photographingtravis/14745936519

Root.jpg - きうこ - https://www.flickr.com/photos/kiuko/9112281601

Home.jpg - Warren - https://www.flickr.com/photos/jety/3277812553

Alarm.jpg - Fabian - https://www.flickr.com/photos/snapsi42/3436162040

Containers.jpg - www.GlynLowe.com - https://www.flickr.com/photos/glynlowe/10921733615

Mess.jpg - Moyan Brenn - https://www.flickr.com/photos/aigle_dore/5481297641

Shells.jpg - Bemep - https://www.flickr.com/photos/40626436@N00/40822551

DoItYourself.jpg - Vlasta Juricek - https://www.flickr.com/photos/vlastula/3229196769

Languages.jpg - Valerie Everett - https://www.flickr.com/photos/valeriebb/3008977110

Javascript.jpg - Nathan Smith - https://www.flickr.com/photos/nathansmith/4704268314

Stacks.jpg - Roman Boed - https://www.flickr.com/photos/romanboed/13356494013

js.png - Chris Williams - https://github.com/voodootikigod/logo.js

AssemblyLine.jpg - Fiat Chrysler Automobiles: Corporate - https://www.flickr.com/photos/chryslergroup/13194222244

CarCrash.jpg - JaseMan - http://www.flickr.com/photos/bargas/3695903512/