Put an end to regression with codeception testing

75
Put an end to regression with Codeception Testing Joe Ferguson

Transcript of Put an end to regression with codeception testing

Page 1: Put an end to regression with codeception testing

Put an end to regression with Codeception Testing

Joe Ferguson

Page 2: Put an end to regression with codeception testing

Who Am I?Joe Ferguson

PHP Developer

Engineer @ Aol.

Twitter: @JoePFerguson

Organizer of @MemphisPHP

OSMI Board Member

@NomadPHP Lightning Talks

Passionate about Community

Page 3: Put an end to regression with codeception testing

Testing Resources

http://grumpy-learning.com

Page 4: Put an end to regression with codeception testing

Assumptions

You know you need to write tests

You have written a test or two before

You might have even like writing tests

Page 5: Put an end to regression with codeception testing

“There are no solutions, only trade offs”

- Joe Ferguson

- Paul M. Jones

- Thomas Sowell

Page 6: Put an end to regression with codeception testing

How is your test coverage?

Page 7: Put an end to regression with codeception testing

Code Metrics

Code Climate - Grades your code quality

Coveralls - Shows what code is not covered by your test suite

PHPUnit Code Coverage Analysis

Page 8: Put an end to regression with codeception testing

When your app crashes how hard does it crash?

Page 9: Put an end to regression with codeception testing

“Can’t see the forest for the trees”

Page 10: Put an end to regression with codeception testing

“Can’t see the forest for the trees”X XApp on Fire unit tests

^ ^

Page 11: Put an end to regression with codeception testing

2 unit tests 0 integration tests

https://twitter.com/thepracticaldev/status/687672086152753152

Page 12: Put an end to regression with codeception testing

2 unit tests 0 integration tests

Page 13: Put an end to regression with codeception testing

2 unit tests 0 integration tests

https://twitter.com/ThePracticalDev/status/852508104914874369

Page 14: Put an end to regression with codeception testing

Unit Tests ARE Important

Page 15: Put an end to regression with codeception testing

Testing Trinity

Unit Tests - This method works

Functional Tests - This feature works

Acceptance Tests - This product works

Page 16: Put an end to regression with codeception testing

Testing Trinity

Unit Tests - I know what broke

Functional Tests - Something Broke

Acceptance Tests - This product broke

Page 17: Put an end to regression with codeception testing

Regression Testing

Page 18: Put an end to regression with codeception testing

Regression Testing is Easy**

Page 19: Put an end to regression with codeception testing

Bug Report Scenario

User reports that feature “Return Foo” always returns “Bar”

Create a test to ensure that feature “Return Foo” returns “Foo”

See test fails to confirm report or verify there is no bug

Correct your application logic and make your user happy

Page 20: Put an end to regression with codeception testing

You should not run into this bug again*

* If you do, you know because you have a test!

Page 21: Put an end to regression with codeception testing
Page 22: Put an end to regression with codeception testing

Install Codeception

Page 23: Put an end to regression with codeception testing

Set Up Codeception

Page 24: Put an end to regression with codeception testing

Codeception Files

Page 25: Put an end to regression with codeception testing

Unit Test Suite

Page 26: Put an end to regression with codeception testing

HomeController Foo Function

Page 27: Put an end to regression with codeception testing

New Unit Test

./vendor/bin/codecept g:phpunit unit "App\Http\Controllers\HomeController"

Page 28: Put an end to regression with codeception testing

HomeControllerTest.php

Page 29: Put an end to regression with codeception testing

Write our test for foo()

Page 30: Put an end to regression with codeception testing

Run our test

Page 31: Put an end to regression with codeception testing

Run only unit tests

Page 32: Put an end to regression with codeception testing

Later that week…

Page 33: Put an end to regression with codeception testing

We caught the bug!

Page 34: Put an end to regression with codeception testing

Customer Confidence++

Page 35: Put an end to regression with codeception testing

I can do that with PHPUnit!

Page 36: Put an end to regression with codeception testing

Functional Testing

This feature works

Page 37: Put an end to regression with codeception testing

Functional Tests TL;DR

https://twitter.com/RebeccaSlatkin/status/852627030092939274

Page 38: Put an end to regression with codeception testing

Baked in Functional Testing

Page 39: Put an end to regression with codeception testing

Functional Test Suite

Page 40: Put an end to regression with codeception testing

New Functional Test

./vendor/bin/codecept g:cept functional HomeController

Page 41: Put an end to regression with codeception testing

Functional Test

Page 42: Put an end to regression with codeception testing

HomeController.php

Page 43: Put an end to regression with codeception testing

Test to verify we get redirected

Page 44: Put an end to regression with codeception testing

Run our Functional Tests

Page 45: Put an end to regression with codeception testing

Expand Our Test

Page 46: Put an end to regression with codeception testing

Run our Functional Tests

Page 47: Put an end to regression with codeception testing

Test Failure

Page 48: Put an end to regression with codeception testing

Test Failure Output

Page 49: Put an end to regression with codeception testing

What if something breaks?

Page 50: Put an end to regression with codeception testing

Of course our test fails…

Page 51: Put an end to regression with codeception testing

Browser Output on Failure

Page 52: Put an end to regression with codeception testing

Acceptance Tests

This product works

Page 53: Put an end to regression with codeception testing

PhantomJS

Page 54: Put an end to regression with codeception testing

Acceptance Test Suite

Page 55: Put an end to regression with codeception testing

New Acceptance Test

./vendor/bin/codecept g:cept acceptance RegisterCept

Page 56: Put an end to regression with codeception testing

RegisterCept.php

Page 57: Put an end to regression with codeception testing

Run Our Test

Page 58: Put an end to regression with codeception testing

Run ALL the tests

Page 59: Put an end to regression with codeception testing

There is not much difference between functional and acceptance tests

Page 60: Put an end to regression with codeception testing

Reuse Test Logic

Page 61: Put an end to regression with codeception testing

Refactor RegisterCept.php

Page 62: Put an end to regression with codeception testing

UserCanLoginCept.php

Page 63: Put an end to regression with codeception testing

Run our tests

Page 64: Put an end to regression with codeception testing

There is a problem with our configuration of Codeception

Page 65: Put an end to regression with codeception testing

Create a Clean DB Snapshot

Page 66: Put an end to regression with codeception testing

codeception.yml

Page 67: Put an end to regression with codeception testing

acceptance.suite.yml

Page 68: Put an end to regression with codeception testing

Codecept Build

Page 69: Put an end to regression with codeception testing

DB is cleaned up after

Page 70: Put an end to regression with codeception testing

Testing for Specific Content

Page 71: Put an end to regression with codeception testing

Testing Navigation Clicks

Page 72: Put an end to regression with codeception testing

Legacy Applications

Page 73: Put an end to regression with codeception testing

Recap

Unit tests are important!

Use functional and acceptance suites to test operation of you applications

Turn every bug report into a regression test so you’ll catch issues faster

Too many regression tests? Could indicate workflow or UI/UX issue in your application

Page 74: Put an end to regression with codeception testing

Questions?

Page 75: Put an end to regression with codeception testing

Feedback!

https://joind.in/talk/b7324

Joe FergusonTwitter: @JoePFergusonEmail: [email protected]: joepferguson

Contact Info: