Slide 1

Slide 1 text

Getting Started With Getting Started With TDD + Travis CI TDD + Travis CI By Liam Norman | Superbalist.com 1 . 1

Slide 2

Slide 2 text

Who Am I? Who Am I? Software Engineer at Superbalist.com Organiser of PHP Cape Town Blog at www.liamnorman.com On twitter as @liamjnorman 1 . 2

Slide 3

Slide 3 text

Objectives Objectives 1 . 3

Slide 4

Slide 4 text

Objectives Objectives What is Testing and TDD? 1 . 3

Slide 5

Slide 5 text

Objectives Objectives What is Testing and TDD? Why is testing useful? 1 . 3

Slide 6

Slide 6 text

Objectives Objectives What is Testing and TDD? Why is testing useful? Write our first tests with PHPUnit in Laravel 5 1 . 3

Slide 7

Slide 7 text

Objectives Objectives What is Testing and TDD? Why is testing useful? Write our first tests with PHPUnit in Laravel 5 Implement Laravel Dusk to implement our first browser tests. 1 . 3

Slide 8

Slide 8 text

Objectives Objectives What is Testing and TDD? Why is testing useful? Write our first tests with PHPUnit in Laravel 5 Implement Laravel Dusk to implement our first browser tests. How to simply use Travis CI 1 . 3

Slide 9

Slide 9 text

What is Software Testing? What is Software Testing? 1 . 4

Slide 10

Slide 10 text

What is Software Testing? What is Software Testing? It's an activity to check whether the actual results match the expected results and to ensure that the software system is Defect free. 1 . 4

Slide 11

Slide 11 text

“People often underestimate the time they spend debugging. They underestimate how much time they can spend chasing a long bug. With testing, I know straight away when I have added a bug. That lets me fix the bug immediately. There are few things more frustrating or time wasting than debugging. Wouldn’t it be a hell of a lot quicker if we just didn’t create the bugs in the first place” - Martin Fowler 1 . 5

Slide 12

Slide 12 text

Why Test? Why Test? 1 . 6

Slide 13

Slide 13 text

Why Test? Why Test? It saves time. 1 . 6

Slide 14

Slide 14 text

Why Test? Why Test? It saves time. It increases confidence in your codebase. 1 . 6

Slide 15

Slide 15 text

Why Test? Why Test? It saves time. It increases confidence in your codebase. Points out defects and errors that were made during the development phase of a project or task. 1 . 6

Slide 16

Slide 16 text

Why Test? Why Test? It saves time. It increases confidence in your codebase. Points out defects and errors that were made during the development phase of a project or task. Open Source Software 1 . 6

Slide 17

Slide 17 text

1 . 7

Slide 18

Slide 18 text

What makes a good test suite? What makes a good test suite? 1 . 8

Slide 19

Slide 19 text

What makes a good test suite? What makes a good test suite? Confidence system works 1 . 8

Slide 20

Slide 20 text

What makes a good test suite? What makes a good test suite? Confidence system works Reliable 1 . 8

Slide 21

Slide 21 text

What makes a good test suite? What makes a good test suite? Confidence system works Reliable Fast 1 . 8

Slide 22

Slide 22 text

What makes a good test suite? What makes a good test suite? Confidence system works Reliable Fast Simple 1 . 8

Slide 23

Slide 23 text

What is TDD? What is TDD? "Test-driven development (TDD) is an evolutionary approach to development which combines test-first development where you write a test before you write just enough production code to fulfill that test and refactoring." (Beck 2003; Astels 2003) 1 . 9

Slide 24

Slide 24 text

Testing Types Testing Types 1 . 10

Slide 25

Slide 25 text

Testing Types Testing Types Unit Testing 1 . 10

Slide 26

Slide 26 text

Testing Types Testing Types Unit Testing Functional Testing 1 . 10

Slide 27

Slide 27 text

Testing Types Testing Types Unit Testing Functional Testing Acceptance Testing 1 . 10

Slide 28

Slide 28 text

Testing Types Testing Types Unit Testing Integration Testing Functional Testing Acceptance Testing 1 . 10

Slide 29

Slide 29 text

Testing Pyramid Testing Pyramid (Will Hamill, 2018) 1 . 11

Slide 30

Slide 30 text

Learn testing in one day! Learn testing in one day! That title is wrong... 1 . 12

Slide 31

Slide 31 text

Learn testing in one day! Learn testing in one day! That title is wrong... Learn to test certain elements 1 . 12

Slide 32

Slide 32 text

Learn testing in one day! Learn testing in one day! That title is wrong... Learn to test certain elements Any tests are better than none, aim for 100% coverage 1 . 12

Slide 33

Slide 33 text

Broken Window Theory and Testing Broken Window Theory and Testing 1 . 13

Slide 34

Slide 34 text

Test All The Things Immediately! Test All The Things Immediately! 1 . 14

Slide 35

Slide 35 text

OUR APP OUR APP 1 . 15

Slide 36

Slide 36 text

Arrange - Act - Assert Arrange - Act - Assert 1 . 16

Slide 37

Slide 37 text

Arrange - Act - Assert Arrange - Act - Assert 1. Arrange - Set up the test's initial state 1 . 16

Slide 38

Slide 38 text

Arrange - Act - Assert Arrange - Act - Assert 1. Arrange - Set up the test's initial state 2. Act - Perform the action that you wish to test 1 . 16

Slide 39

Slide 39 text

Arrange - Act - Assert Arrange - Act - Assert 1. Arrange - Set up the test's initial state 2. Act - Perform the action that you wish to test 3. Assert - Inspect the system or results 1 . 16

Slide 40

Slide 40 text

Laravel TestCase Laravel TestCase 1 . 17

Slide 41

Slide 41 text

Test Databases Test Databases [ 'driver' => 'mysql', 'host' => env('DB_TEST_HOST', '127.0.0.1'), 'port' => env('DB_TEST_PORT', '3306'), 'database' => env('DB_TEST_DATABASE', 'forge'), 'username' => env('DB_TEST_USERNAME', 'forge'), 'password' => env('DB_TEST_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], 1 . 18

Slide 42

Slide 42 text

Configuring PHPUnit Configuring PHPUnit ... ./tests ./app 1 . 19

Slide 43

Slide 43 text

Creating Our 1st Unit Test Creating Our 1st Unit Test 1 . 20

Slide 44

Slide 44 text

Creating Our 1st Unit Test Creating Our 1st Unit Test 1 . 21

Slide 45

Slide 45 text

Our 1st Unit Test Our 1st Unit Test 'My Test Podcast', 'description' => 'Awesome Test Podcast', 'web_url' => 'https://MyTestPodCast', 'feed_url' => 'http://feeds.com/MyTestPodCast', 'feed_thumbnail_location' => 'images/mytestpodcast.png', ]; Podcast::create($params); $this->assertDatabaseHas('podcasts', $params); } } 1 . 22

Slide 46

Slide 46 text

1 . 23

Slide 47

Slide 47 text

TDD Mantra TDD Mantra 1 . 24

Slide 48

Slide 48 text

Going Green Going Green

Slide 49

Slide 49 text

1 . 26

Slide 50

Slide 50 text

PHPUnit (Basic) Assertions PHPUnit (Basic) Assertions assertEquals(expected, actual); // e.g. $this->assertEquals($podcast->name, 'my podcast'); $this->assertTrue(condition); // e.g. $this->assertTrue($podcast->active); $this->assertContains(needle, haystack); // e.g. $this->assertContains($podcast->id, $podcasts); 1 . 27

Slide 51

Slide 51 text

Database Assertions Database Assertions assertDatabaseHas($table, array $data); // e.g. $this->assertDatabaseHas('podcasts', ['name' => 'laravel podcast'] ); $this->assertDatabaseMissing($table, array $data); // e.g. $this->assertDatabaseMissing('podcasts', ['name' => 'deleted podcast'] ); $this->assertSoftDeleted($table, array $data); // e.g. $this->assertSoftDeleted('podcasts', ['id' => '1'] ); 1 . 28

Slide 52

Slide 52 text

JSON API Assertions JSON API Assertions assertStatus($statusCode) // e.g. $this->assertStatus(200); $this->assertExactJson($expected); // e.g. $this->assertExactJson([ 'name' => 'My Podcast', 'is_active' => true ]); $this->assertJsonFragment($expected); // e.g. $this->assertJsonFragment([ 'name' => 'My Podcast' ]); 1 . 29

Slide 53

Slide 53 text

Minimising Details Minimising Details 'My Test Podcast', 'description' => 'Awesome Test Podcast', 'web_url' => 'https://MyTestPodCast', 'feed_url' => 'http://feeds.com/MyTestPodCast', 'feed_thumbnail_location' => 'images/mytestpodcast.png', ]; Podcast::create($params); $this->assertDatabaseHas('podcasts', $params); } } 1 . 30

Slide 54

Slide 54 text

Model Factories Model Factories 1 . 31

Slide 55

Slide 55 text

Model Factories Model Factories Allows us to quickly create dummy data. 1 . 31

Slide 56

Slide 56 text

Model Factories Model Factories Allows us to quickly create dummy data. Cleans up our testing implementations. 1 . 31

Slide 57

Slide 57 text

Model Factories Model Factories Allows us to quickly create dummy data. Cleans up our testing implementations. 1 . 31

Slide 58

Slide 58 text

Model Factories Model Factories define(Podcast::class, function (Faker $faker) { return [ 'name' => 'My Test Podcast', 'description' => 'Awesome Test Podcast', 'web_url' => 'https://MyTestPodCast', 'feed_url' => 'http://feeds.com/MyTestPodCast', 'feed_thumbnail_location' => 'testpodcast.png', 'active' => true, ]; }); 1 . 32

Slide 59

Slide 59 text

Refactoring with Model Factories Refactoring with Model Factories create(); $this->assertDatabaseHas( 'podcasts', $podcast->toArray() ); } 1 . 33

Slide 60

Slide 60 text

Feature Testing Feature Testing 1 . 34

Slide 61

Slide 61 text

Feature Testing Feature Testing Features are functionality that are added or modified to software. 1 . 34

Slide 62

Slide 62 text

Feature Testing Feature Testing Features are functionality that are added or modified to software. Feature testing involves testing that a feature works such as favouriting podcasts. 1 . 34

Slide 63

Slide 63 text

Unit Tests vs Feature Tests Unit Tests vs Feature Tests 1 . 35

Slide 64

Slide 64 text

Unit Tests vs Feature Tests Unit Tests vs Feature Tests Unit Tests are written to ensure that a particular method (or a unit) of a class performs a set of specific tasks. 1 . 35

Slide 65

Slide 65 text

Unit Tests vs Feature Tests Unit Tests vs Feature Tests Unit Tests are written to ensure that a particular method (or a unit) of a class performs a set of specific tasks. Feature Tests ensure that the system is functioning as users are expecting it to. 1 . 35

Slide 66

Slide 66 text

Creating our First Feature Test Creating our First Feature Test 1 . 36

Slide 67

Slide 67 text

Our First Feature Test Our First Feature Test create(); $response = $this->get("/podcasts/{$podcast->id}/favourite"); $this->assertDatabaseHas('podcasts', [ 'id' => $podcast->id, 'is_favourite' => true, ]); $response->assertStatus(200); } } 1 . 37

Slide 68

Slide 68 text

Analysing Test Failures Analysing Test Failures Since Laravel 5.5, we can toggle exception handling within our tests. get("/podcasts/{$invalidId}"); $response->assertOk(); } 1 . 38

Slide 69

Slide 69 text

PHPUnit 7.3.5 by Sebastian Bergmann and contributors. .F 2 Time: 211 ms, Memory: 16.00MB There was 1 failure: 1) PodcastTest::cannot_view_missing_podcast() Response status code [404] does not match expected 200 status code. Failed asserting that false is true. ... 1 . 39

Slide 70

Slide 70 text

withoutExceptionHandling(); $invalidId = 991239123912999193192391; $response = $this->get("/podcasts/{$invalidId}"); $response->assertOk(); } Analysing Test Failures Analysing Test Failures 1 . 40

Slide 71

Slide 71 text

PHPUnit 7.3.5 by Sebastian Bergmann and contributors. .E Time: 191 ms, Memory: 16.00MB There was 1 error: 1) PodcastTest::cannot_view_missing_podcast() Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Podcast] 9.91239123913E+23 ... 1 . 41

Slide 72

Slide 72 text

withoutExceptionHandling(); $invalidId = 991239123912999193192391; $response = $this->get("/podcasts/{$invalidId}") $response->assertStatus(404); } 1 . 42

Slide 73

Slide 73 text

Laravel Dusk Laravel Dusk 1 . 43

Slide 74

Slide 74 text

Laravel Dusk Laravel Dusk Easy Installation 1 . 43

Slide 75

Slide 75 text

Laravel Dusk Laravel Dusk Easy Installation No JDK or Selenium installs 1 . 43

Slide 76

Slide 76 text

Laravel Dusk Laravel Dusk Easy Installation No JDK or Selenium installs Standalone ChromeDriver 1 . 43

Slide 77

Slide 77 text

Laravel Dusk Laravel Dusk Easy Installation No JDK or Selenium installs Standalone ChromeDriver Easy Browser Testing 1 . 43

Slide 78

Slide 78 text

Setting up Laravel Dusk Setting up Laravel Dusk 1 . 44

Slide 79

Slide 79 text

Creating our First Browser Test Creating our First Browser Test 1 . 45

Slide 80

Slide 80 text

Our First Browser Test Our First Browser Test create(); foreach ($podcasts as $podcast) { // visit podcasts route and ensure podcast name is visible $this->browse(function (Browser $browser) use ($podcast) { $browser->visit("/podcasts/") ->assertSee($podcast->name); }); } } } 1 . 46

Slide 81

Slide 81 text

Browser Tests In Detail Browser Tests In Detail 1 . 47

Slide 82

Slide 82 text

Browser Tests In Detail Browser Tests In Detail Simulates User 1 . 47

Slide 83

Slide 83 text

Browser Tests In Detail Browser Tests In Detail Simulates User App works end-to-end 1 . 47

Slide 84

Slide 84 text

Browser Tests In Detail Browser Tests In Detail Simulates User App works end-to-end Slower 1 . 47

Slide 85

Slide 85 text

Browser Tests In Detail Browser Tests In Detail Simulates User App works end-to-end Slower Need to make assertions through UI 1 . 47

Slide 86

Slide 86 text

Mocking in Laravel Mocking in Laravel 1 . 48

Slide 87

Slide 87 text

Mocking in Laravel Mocking in Laravel Laravel comes with helpers to mock events, storage, jobs 1 . 48

Slide 88

Slide 88 text

Mocking in Laravel Mocking in Laravel Laravel comes with helpers to mock events, storage, jobs You can use Mockery, PHPUnit etc if you wish to become more in-depth with your mocking. 1 . 48

Slide 89

Slide 89 text

Our Event Our Event podcast = $podcast; // upload logic in listener } } 1 . 49

Slide 90

Slide 90 text

Testing Events Testing Events make(); $this->json('POST', '/podcasts/store', $podcast->toArray()); Event::assertDispatched(PodcastAdded::class, function ($event) { $podcast = Podcast::firstOrFail(); return $event->podcast->is($podcast); }); } 1 . 50

Slide 91

Slide 91 text

1 . 51

Slide 92

Slide 92 text

What is CI? What is CI? 1 . 52

Slide 93

Slide 93 text

What is CI? What is CI? “A software development practice where members of a team integrate their work frequently … verified by an automated build (including tests) to detect integration errors” - Martin Fowler 1 . 52

Slide 94

Slide 94 text

What is CI? What is CI? “A software development practice where members of a team integrate their work frequently … verified by an automated build (including tests) to detect integration errors” - Martin Fowler Combine the process with automated testing using PHPUnit, Dusk etc then continuous integration can enable your code to be dependable. 1 . 52

Slide 95

Slide 95 text

What is Travis CI? What is Travis CI? 1 . 53

Slide 96

Slide 96 text

What is Travis CI? What is Travis CI? 1 . 53

Slide 97

Slide 97 text

What is Travis CI? What is Travis CI? 1 . 53

Slide 98

Slide 98 text

What is Travis CI? What is Travis CI? A hosted continuous integration platform Open Source .travis.yml details how Travis interacts with project Features an advanced build system 1 . 53

Slide 99

Slide 99 text

Why Use Travis? Why Use Travis? 1 . 54

Slide 100

Slide 100 text

Why Use Travis? Why Use Travis? Nothing to install, Travis has a web-based interface. 1 . 54

Slide 101

Slide 101 text

Why Use Travis? Why Use Travis? Nothing to install, Travis has a web-based interface. Free to use for open-source projects! 1 . 54

Slide 102

Slide 102 text

Why Use Travis? Why Use Travis? Nothing to install, Travis has a web-based interface. Free to use for open-source projects! Integrates nicely with GitHub without any developer effort 1 . 54

Slide 103

Slide 103 text

The Build System The Build System 1 . 55

Slide 104

Slide 104 text

Setting up Travis CI Setting up Travis CI 1 . 56

Slide 105

Slide 105 text

Setup Travis with Laravel & Setup Travis with Laravel & PHPUnit PHPUnit 1 . 57

Slide 106

Slide 106 text

Setup Travis with Laravel & Setup Travis with Laravel & PHPUnit PHPUnit Add a .travis.yml file to your application directory 1 . 57

Slide 107

Slide 107 text

Setup Travis with Laravel & Setup Travis with Laravel & PHPUnit PHPUnit Add a .travis.yml file to your application directory Configure .travis.yml 1 . 57

Slide 108

Slide 108 text

Setup Travis with Laravel & Setup Travis with Laravel & PHPUnit PHPUnit Add a .travis.yml file to your application directory Configure .travis.yml We will use the same phpunit and database setup that we did earlier. 1 . 57

Slide 109

Slide 109 text

Setup Travis with Laravel & Setup Travis with Laravel & PHPUnit PHPUnit Add a .travis.yml file to your application directory Configure .travis.yml We will use the same phpunit and database setup that we did earlier. Add various PHP versions to build against (if necessary) 1 . 57

Slide 110

Slide 110 text

Setup Travis with Laravel & Setup Travis with Laravel & PHPUnit PHPUnit Add a .travis.yml file to your application directory Configure .travis.yml We will use the same phpunit and database setup that we did earlier. Add various PHP versions to build against (if necessary) Add services if needed (Redis, Memcached etc…) 1 . 57

Slide 111

Slide 111 text

.env.travis .env.travis APP_ENV=testing APP_URL=http://127.0.0.1:8000 APP_KEY=SomeRandomString DB_CONNECTION=testing DB_TEST_CONNECTION=mysql DB_TEST_HOST=127.0.0.1 DB_TEST_PORT=3306 DB_TEST_DATABASE=larapod_app_testing DB_TEST_USERNAME=root DB_TEST_PASSWORD= CACHE_DRIVER=array SESSION_DRIVER=array QUEUE_DRIVER=sync 1 . 58

Slide 112

Slide 112 text

.travis.yml .travis.yml language: php php: - 7.1 - 7.2 services: - mysql before_script: - cp .env.travis .env - mysql -e 'create database larapod_app_testing;' - composer self-update - composer install --no-interaction - php artisan key:generate - php artisan migrate --seed - export DISPLAY=:99.0 - sh -e /etc/init.d/xvfb start - ./vendor/laravel/dusk/bin/chromedriver-linux & - php artisan serve & script: - vendor/bin/phpunit - php artisan dusk notifications: email: recipients: - [email protected] on_success: always on_failure: always 1 . 59

Slide 113

Slide 113 text

Live build log Live build log 1 . 60

Slide 114

Slide 114 text

Announcing Your Build Status Announcing Your Build Status 1 . 61

Slide 115

Slide 115 text

Travis Outcome Travis Outcome 1 . 62

Slide 116

Slide 116 text

Testing Tips Testing Tips 1 . 63

Slide 117

Slide 117 text

Testing Tips Testing Tips Follow the TDD Mantra (red-green-refactor) 1 . 63

Slide 118

Slide 118 text

Testing Tips Testing Tips Follow the TDD Mantra (red-green-refactor) Test For Failure 1 . 63

Slide 119

Slide 119 text

Testing Tips Testing Tips Follow the TDD Mantra (red-green-refactor) Test For Failure Keep Tests Small 1 . 63

Slide 120

Slide 120 text

Testing Tips Testing Tips Follow the TDD Mantra (red-green-refactor) Test For Failure Keep Tests Small Descriptive Test Method Signatures 1 . 63

Slide 121

Slide 121 text

Testing Tips Testing Tips Follow the TDD Mantra (red-green-refactor) Test For Failure Keep Tests Small Descriptive Test Method Signatures Practice 1 . 63

Slide 122

Slide 122 text

Continued Reading Continued Reading 1 . 64

Slide 123

Slide 123 text

Thanks Thanks 1 . 65