abapcoderetreat frankfurt 2016 - tdd with abap

41
TDD Test Driven Development Hendrik Neumann

Upload: abapcoderetreat

Post on 11-Apr-2017

79 views

Category:

Software


0 download

TRANSCRIPT

Page 1: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

TDD – Test Driven Development

Hendrik Neumann

Page 2: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Agenda

(1) Unit Testing

(2) ABAP Unit

(3) Test Driven Development

Page 3: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Unit Testing

Page 4: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Unit Testing

Definition

Page 5: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Unit Testing

Unit Testing is a method by which

individual units of source code are

tested to determine if they are fit to

use.

Page 6: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Unit Testing

Unit Testing is a method by which

individual units are e.g.ource code

fittMethods of t Function Modules fit

Function Groups

fiMoModule Pools Function

Functn Reports

Methods

Function Groups

Page 7: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Unit Testing

aim:

Find bugs early

Write once, run often

Effective Tests must be automated

solution:

xUnit Frameworks

Page 8: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

xUnit Framework

Page 9: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

xUnit Framework

Definition

Page 10: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

xUnit Framework

Provides an automated solution with no need to write the same tests many times and no need to remember what should be

the result of each test.

Page 11: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

xUnit Framework

How does it work?

Page 12: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

xUnit Framework

setup()

creates object under test – used in all tests

Page 13: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

xUnit Framework

setup() test_method()

Clear up your mess..

calls object under test – e.g. a public method

with test data

Page 14: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

xUnit Framework

setup() test_method()

assert_*()–methods

verify the return value of the object under test using

xUnit’s assert methods

Page 15: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

xUnit Framework

setup() test_method() teardown()

Clear up your mess..

Page 16: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

xUnit Framework

setup() test_method() teardown() test_method()

test_method() test_method()

test_method() test_method()

test_method()

Page 17: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

ABAP Unit

Page 18: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

ABAP Unit

SAP‘s xUnit implemenation

Page 19: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

ABAP Unit

cl_abap_unit_assert=>assert_equals(

msg = 'Test msg'

act = act

exp = out->( )

).

Page 20: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

assert_*()-Methods

Page 21: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

ABAP Unit in ADT

Page 22: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

ABAP Unit

Local test classes for global development artifacts

In Reports, Function Modules and Classes

Not acitve on production systems

Page 23: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

ABAP Unit

• Tools integration:

–ABAP Workbench

–Code Inspector Integration

–ABAP Unit Browser

–ABAP Test Cockpit (ATC)

–ABAP in Eclipse

Page 24: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Driven Development

Page 25: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Driven Development

• Never

• After you wrote your code

• Before you write your code

When do you write your Unit Tests?

Page 26: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Driven Development

• Never

• After you wrote your code

• Before you write your code

When do you write your Unit Tests?

Test Driven Development

Page 27: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Driven Development

Test 1st

Page 28: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Cycle

Test Write a test that expresses how you’ll use

the code and what you need it to do.

Page 29: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Cycle

Write enough code to get the test to pass, but no

more.

Code

Test

Page 30: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Cycle

Code

Test

Refactor Clean up the code to remove redundancy and improve the design.

Page 31: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Cycle

Code

Test

Refactor

Page 32: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Cycle

Code Test

Refactor

Page 33: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Cycle

Code

Test Refactor

Page 34: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Test Cycle

Code

Test

Refactor

Page 35: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Green Bar Patterns

Page 36: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Green Bar Patterns

Fake it till you make it

• Start with hardcoded results and wait until tests force them to become real

Page 37: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Green Bar Patterns

Triangulate To Abstraction

• Make the code abstract only when you have two or more examples

Page 38: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Green Bar Patterns

Obvious Implementation

• aka Don‘t be stupid

• If you really know the right way to implement it, do it that way

Page 39: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Have FUN!

Page 40: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Backup

Page 41: ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP

Code Kata: Roman Numerals

Rules: • smaller number in front of a larger number subtraction • all else addition • put only one smaller number in front of a larger number • ones, tens, hundreds and thousands are separate items

I The numeral one. II is two, III is three.

V The numeral 5. IV is 4, VI is 6, VII is 7, VIII is 8.

X The numeral 10. IX is 9, XI is 11, etc.

L The numeral 50. XL would be 40.

C The numeral 100. C is short for the Latin word Centum, but Century is eaysier to remember ;-)

D The numeral 500.

M The numeral 1000.