• No results found

Front-end Automated Testing #drupal-fat

N/A
N/A
Protected

Academic year: 2021

Share "Front-end Automated Testing #drupal-fat"

Copied!
67
0
0

Loading.... (view fulltext now)

Full text

(1)

Front-end Automated Testing

(2)

Ruben Teijeiro

@rteijeiro

I don't know

what I like

more: Drupal

or Beer

(3)
(4)
(5)
(6)

Developers

I'm ready for

website

(7)
(8)

DevOps

Almost finished

setting up your

server. Just one

(9)
(10)

Designers

Just redesigned

the website. Now

it's shinny, edgy

and it pops!!

(11)

So,

what?

(12)

Users

(13)

Holy

shit!!

(14)

Clients

Just something

like Facebook!

We need it

yesterday...

And kitten

pics. Everyone

loves kittens!

Better in

Comic Sans

Should work

also in IE7

(15)
(16)
(17)
(18)
(19)

Front-end

(20)
(21)

Fixed

Refactoring

Fixed

Fixed

Fixed

Fixed

Fixed

Fixed

(22)

Oh

man!

(23)
(24)
(25)
(26)

And now I will

show you how it

looks like in

(27)

Now

what?

(28)
(29)
(30)
(31)
(32)
(33)
(34)
(35)

· Automated

· Repeteable

· Easy to understand

· Incremental

· Easy to run

· Fast

Unit Test

(36)

BA-K-47

(37)
(38)

Drupal Modules

· TestSwarm

https://drupal.org/project/testswarm

· FAT

(39)
(40)

· QUnit

· CasperJS

· PhantomJS

· Jasmine

(41)

TestSwarm module

FAT module

(42)
(43)
(44)

/**

* Implements hook_testswarm_tests(). */

function bacon_testswarm_tests() { 'bacon_test' => array(

'module' => 'bacon',

'description' => 'Testing bacon.', 'js' => array(

$path . '/tests/bacon.tests.js' => array(), ),

'dependencies' => array(

array('testswarm', 'jquery.simulate'), ),

'path' => 'admin/bacon/test',

'permissions' => array('test bacon') ),

}

(45)

/*global Drupal: true, jQuery: true, QUnit:true*/

(function ($, Drupal, window, document, undefined) { "use strict";

Drupal.tests.bacon = { getInfo: function() { return {

name: 'Bacon test',

description: 'Testing bacon.', group: 'Bacon'

}; },

tests: function() {

[Insert your QUnit tests here] },

};

})(jQuery, Drupal, this, this.document);

(46)
(47)

Drupal.tests.bacon = { getInfo: function() { return {

name: 'Bacon test',

description: 'Testing bacon.', group: 'Bacon'

}; },

setup: function() {

[Insert your setup code here] },

teardown: function() {

[Insert your teardown code here] },

tests: function() {

[Insert your QUnit tests here] },

};

(48)
(49)
(50)

Assert

Passes if the first argument is truthy.

var

bbq_ready

= true;

QUnit.

ok

(

bbq_ready

,

'Barbecue ready!.'

);

var bbq_ready = false;

QUnit.

ok

(

bbq_ready

,

'Barbecue ready!.'

);

ok(state, message)

(51)

equal(actual, expected, message)

Assert

Simple comparison operator (==) to compare the

actual and expected arguments.

var

bbq

= 'Bacon';

QUnit.

equal

(

bbq

,

'Bacon'

,

'Bacon barbecue!'

);

(52)

notEqual(actual, expected, message)

Assert

Simple inverted comparison operator (!=) to

compare the actual and expected arguments.

var

bbq

= 'Bacon';

QUnit.

notEqual

(

bbq

,

'Salad'

,

'No salad!'

);

var

bbq

= 'Salad';

(53)

deepEqual(actual, expected, message)

Assert

Just like equal() when comparing objects, such

that { key: value } is equal to { key: value }.

var

bbq

= {meat: 'Bacon'};

QUnit.

deepEqual

(

bbq

,

{meat: 'Bacon'}

,

'Bacon barbecue!'

);

var

bbq

= {meat: 'Chicken'};

QUnit.

deepEqual

(

bbq

,

(54)

notDeepEqual(actual, expected, message)

Assert

Just like notEqual() when comparing objects, such

that { key: value } is not equal to { key: value }.

var

bbq

= {food: 'Bacon'};

QUnit.

notDeepEqual

(

bbq

,

{food: 'Salad'}

,

'No salad!'

);

var

bbq

= {food: 'Salad'};

QUnit.

notDeepEqual

(

bbq

,

(55)

strictEqual(actual, expected, message)

Assert

Most rigid comparison of type and value with the

strict equality operator (===).

var bacon = '1';

QUnit.strictEqual(bacon, '1', 'Bacon!'); QUnit.strictEqual(bacon, 1, 'Bacon!');

(56)

notStrictEqual(actual, expected, message)

Assert

Most rigid comparison of type and value with the

strict inverted equality operator (!==).

var bacon = '1';

QUnit.notStrictEqual(bacon, 1, 'No Bacon!'); QUnit.notStrictEqual(bacon, '1', 'No Bacon!');

(57)
(58)

expect(amount)

Expect

Specify how many assertions are expected to run

within a test. If the number of assertions run does

not match the expected count, the test will fail.

var bbq = 'Bacon'; // Good

QUnit.expect(1);

QUnit.equal(bbq, 'Bacon', 'Bacon barbecue!');

// Wrong

QUnit.expect(1);

QUnit.equal(bbq, 'Bacon', 'Bacon barbecue!');

(59)
(60)

Synchronous Testing

// Number of assertions. QUnit.expect(3);

var bbq_ready = true, bbq = 'Bacon';

// Assertions.

QUnit.ok(bbq_ready, 'Barbacue is ready!');

QUnit.equal(bbq, 'Bacon', 'Bacon barbecue!'); QUnit.notEqual(bbq, 'Salad', 'No salad!');

(61)
(62)

Asynchronous Testing

QUnit.expect(2);

var bbq_ready = false, bbq = 'Bacon',

time = 36000; // Miliseconds.

QUnit.stop();

setTimeout(function() { bbq_ready = true;

QUnit.ok(bbq_ready, 'Barbacue is ready!'); QUnit.start();

}, time);

(63)
(64)

Testing User Actions

/** * Implements hook_testswarm_tests(). */ function bacon_testswarm_tests() { 'bacon_test' => array( 'module' => 'bacon',

'description' => 'Testing bacon.', 'js' => array(

$path . '/tests/bacon.tests.js' => array(), ),

'dependencies' => array(

array('testswarm', 'jquery.simulate'), ),

'path' => 'admin/bacon/test',

'permissions' => array('test bacon') ),

(65)

Testing User Actions

QUnit.expect(1);

var bbq_ready = false, bbq = 'Bacon';

bbq_ready.trigger('change');

QUnit.ok(bbq_ready, 'Barbecue ready!');

(66)
(67)

Questions

?

@rteijeiro TestSwarm FAT https://github.com/jquery/jquery-simulate

References

Related documents