BDD in the browser (or how to have fun with testing)

A presentation at Codepen Meetup in June 2015 in Glasgow, UK by Aaron Bassett

Slide 1

Slide 1

BDD in the Browser @aaronbassett – rawtech.io

Slide 2

Slide 2

@aaronbassett

Slide 3

Slide 3

@rawtechio

Slide 4

Slide 4

BDD in the Browser @aaronbassett – rawtech.io

Slide 5

Slide 5

Slide 6

Slide 6

Slide 7

Slide 7

TDD is DEAD

Slide 8

Slide 8

“I think that's the direction we're heading. Less emphasis on unit tests, because we're no longer doing test-first as a design practice, and more emphasis on, yes, slow, system tests.” – http://david.heinemeierhansson.com/2014/tdd-is-dead-long-live-testing.html

Slide 9

Slide 9

codon.com/the-dhh-problem

Slide 10

Slide 10

BORING

Slide 11

Slide 11

Having fun with testing. @aaronbassett – rawtech.io

Slide 12

Slide 12

Slide 13

Slide 13

Slide 14

Slide 14

Slide 15

Slide 15

Slide 16

Slide 16

mochajs.org

Slide 17

Slide 17

Slide 18

Slide 18

Slide 19

Slide 19

Slide 20

Slide 20

Slide 21

Slide 21

Slide 22

Slide 22

Slide 23

Slide 23

1 2 3 4 5 6 7 before(function(){ exec("afplay /Codepen/jstest/nyan.mp3"); }); after(function(){ exec("killall afplay"); });

Slide 24

Slide 24

imagesnap

Slide 25

Slide 25

Slide 26

Slide 26

mocha || (imagesnap pic.png && exit 1)

Slide 27

Slide 27

Slide 28

Slide 28

Slide 29

Slide 29

Slide 30

Slide 30

$(date +%y%m%d%H%M%S).png

Slide 31

Slide 31

mocha -R nyan -t 4000 || (imagesnap ~/Pictures/FAIL/$ (date +%y%m%d%H%M%S).png && exit 1)

Slide 32

Slide 32

1 test: 2 mocha ... # Our super long command 3 4 .PHONY: test

Slide 33

Slide 33

Slide 34

Slide 34

2 var exec = require('child_process').exec; 3 var sleep = require('sleep'); 4 var Twitter = require('node-twitter'); 5 6 var mocha = new Mocha({}); 7 var twitterRestClient = new Twitter.RestClient( 8 'CONSUMER_KEY', 9 'CONSUMER_SECRET', 10 'TOKEN', 11 'TOKEN_SECRET' 12 ); 13 14 mocha.addFile('/Users/aaronbassett/twitter/tests.js') 15 16 var failCount = 0; 17 var passCount = 0; 18 19 mocha.run() 20 .on('pass', function(test) { 21 passCount++; 22 }) 23 .on('fail', function(test, err) { 24 failCount++; 25 }) 26 .on('end', function() { 27 if(failCount > 0) { 28 exec("imagesnap /Users/aaronbassett/FAIL.png"); 29 sleep.sleep(2); 30 twitterRestClient.statusesUpdateWithMedia({ 31 'status': '‚ÜÓ ' + passCount + ' passed - ‚Üó ' + failCount + ' failed.', 32 'media[]': '/Users/aaronbassett/FAIL.png' 33 }); 34 } 35 });

Slide 35

Slide 35

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var twitterRestClient = new Twitter.RestClient( ... ); var mocha = new Mocha({}); mocha.addFile(...) mocha.run() .on('pass', function(test) {}) .on('fail', function(test, err) {}) .on('end', function() { if(failCount > 0) { ... } });

Slide 36

Slide 36

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var twitterRestClient = new Twitter.RestClient( ... ); var mocha = new Mocha({}); mocha.addFile(...) mocha.run() .on('pass', function(test) {}) .on('fail', function(test, err) {}) .on('end', function() { if(failCount > 0) { ... } });

Slide 37

Slide 37

1 2 3 4 5 6 7 8 9 10 11 12 var twilio = require('twilio'); var client = new twilio.RestClient( 'TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN' ); client.sms.messages.create({ to: 'MUM', from: 'AARON', body:'I have failed you as a son.' });

Slide 38

Slide 38

1 if(date.getHours() > 17) { 2 client.sms.messages.create({ 3 to: 'WIFE', 4 from: 'AARON', 5 body:'Sorry, I'm going to be a bit late home tonight' 6 }); 7 }

Slide 39

Slide 39

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var twitterRestClient = new Twitter.RestClient( ... ); var mocha = new Mocha({}); mocha.addFile(...) mocha.run() .on('pass', function(test) {}) .on('fail', function(test, err) {}) .on('end', function() { if(failCount > 0) { ... } });

Slide 40

Slide 40

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var twitterRestClient = new Twitter.RestClient( ... ); var mocha = new Mocha({}); mocha.addFile(...) mocha.run() .on('pass', function(test) {}) .on('fail', function(test, err) {}) .on('end', function() { if(failCount > 0) { ... } });

Slide 41

Slide 41

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var twitterRestClient = new Twitter.RestClient( ... ); var mocha = new Mocha({}); mocha.addFile(...) mocha.run() .on('pass', function(test) {}) .on('fail', function(test, err) {}) .on('end', function() { if(failCount > 0) { ... } });

Slide 42

Slide 42

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var twitterRestClient = new Twitter.RestClient( ... ); var mocha = new Mocha({}); mocha.addFile(...) mocha.run() .on('pass', function(test) {}) .on('fail', function(test, err) {}) .on('end', function() { if(failCount > 0) { ... } });

Slide 43

Slide 43

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var twitterRestClient = new Twitter.RestClient( ... ); var mocha = new Mocha({}); mocha.addFile(...) mocha.run() .on('pass', function(test) {}) .on('fail', function(test, err) {}) .on('end', function() { if(failCount > 0) { ... } });

Slide 44

Slide 44

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var twitterRestClient = new Twitter.RestClient( ... ); var mocha = new Mocha({}); mocha.addFile(...) mocha.run() .on('pass', function(test) {}) .on('fail', function(test, err) {}) .on('end', function() { if(failCount > 0) { ... } });

Slide 45

Slide 45

Slide 46

Slide 46

1 mocha.run() 2 .on('fail', function(test, err) { 3 failCount++; 4 }) 5 .on('end', function() { 6 if(failCount === 0) { 7 exec("afplay applause.mp3"); 8 } 9 });

Slide 47

Slide 47

1 mocha.run() 2 .on('fail', function(test, err) { 3 failCount++; 4 }) 5 .on('end', function() { 6 if(failCount === 0) { 7 exec("afplay applause.mp3"); 8 } 9 });

Slide 48

Slide 48

Slide 49

Slide 49

ssh -t raspberrypi ‘echo O M G $NAME! You broke your tests again? | festival --tts'

Slide 50

Slide 50

Team Member Tests ran Failed Passed Frequency Points 10,439 654 9785 3.6 35226 Steven 9,719 2,198 7,521 2.4 18050.4 Sara 7,976 13 7963 2.1 16722.3 Peter 6,793 356 6437 1.5 9655.5 Jo 3,615 402 3213 2.4 7711.2 Jane 2,923 476 2447 2.6 6362.2 Simon 1,423 187 1236 0.5 618 Jack 543 165 378 0.3 113.4 Mike 847 302 545 0.1 54.5 Anne

Slide 51

Slide 51

Slide 52

Slide 52

Slide 53

Slide 53

Slide 54

Slide 54

BORING

Slide 55

Slide 55

Slide 56

Slide 56

@aaronbassett