Automated Testing
For EDU,�Right Now
Steve Persch
Technical Product Marketing Manager�@stevector
PANTHEON.IO
This is not a
Big Idea Talk.
This is a
Pep Talk
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
The Test Driven Development Dream
In the TDD Dream...
You get BIG benefits
PANTHEON.IO
Somehow the time is never right
PANTHEON.IO
Right Now
Zombie Projects
Myth: Not worth the time to add any automated tests
Visual Regression Testing
Try
Visual Regression Testing Has Limits
False Positives / False Negatives
PANTHEON.IO
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
Code Sniffing
Try
Use an indent of 2 spaces, with no tabs.
Drupal Coding Standards
https://www.drupal.org/docs/develop/standards/coding-standards
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
Code Sniffing
Easy to add / Clear pass or fail
PANTHEON.IO
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
Post-Launch Bug-Fixing
Trap: If I'm not doing TDD
then I'm only getting
half of the benefit of writing the test
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
Unit / System Tests
Try
A bug is a mismatch between expected behavior and actual behavior
Also Me
Also Right Now
Example Bug / Behat Scenario
Old events are on the homepage
PANTHEON.IO
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
Example Bug / PHPUnit Test
The website isn't recognizing iPod browsers
PANTHEON.IO
Detecting iOS
function is_ios( $user_agent ) {
if ( strpos( $user_agent, 'iPad' ) ) {
return true;
} elseif ( strpos( $user_agent, 'iPhone' ) ) {
return true;
} else {
return false;
}
}
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 );
}
PASS
Write the Failing Test First
public function testIpod() {
$actual = is_ios( 'Mozilla/5.0 (iPod)' );
$expected = true;
$this->assertEquals( $actual, $expected );
}
FAIL
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;
}
}
PASS
Freedom to Refactor Safely
function is_ios( $user_agent ) {
return preg_match( '/iPad|iPod|iPhone/', $user_agent );
}
PASS
Unit Test /
System Tests
You write the expected behavior
PANTHEON.IO
Pre-launch Site Building
Oh no: My client says they care about performance and accessibility
but they ask for things that hurt both
The website is slow.
Can you make it faster?
Your Client
Two days before launch
Performance and Accessibility Audits
Try
Performance and Accessibility audits
It's better to know
PANTHEON.IO
Discovery Phase
Huh: Writing tests is supposed to happen from the beginning
but this feels… too early?
Behavior-Driven Development
Try
Demo-Driven Development
Actually, first try
Just write down
how you're gonna show the client
that the thing is done
Demoing past / future events
It doesn't have to be complicated
PANTHEON.IO
Demo-Driven Development
Baby-step toward BDD
PANTHEON.IO
What parts of your job can robots do better and faster than you?
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
Thank You!