Вы находитесь на странице: 1из 50

Week 1: Introduction to ABAP Unit Testing

Unit 1: Overview and Motivation


Overview and Motivation
Course overview

▪ Motivation
▪ ABAP unit
▪ Test-driven development
▪ Pair programming
▪ Test isolation
▪ Legacy code
▪ Continuous integration
Write Write
testable efficient
code and
effective
automated
tests

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2


Overview and Motivation
Motivation

“There are no tricks to writing tests, there are only


tricks to writing testable code.”
Miško Hevery – Agile Coach at Google [1]

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 3


Overview and Motivation
Integrated/system tests (1)

Advantages Integrated /
▪ Tests run in real environment System Test

Disadvantages
▪ Can be built only at end of development A B
▪ Complex setup of environment
▪ Long running tests
▪ High effort to analyze errors CATDD D X

▪ Fragile
▪ High maintenance
E F

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 4


Overview and Motivation
Integrated/system tests (2)

Using only integrated/system tests at the end of development Integrated /


is like putting together an airplane without testing the parts… System Test

…and then conducting the first test flight!


A B

CATDD D X

E F

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 5


Overview and Motivation
Unit tests

Advantages
▪ Can be built during development
▪ Isolated from the environment
▪ Fast
A B
▪ Error analysis easy
▪ Stable Unit Test
▪ Low maintenance D
CATDD X

Disadvantage
▪ Tests run in simulated environment
E F

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 6


Overview and Motivation
Tests need testable code

Decoupling
▪ Only decoupled product code enables you to write
your tests isolated from the environment

Refactoring
▪ “Refactoring is the process of changing a software
system in such a way that it does not alter the
external behavior of the code yet improves its
internal structure.”
Write Write
Martin Fowler [2]
testable efficient
Clean Code code and
effective
▪ “Clean code always looks like it was written by automated
someone who cares.” tests
Robert C. Martin [3]

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 7


Overview and Motivation
Importance of tests

“If the tests are good, then the product code is


probably good enough.
If tests are bad or do not exist, then all hope is
lost.”
Gerard Meszaros – @SAP 2012
Author of “xUnit Test Patterns”

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 8


Overview and Motivation
Agile test practices in combination

manual

Test costs and runtime


automated
Effort and test coverage
© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 9
Overview and Motivation
Bring the fun back to development

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 10


Thank you.
Contact information:

open@sap.com
© 2017 SAP SE or an SAP affiliate company. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components
of other software vendors. National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated
companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are
set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release
any functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products,
and/or platform directions and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The
information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various
risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements,
and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company)
in Germany and other countries. All other product and service names mentioned are the trademarks of their respective companies.
See http://global.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and notices.
Week 1: Introduction to ABAP Unit Testing
Unit 2: Introduction to ABAP Unit
Introduction to ABAP Unit
Money machine

During this training, we’ll develop a money machine


that will provide different services, like…
▪ Provide change for a given amount using the least
possible number of notes and coins
▪ Get the amount returned in coins
▪ Get the amount returned in notes

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2


Introduction to ABAP Unit
Money machine – Examples

Input amount 1 EUR

Output 1 EUR coin

Input amount 7 EUR

Output 5 EUR note and 2 EUR coin

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 3


Introduction to ABAP Unit
Product code

CLASS cl_money_machine DEFINITION


PUBLIC
FINAL
CREATE PUBLIC.

PUBLIC SECTION.
METHODS get_amount_in_coins
IMPORTING i_amount TYPE i
RETURNING VALUE(r_value) TYPE i.

PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

CLASS cl_money_machine IMPLEMENTATION.

METHOD get_amount_in_coins.
r_value = COND #( WHEN i_amount <= 0
THEN -1
ELSE i_amount MOD 5 ).
ENDMETHOD.

ENDCLASS.

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 4


Introduction to ABAP Unit
Add a test class

Product code Test include


CLASS cl_money_machine DEFINITION CLASS ltc_get_amount_in_coins DEFINITION FOR TESTING
PUBLIC RISK LEVEL HARMLESS
FINAL DURATION SHORT.
CREATE PUBLIC.
ENDCLASS.
PUBLIC SECTION.
METHODS get_amount_in_coins
IMPORTING i_amount TYPE i
CLASS ltc_get_amount_in_coins IMPLEMENTATION.
RETURNING VALUE(r_value) TYPE i.

PROTECTED SECTION. ENDCLASS.


PRIVATE SECTION.
ENDCLASS.

CLASS cl_money_machine IMPLEMENTATION.

METHOD get_amount_in_coins.
r_value = COND #( WHEN i_amount <= 0
THEN -1
ELSE i_amount MOD 5 ).
ENDMETHOD.

ENDCLASS.

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 5


Introduction to ABAP Unit
Add a test method

Product code Test include


CLASS cl_money_machine DEFINITION CLASS ltc_get_amount_in_coins DEFINITION FOR TESTING
PUBLIC RISK LEVEL HARMLESS
FINAL DURATION SHORT.
CREATE PUBLIC.
PRIVATE SECTION.
PUBLIC SECTION. METHODS amount_1_coin_1 FOR TESTING.
METHODS get_amount_in_coins
IMPORTING i_amount TYPE i ENDCLASS.
RETURNING VALUE(r_value) TYPE i.

PROTECTED SECTION. CLASS ltc_get_amount_in_coins IMPLEMENTATION.


PRIVATE SECTION.
ENDCLASS. METHOD amount_1_coin_1.

ENDMETHOD.
CLASS cl_money_machine IMPLEMENTATION.
ENDCLASS.
METHOD get_amount_in_coins.
r_value = COND #( WHEN i_amount <= 0
THEN -1
ELSE i_amount MOD 5 ).
ENDMETHOD.

ENDCLASS.

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 6


Introduction to ABAP Unit
Test method story

Product code Test include


CLASS cl_money_machine DEFINITION CLASS ltc_get_amount_in_coins DEFINITION FOR TESTING
PUBLIC RISK LEVEL HARMLESS
FINAL DURATION SHORT.
CREATE PUBLIC.
PRIVATE SECTION.
PUBLIC SECTION. "! Amount of 1 EUR results in 1 EUR coin
METHODS get_amount_in_coins METHODS amount_1_coin_1 FOR TESTING.
IMPORTING i_amount TYPE i
RETURNING VALUE(r_value) TYPE i. ENDCLASS.

PROTECTED SECTION.
PRIVATE SECTION. CLASS ltc_get_amount_in_coins IMPLEMENTATION.
ENDCLASS.
METHOD amount_1_coin_1.
"given
CLASS cl_money_machine IMPLEMENTATION.
"when
METHOD get_amount_in_coins.
r_value = COND #( WHEN i_amount <= 0 "then
THEN -1
ELSE i_amount MOD 5 ). ENDMETHOD.
ENDMETHOD.
ENDCLASS.
ENDCLASS.

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 7


Introduction to ABAP Unit
Test method content

Product code Test include


CLASS cl_money_machine DEFINITION CLASS ltc_get_amount_in_coins DEFINITION FOR TESTING
PUBLIC RISK LEVEL HARMLESS
FINAL DURATION SHORT.
CREATE PUBLIC.
PRIVATE SECTION.
PUBLIC SECTION. "! Amount of 1 EUR results in 1 EUR coin
METHODS get_amount_in_coins METHODS amount_1_coin_1 FOR TESTING.
IMPORTING i_amount TYPE i
RETURNING VALUE(r_value) TYPE i. ENDCLASS.

PROTECTED SECTION.
PRIVATE SECTION. CLASS ltc_get_amount_in_coins IMPLEMENTATION.
ENDCLASS.
METHOD amount_1_coin_1.
"given
CLASS cl_money_machine IMPLEMENTATION. DATA(cut) = NEW cl_money_machine( ).

METHOD get_amount_in_coins. "when


r_value = COND #( WHEN i_amount <= 0 DATA(coin_amount) = cut->get_amount_in_coins( 1 ).
THEN -1
ELSE i_amount MOD 5 ). "then
ENDMETHOD. cl_abap_unit_assert=>assert_equals( act = coin_amount
exp = 1 ).
ENDCLASS. ENDMETHOD.

ENDCLASS.
© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 8
Introduction to ABAP Unit
Second test method

Test include Test include


CLASS ltc_get_amount_in_coins DEFINITION FOR TESTING CLASS ltc_get_amount_in_coins IMPLEMENTATION.
RISK LEVEL HARMLESS
DURATION SHORT. METHOD amount_1_coin_1.
"given
PRIVATE SECTION. DATA(cut) = NEW cl_money_machine( ).
"! Amount of 1 EUR results in 1 EUR coin
METHODS amount_1_coin_1 FOR TESTING. "when
"! Amount of 2 EUR results in 2 EUR coin DATA(coin_amount) = cut->get_amount_in_coins( 1 ).
METHODS amount_2_coin_2 FOR TESTING.
ENDCLASS. "then
cl_abap_unit_assert=>assert_equals( act = coin_amount
exp = 1 ).
ENDMETHOD.

METHOD amount_2_coin_2.
"given
DATA(cut) = NEW cl_money_machine( ).

"when
DATA(coin_amount) = cut->get_amount_in_coins( 2 ).

"then
cl_abap_unit_assert=>assert_equals( act = coin_amount
exp = 2 ).
ENDMETHOD.

ENDCLASS.
© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 9
Introduction to ABAP Unit
Unit test framework – Special methods

▪ CLASS_SETUP
static method, called once before the first SETUP of the test class
▪ SETUP
instance method, called before each test method
▪ TEARDOWN
instance method, called after each test method
▪ CLASS_TEARDOWN
static method, called once after the last TEARDOWN of the test class

Common properties
▪ optional – only define them if you need them
▪ private
▪ have no parameters

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 10


Introduction to ABAP Unit
Refactor test

Test include Test include


CLASS ltc_get_amount_in_coins DEFINITION FOR TESTING CLASS ltc_get_amount_in_coins IMPLEMENTATION.
RISK LEVEL HARMLESS
DURATION SHORT. METHOD amount_1_coin_1.
"given
PRIVATE SECTION. DATA(cut) = NEW cl_money_machine( ).
"! Amount of 1 EUR results in 1 EUR coin
METHODS amount_1_coin_1 FOR TESTING. "when
"! Amount of 2 EUR results in 2 EUR coin DATA(coin_amount) = cut->get_amount_in_coins( 1 ).
METHODS amount_2_coin_2 FOR TESTING.
ENDCLASS. "then
cl_abap_unit_assert=>assert_equals( act = coin_amount
exp = 1 ).
ENDMETHOD.

METHOD amount_2_coin_2.
"given
DATA(cut) = NEW cl_money_machine( ).

"when
DATA(coin_amount) = cut->get_amount_in_coins( 2 ).

"then
cl_abap_unit_assert=>assert_equals( act = coin_amount
exp = 2 ).
ENDMETHOD.

ENDCLASS.
© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 11
Introduction to ABAP Unit
Refactor test methods

Test include Test include


CLASS ltc_get_amount_in_coins DEFINITION FOR TESTING METHOD amount_1_coin_1.
RISK LEVEL HARMLESS "when
DURATION SHORT. DATA(coin_amount) = m_cut->get_amount_in_coins( 1 ).

PRIVATE SECTION. "then


DATA m_cut TYPE REF TO cl_money_machine. cl_abap_unit_assert=>assert_equals( act = coin_amount
exp = 1 ).
METHODS setup. ENDMETHOD.
"! Amount of 1 EUR results in 1 EUR coin
METHODS amount_1_coin_1 FOR TESTING. METHOD amount_2_coin_2.
"! Amount of 2 EUR results in 2 EUR coin "when
METHODS amount_2_coin_2 FOR TESTING. DATA(coin_amount) = m_cut->get_amount_in_coins( 2 ).
ENDCLASS.
"then
cl_abap_unit_assert=>assert_equals( act = coin_amount
CLASS ltc_get_amount_in_coins IMPLEMENTATION. exp = 2 ).
ENDMETHOD.
METHOD setup.
ENDCLASS.
"given
m_cut = NEW cl_money_machine( ).
ENDMETHOD.

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 12


Introduction to ABAP Unit
ABAP unit run time

▪ All test methods are executed independently of each other


▪ Execution is in undefined order
▪ One instance of the test class per test method
▪ One rollback at the end of each test class
© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 13
Introduction to ABAP Unit
The “then” part of the test story

Assert helper methods in class cl_abap_unit_assert


▪ assert_equals( .. )
▪ assert_bound( .. ) / assert_not_bound( .. )
▪ assert_initial( .. ) / assert_not_initial( .. )
▪ assert_true( .. ) / assert_false( .. )
▪ assert_subrc( .. )
▪ fail( .. )
▪ abort( .. )

They all pass messages to the ABAP unit runner

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 14


Thank you.
Contact information:

open@sap.com
© 2017 SAP SE or an SAP affiliate company. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components
of other software vendors. National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated
companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are
set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release
any functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products,
and/or platform directions and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The
information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various
risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements,
and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company)
in Germany and other countries. All other product and service names mentioned are the trademarks of their respective companies.
See http://global.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and notices.
Week 1: Introduction to ABAP Unit Testing
Unit 3: Demo of ABAP Unit Testing in Roman
Numerals
Demo of ABAP Unit Testing in Roman Numerals
Unit Test Exercise

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2


Demo of ABAP Unit Testing in Roman Numerals
Roman numerals converter – Overview

The converter takes a Roman number and returns the


corresponding Arabic number or -1 on invalid input.

Your task is to write unit tests for the converter and find the bug
hidden in the code with your tests – and then fix it.

Write tests for …


▪ I, II, III, IX, CC, D, MMVII, empty string, space
▪ VV, AA, IIII, VX

Digits: I = 1, II = 2, III = 3, IV = 4, V = 5, VI = 6, VII = 7, VIII =


8, IX = 9, X = 10, L = 50, C = 100, D = 500, M = 1000

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 3


Demo of ABAP Unit Testing in Roman Numerals
Roman numerals converter – Demo

Trainer Demo:
▪ Write first three tests
▪ Show ABAP Development Tools
 Perspectives
 Layout
 Shortcuts
• Quick fix (Ctrl-1)
• Execute unit test (Shift-Ctrl-F10)

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 4


Thank you.
Contact information:

open@sap.com
© 2017 SAP SE or an SAP affiliate company. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components
of other software vendors. National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated
companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are
set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release
any functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products,
and/or platform directions and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The
information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various
risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements,
and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company)
in Germany and other countries. All other product and service names mentioned are the trademarks of their respective companies.
See http://global.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and notices.
Week 1: Introduction to Unit Testing
Unit 4: Exercise – ABAP Unit Testing in Roman
Numerals
Exercise – ABAP Unit Testing in Roman Numerals
Where to find the code

Packages for this course Exercise


▪ All code relevant for this course can be found ▪ Your class for this exercise is
under the package ZCL_WTC_ROMAN_CONVERTER
ASE_WRITING_TESTABLE_CODE
▪ The code for this exercise is in the package
$WTC_EXERCISES

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2


Exercise – ABAP Unit Testing in Roman Numerals
Roman numerals converter – Exercise

The converter takes a Roman number and returns the


corresponding Arabic number or -1 on invalid input.

Your task is to write unit tests for the converter and find the bug
hidden in the code with your tests – and then fix it.

Write tests for …


▪ I, II, III, IX, CC, D, MMVII, empty string, space
▪ VV, AA, IIII, VX

Digits: I = 1, II = 2, III = 3, IV = 4, V = 5, VI = 6, VII = 7, VIII =


8, IX = 9, X = 10, L = 50, C = 100, D = 500, M = 1000

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 3


Thank you.
Contact information:

open@sap.com
© 2017 SAP SE or an SAP affiliate company. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components
of other software vendors. National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated
companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are
set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release
any functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products,
and/or platform directions and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The
information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various
risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements,
and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company)
in Germany and other countries. All other product and service names mentioned are the trademarks of their respective companies.
See http://global.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and notices.
Week 1: Introduction to ABAP Unit Testing
Unit 5: Review of Sample Solution
Review of Sample Solution
ABAP Unit Testing in Roman Numerals

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2


Thank you.
Contact information:

open@sap.com
© 2017 SAP SE or an SAP affiliate company. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components
of other software vendors. National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated
companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are
set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release
any functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products,
and/or platform directions and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The
information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various
risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements,
and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company)
in Germany and other countries. All other product and service names mentioned are the trademarks of their respective companies.
See http://global.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and notices.
Week 1: Introduction to Unit Testing
Unit 6: Unit Test Design, Tips, and Benefits
Unit Test Design, Tips, and Benefits
What cases to cover?

Consider for your test cases… For large numbers of independent but interacting
▪ Positive cases – correct values variables, consider Combinatorial Test Design
▪ Negative cases – incorrect values
▪ Boundary values and their proximity
▪ Combinations of input values

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2


Unit Test Design, Tips, and Benefits
Tips and tricks (1)

▪ Do not use test, verify etc. in test names


▪ Test class name shall express the “given” or the
“when” semantic
▪ Test method name shall represent the other parts
of the test story
▪ Always name the code under test CUT
▪ Group tests in a test class by same setup or same
code under test, e.g. method
▪ Test methods tell a story
 Short and crisp in less than one screen
 In sections of Given – When – Then

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 3


Unit Test Design, Tips, and Benefits
Tips and tricks (2)

▪ Test only the public interface, not private methods


▪ One test method tests one test aspect
▪ F.I.R.S.T. paradigm
 Fast – tests run quickly
 Independent – clearly isolated
 Repeatable – constant behavior
 Self-validating – pass or fail
 Timely – write tests with the product code
▪ Refactor tests at least as good as product code
Consider design principles
 KISS – keep it simple, stupid
 DRY – don’t repeat yourself

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 4


Unit Test Design, Tips, and Benefits
Benefits of unit tests

▪ Confidence ▪ Living documentation


 Tests create a safety net  Test as executable specification
 Encourage and enable changes  Tests document the API usage
 Tests codify the requirements
▪ Better code quality
 Consumable APIs ▪ Debugging
 Decoupling  Use tests to debug code
 Testability
 Less maintenance effort

© 2017 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 5


Thank you.
Contact information:

open@sap.com
© 2017 SAP SE or an SAP affiliate company. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components
of other software vendors. National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated
companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are
set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release
any functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products,
and/or platform directions and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The
information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various
risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements,
and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company)
in Germany and other countries. All other product and service names mentioned are the trademarks of their respective companies.
See http://global.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and notices.

Вам также может понравиться