1 of 59

Automated Testing

For EDU,�Right Now

2 of 59

Steve Persch

Technical Product Marketing Manager�@stevector

PANTHEON.IO

3 of 59

This is not a

Big Idea Talk.

This is a

Pep Talk

4 of 59

PANTHEON.IO

Agenda

05/ Performance and Accessibility

04/ Unit / System Tests

03/ Code Sniffing

02/ Visual Regression Testing

01/ The TDD Dream

06/ Demo Driven Development

5 of 59

The Test Driven Development Dream

6 of 59

In the TDD Dream...

You get BIG benefits

  • The TDD process helps you design your code
  • Maximum regression test value
  • The feeling of your job is totally changed

PANTHEON.IO

7 of 59

Somehow the time is never right

PANTHEON.IO

8 of 59

Right Now

9 of 59

Zombie Projects

Myth: Not worth the time to add any automated tests

10 of 59

11 of 59

Visual Regression Testing

Try

12 of 59

13 of 59

14 of 59

15 of 59

16 of 59

17 of 59

18 of 59

19 of 59

Visual Regression Testing Has Limits

False Positives / False Negatives

  • Lots of noise
  • Randomize/rotating content
  • Still saves time

PANTHEON.IO

20 of 59

Workflow Overhaul

Trap: We can't run tests until we write tests

We can't write tests until we have a way to run tests

21 of 59

22 of 59

23 of 59

Code Sniffing

Try

24 of 59

Use an indent of 2 spaces, with no tabs.

Drupal Coding Standards

https://www.drupal.org/docs/develop/standards/coding-standards

25 of 59

Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls.

Drupal Coding Standards

https://www.drupal.org/docs/develop/standards/coding-standards

26 of 59

Code Sniffing

Easy to add / Clear pass or fail

  • Start with already clean files
  • Clean a large code base gradually
  • Counter-productive to do code style reviews manually

PANTHEON.IO

27 of 59

Post-Launch Bug-Fixing

Catch-22: I feel like I need full test coverage

but I know writing a test for everything is not worth the time

28 of 59

Post-Launch Bug-Fixing

Trap: If I'm not doing TDD

then I'm only getting

half of the benefit of writing the test

29 of 59

If you think your client is annoyed they have to report a bug, just wait for them to report that bug a second time

Me

Right Now

30 of 59

Unit / System Tests

Try

31 of 59

A bug is a mismatch between expected behavior and actual behavior

Also Me

Also Right Now

32 of 59

Example Bug / Behat Scenario

Old events are on the homepage

PANTHEON.IO

33 of 59

Scenario: Events in the future

Given I log in as a content_administrator

When I create an Event with a date in the future

Then it appears on the events page in the upcoming events section

And that event appears on the homepage

Scenario: Events in the past

Given I log in as a content_administrator

When I create an Event with a date in the past

Then that event does not appears on the homepage

And it appears on the events page in the past events section

34 of 59

Example Bug / PHPUnit Test

The website isn't recognizing iPod browsers

PANTHEON.IO

35 of 59

Detecting iOS

function is_ios( $user_agent ) {

if ( strpos( $user_agent, 'iPad' ) ) {

return true;

} elseif ( strpos( $user_agent, 'iPhone' ) ) {

return true;

} else {

return false;

}

}

36 of 59

Compare Actual and Expected

public function testIphone() {

$actual = is_ios( 'Mozilla/5.0 (iPhone)' );

$expected = true;

$this->assertEquals( $actual, $expected );

}

public function testAndroid() {

$actual = is_ios( 'Mozilla/5.0 (Android 4.4; Tablet;)' );

$expected = false;

$this->assertEquals( $actual, $expected );

}

37 of 59

PASS

38 of 59

Write the Failing Test First

public function testIpod() {

$actual = is_ios( 'Mozilla/5.0 (iPod)' );

$expected = true;

$this->assertEquals( $actual, $expected );

}

39 of 59

FAIL

40 of 59

Add One or Elseif

function is_ios( $user_agent ) {

if ( strpos( $user_agent, 'iPad' ) ) {

return true;

} elseif ( strpos( $user_agent, 'iPhone' ) ) {

return true;

} elseif ( strpos( $user_agent, 'iPod' ) ) {

return true;

} else {

return false;

}

}

41 of 59

PASS

42 of 59

Freedom to Refactor Safely

function is_ios( $user_agent ) {

return preg_match( '/iPad|iPod|iPhone/', $user_agent );

}

43 of 59

PASS

44 of 59

Unit Test /

System Tests

You write the expected behavior

  • There are a ton of testing frameworks to choose from
  • Write tests at the same level you are working
    • Write unit tests for new units
    • Write system tests when configuring a whole system

PANTHEON.IO

45 of 59

Pre-launch Site Building

Oh no: My client says they care about performance and accessibility

but they ask for things that hurt both

46 of 59

The website is slow.

Can you make it faster?

Your Client

Two days before launch

47 of 59

Performance and Accessibility Audits

Try

48 of 59

49 of 59

Performance and Accessibility audits

It's better to know

  • Starting early will help avoid awkward client conversations
  • They aren't perfect, especially for accessibility

PANTHEON.IO

50 of 59

Discovery Phase

Huh: Writing tests is supposed to happen from the beginning

but this feels… too early?

51 of 59

52 of 59

Behavior-Driven Development

Try

53 of 59

Demo-Driven Development

Actually, first try

54 of 59

Just write down

how you're gonna show the client

that the thing is done

55 of 59

Demoing past / future events

It doesn't have to be complicated

  • Sign in as admin
  • Make an event with a date in the past
  • Save
  • Go to the events listing page
  • Show that the test event is under "Past Events"
  • Go to the homepage
  • Point out that the event isn't there

PANTHEON.IO

56 of 59

Demo-Driven Development

Baby-step toward BDD

  • You need to demo eventually anyway.
  • Get many of the TDD/BDD benefits without the headache of figuring out a testing framework

PANTHEON.IO

57 of 59

What parts of your job can robots do better and faster than you?

58 of 59

WebOps

WebOps is a set of practices that facilitates collaboration and automates processes to improve the productivity of the whole web team from developers and designers to content editors, stakeholders, and more.

The result is cross-functional web teams empowered to develop, test, and release website changes faster and more reliably.

PANTHEON.IO

59 of 59

Thank You!