AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to...

24
AUTOMATED TESTING HOW TO KNOW YOUR CODE WORKS Created by / Chris Foster @chrisfosterelli

Transcript of AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to...

Page 1: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

AUTOMATED TESTINGHOW TO KNOW YOUR CODE WORKS

Created by / Chris Foster @chrisfosterelli

Page 2: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

WHAT IS TESTING?

Page 3: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

AUTOMATED VS. MANUALManual testing: a human tests the codeAutomated testing: code tests the code

Page 4: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

AUTOMATED TESTING1. Unit testing2. End-to-end

We're going to learn about Unit testing!

Page 5: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

WHY UNIT TEST?Reliable codeModular codeMaintainable codeError resistant code

Page 6: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

UNIT TESTING

In unit testing the smallest testable parts of an application,called units, are individually and independently scrutinized

for proper operation.

Page 7: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

JS TESTINGMochaChaiJasmineSinonQunitexpect.jsand more...

Better-assertUnexpectedJestJSUnitUnitJSshould.jsand more...

Page 8: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

EXAMPLE UNITfunction getGreeting(name, greeting) { if (!greeting) greeting = 'Hello '; if (name) return greeting + name + '!'; return greeting + '!';}

What do we need to test for?

Page 9: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

TEST CASES1. Given no name or greeting -> returns 'Hello!'2. Given only a name 'John' -> returns 'Hello John!'3. Given 'John' and 'Good day ' -> returns 'Good day John!'

Page 10: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

TEST SUITEdescribe('getGreeting(name, greeting)', function() { it('should return a default greeting', function() { expect(getGreeting()).toEqual('Hello!'); });

it('should return a greeting for a name', function() { expect(getGreeting('Sam')).toEqual('Hello Sam!'); });

it('should return a custom greeting for a name', function() { expect(getGreeting('Sam', 'Good Day')).toEqual('Good Day Sam!'); });

});

Page 11: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

SUCCESS!

Page 12: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

LET'S WRITE SOME TESTS!http://bit.ly/1I24eLn

Page 13: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

PICKING UNITSJava: classNode: moduleBrowser: file

Page 14: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

TESTING WORKFLOWCode is only accepted if tests writtenCode is only accepted if tests passGreen builds and Red builds

Page 15: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

TEST DRIVEN DEVELOPMENT

Write tests before codeEmphasis on minimal codeEmphasis on code coverage

Page 16: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

SPIES, STUBS, AND MOCKSA mock is a fake object for testing uponA stub is a function with pre-programmed behaviourA spy is a 'wrapper' around another function

Page 17: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

FIXTURESRefreshed during each testFake test data loaded into a databaseAllows your tests to remain reliable

Page 18: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

FAKING ITSystem clockHTTP requestsEntire modulesDatabase values

Page 19: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

DEALING WITH ASYNCfunction setAndWait(cb) { setTimeout(function() { window.global = true; cb(); }, 5000);}

it('should call the timeout', function(done) { window.global = false; setAndWait(function() { expect(window.global).toEqual(true); done(); });});

Page 20: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

SETUPdescribe('My Suite', function() { var database, foo;

before(function() { foo = 'static value'; });

beforeEach(function() { database = new Database(); });

/* [... tests ...] */

});

Page 21: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

TEARDOWNdescribe('My Suite', function() { var database, foo;

/* [... tests ...] */

afterEach(function() { database.close(); });

after(function() { console.log('All tests done!'); });

});

Page 22: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

NESTING DESCRIBEdescribe('getConnection()', function() {

describe('when the server is down', function() { it('should return an error', /*...*/); it('should close the connection', /*...*/); });

describe('when the server is up', function() { it('should return the connection', /*...*/); }); });

Page 23: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

MORE ADVANCED TESTS!http://bit.ly/1I2AkGI

Page 24: AUTOMATED TESTING · 2020-05-25 · AUTOMATED TESTING 1. Unit testing 2. End-to-end We're going to learn about Unit testing!

THE END- -

@chrisfosterellihttps://fosterelli.co