Unit test in a nutshell

Post on 14-Jul-2015

98 views 0 download

Tags:

transcript

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Unit Test in a nutshellShort presentation of a method to verify isolated units of code

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

About me

Agile Coach eXtreme Programming Trainer !Certified Scrum Professional >30 years of coding (and still proudly counting) !roberto.bettazzoni@agile42.com linkedin.com/in/robertobettazzoni @bettazzoni

Roberto Bettazzoni

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test

Method to verify isolated units of code

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test

user

code

Acceptance Criteria test

Functional test

Unit test

Unit tests are written by coders for coders

to test the code design

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test

Method to verify isolated units of code

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test

Method to verify isolated units of code

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test

assert( condition )

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test library

assert( bool condition ) assertEqual( <T> a, <T> b ) assertEqual( float a, float b, float precision) assertThrow( exceptionType, function)

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test

Method to verify isolated units of code

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Test Isolation

B

D

C

A

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Test Isolation

Run 4 tests: A, B, C, D

B

D

C

A

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Test Isolation

B

D

C

A

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Test Isolation

Run 3 tests: A, B, D

B

D

A

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Test Isolation

Run 3 tests: D, A, B

B

D

A

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test library

test()

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test library

setup() - test() - teardown()

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test library

{ setup() {} teardown() {} !

test_method_1() test_method_2() test_method_3() }

Test Class also known as TestCase or TestFixture

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test library

setup() - test_method_1() - teardown()

Test Class also known as TestCase or TestFixture

setup() - test_method_2() - teardown()setup() - test_method_3() - teardown()

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test

Method to verify isolated units of code

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

unit test - What is a unit of code?

Classes Methods Functions

Blocks of code

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Code Isolation (or Unit Isolation)

B

D

C

A

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Code Isolation (or Unit Isolation)

B

D

C

A

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Take a guess

What makes a “good unit test”?

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Take a guess

What makes a “good unit test”?

Readability

agile42 | We advise, train and coach companies building software | www.agile42.com | Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Food for thoughts