{"id":16009,"date":"2024-02-13T15:40:07","date_gmt":"2024-02-13T14:40:07","guid":{"rendered":"https:\/\/mmmake.com\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/"},"modified":"2024-02-23T13:04:23","modified_gmt":"2024-02-23T12:04:23","slug":"mastering-unit-testing-in-abap-a-guide-to-software-quality","status":"publish","type":"post","link":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/","title":{"rendered":"Mastering Unit Testing in ABAP: a guide to software quality"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Unit testing is an indispensable tool for any software developer. It enables the verification of the functional correctness of software modules at the smallest unit level, such as procedures, classes or function groups. In the ABAP world, clean code is tested code. Our SAP HR Solution Developer Lev Lourie shows you the best way to proceed in his guest article.<\/p>\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n<h2 class=\"wp-block-heading\">What is a unit test?<\/h2>\n\n<p class=\"wp-block-paragraph\">A unit test is a mechanism that provides concepts and tools for testing. It is carried out using test classes, which are usually local classes within productive global classes. A test class is defined with the addition &#8220;FOR TESTING&#8221;. Test methods check the correctness of the tested code using assertions.<\/p>\n\n<pre class=\"wp-block-code\"><code>CLASS &#91;EXAMPLE_TEST]  DEFINITION\n  FINAL\n  FOR TESTING\n  DURATION SHORT\n  RISK LEVEL HARMLESS.\n\nPRIVATE SECTION.\n METHODS:\n    &#91;METHOD1_TEST]  FOR TESTING.\n ENDCLASS.\n\n CLASS &#91;EXAMPLE_TEST] IMPLEMENTATION.\n METHOD METHOD1_TEST  .\n     \u2026 \n    CL_ABAP_UNIT_ASSERT=&gt;ASSERT_EQUALS(\n      EXP = \u2026\n      ACT = \u2026\n      MSG = 'UNEXPECTED TEST VALUE\u2019).\n ENDMETHOD.\n ENDCLASS.\n<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n<p class=\"wp-block-paragraph\">Additional attributes in the class definition are:<\/p>\n\n<ul class=\"wp-block-list\">\n<li>&#8220;RISK LEVEL&#8221;, which indicates the risk from harmless to critical<\/li>\n\n\n\n<li>and &#8220;DURATION&#8221;, which defines the maximum permissible execution time for the tests within a test class in the ABAP unit before the program terminates the process.<\/li>\n<\/ul>\n\n<h2 class=\"wp-block-heading\">Common functionality in unit tests<\/h2>\n\n<p class=\"wp-block-paragraph\">If several unit tests require the same functionality, they can have a common superclass that has test methods in the subclasses. If no test methods are to be inherited, the common functionality is provided in a test helper class with the addition &#8220;ABSTRACT&#8221;.<\/p>\n\n<h2 class=\"wp-block-heading\">The &#8220;given-if-then&#8221; style<\/h2>\n\n<p class=\"wp-block-paragraph\">The implementation of the test method is constructed as a scenario in the &#8220;given-if-then&#8221; style. In the &#8220;given&#8221; part, the instances and data are prepared for the test. This phase is implemented in the Setup method. The operation to be tested is executed in the &#8220;if&#8221; part. The &#8220;then&#8221; part ensures that the desired result is achieved. Assertions are used in this step and if the result is not achieved, an error message should be displayed.<\/p>\n\n<pre class=\"wp-block-code\"><code>METHOD REJECTS_INVALID_COUNTRY.\n\" WHEN\u2026\n\" THEN\u201d \n  CL_ABAP_UNIT_ASSERT=&gt;ASSERT_TABLE_CONTAINS(\n    TABLE = MESSAGES\n    LINE = \u2026\n    MSG = `EXPECTED ERROR MESSAGE NOT FOUND` ).\n ENDMETHOD.\n\n<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\">TEST-SEAMS und TEST-INJECTION<\/h2>\n\n<p class=\"wp-block-paragraph\">Another test tool is &#8220;TEST-SEAMS&#8221;, which marks the code parts that are replaced by other code during a test. A TEST-INJECTION block is called up in the test code for this purpose. The code in the injection block replaces the code in the test seam.<\/p>\n\n<h2 class=\"wp-block-heading\">First steps with unit tests in ABAP<\/h2>\n\n<p class=\"wp-block-paragraph\">Start by selecting the class you want to test. In the menu, navigate to Utilities -&gt; Test classes -&gt; Generate and follow the wizard to create the test class. Once this is done, you can start writing your first test for a method in your class. Call the method in your test and use the ASSERT_EQUALS method to compare the result obtained with the expected result. Finally, execute the test by clicking on the unit test icon in the toolbar.<\/p>\n\n<h2 class=\"wp-block-heading\">Tested code is clean code<\/h2>\n\n<p class=\"wp-block-paragraph\">Unit testing in ABAP is a powerful tool that significantly improves the quality of software development. It allows developers to check the functionality of their code at the smallest unit level and ensure that their code works correctly. With the use of test classes, assertions, the &#8220;given-if-then&#8221; style, test seeds and test injections, ABAP offers a comprehensive and flexible test environment. By applying these practices, ABAP developers can ensure that their code is clean, maintainable, efficient and error-free, bringing them closer to the goal of clean code.<\/p>\n\n<p class=\"wp-block-paragraph\">Unit testing is therefore an important instrument for quality assurance and at the same time considerably shortens the development time.<\/p>\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unit testing &#8211; an important tool to ensure that your software modules work. We will show you how to proceed.<\/p>\n","protected":false},"author":38,"featured_media":15912,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[52],"class_list":["post-16009","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-sap-en"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Mastering Unit Testing in ABAP: a guide to software quality - mmmake.com<\/title>\n<meta name=\"description\" content=\"Unit testing - an important tool to ensure that your software modules work. We will show you how to proceed.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mastering Unit Testing in ABAP: a guide to software quality - mmmake.com\" \/>\n<meta property=\"og:description\" content=\"Unit testing - an important tool to ensure that your software modules work. We will show you how to proceed.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/\" \/>\n<meta property=\"og:site_name\" content=\"mmmake.com\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-13T14:40:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-23T12:04:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/mmmake.com\/wp-content\/uploads\/2024\/02\/header_abap.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1000\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Lev Lourie\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lev Lourie\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/\"},\"author\":{\"name\":\"Lev Lourie\",\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/#\\\/schema\\\/person\\\/c3491e431067cf396bf47a98774e1f2e\"},\"headline\":\"Mastering Unit Testing in ABAP: a guide to software quality\",\"datePublished\":\"2024-02-13T14:40:07+00:00\",\"dateModified\":\"2024-02-23T12:04:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/\"},\"wordCount\":545,\"publisher\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/mmmake.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/header_abap.webp\",\"keywords\":[\"SAP\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/\",\"url\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/\",\"name\":\"Mastering Unit Testing in ABAP: a guide to software quality - mmmake.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/mmmake.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/header_abap.webp\",\"datePublished\":\"2024-02-13T14:40:07+00:00\",\"dateModified\":\"2024-02-23T12:04:23+00:00\",\"description\":\"Unit testing - an important tool to ensure that your software modules work. We will show you how to proceed.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/#primaryimage\",\"url\":\"https:\\\/\\\/mmmake.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/header_abap.webp\",\"contentUrl\":\"https:\\\/\\\/mmmake.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/header_abap.webp\",\"width\":1920,\"height\":1000},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/blog\\\/mastering-unit-testing-in-abap-a-guide-to-software-quality\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mmmake.com\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mastering Unit Testing in ABAP: a guide to software quality\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/mmmake.com\\\/en\\\/\",\"name\":\"mmmake.com\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/mmmake.com\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/#organization\",\"name\":\"mmmake.com\",\"url\":\"https:\\\/\\\/mmmake.com\\\/en\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/mmmake.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/mmmake-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/mmmake.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/mmmake-logo.jpg\",\"width\":538,\"height\":290,\"caption\":\"mmmake.com\"},\"image\":{\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mmmake.com\\\/en\\\/#\\\/schema\\\/person\\\/c3491e431067cf396bf47a98774e1f2e\",\"name\":\"Lev Lourie\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mastering Unit Testing in ABAP: a guide to software quality - mmmake.com","description":"Unit testing - an important tool to ensure that your software modules work. We will show you how to proceed.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/","og_locale":"en_US","og_type":"article","og_title":"Mastering Unit Testing in ABAP: a guide to software quality - mmmake.com","og_description":"Unit testing - an important tool to ensure that your software modules work. We will show you how to proceed.","og_url":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/","og_site_name":"mmmake.com","article_published_time":"2024-02-13T14:40:07+00:00","article_modified_time":"2024-02-23T12:04:23+00:00","og_image":[{"width":1920,"height":1000,"url":"https:\/\/mmmake.com\/wp-content\/uploads\/2024\/02\/header_abap.webp","type":"image\/webp"}],"author":"Lev Lourie","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Lev Lourie","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/#article","isPartOf":{"@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/"},"author":{"name":"Lev Lourie","@id":"https:\/\/mmmake.com\/en\/#\/schema\/person\/c3491e431067cf396bf47a98774e1f2e"},"headline":"Mastering Unit Testing in ABAP: a guide to software quality","datePublished":"2024-02-13T14:40:07+00:00","dateModified":"2024-02-23T12:04:23+00:00","mainEntityOfPage":{"@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/"},"wordCount":545,"publisher":{"@id":"https:\/\/mmmake.com\/en\/#organization"},"image":{"@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/#primaryimage"},"thumbnailUrl":"https:\/\/mmmake.com\/wp-content\/uploads\/2024\/02\/header_abap.webp","keywords":["SAP"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/","url":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/","name":"Mastering Unit Testing in ABAP: a guide to software quality - mmmake.com","isPartOf":{"@id":"https:\/\/mmmake.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/#primaryimage"},"image":{"@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/#primaryimage"},"thumbnailUrl":"https:\/\/mmmake.com\/wp-content\/uploads\/2024\/02\/header_abap.webp","datePublished":"2024-02-13T14:40:07+00:00","dateModified":"2024-02-23T12:04:23+00:00","description":"Unit testing - an important tool to ensure that your software modules work. We will show you how to proceed.","breadcrumb":{"@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/#primaryimage","url":"https:\/\/mmmake.com\/wp-content\/uploads\/2024\/02\/header_abap.webp","contentUrl":"https:\/\/mmmake.com\/wp-content\/uploads\/2024\/02\/header_abap.webp","width":1920,"height":1000},{"@type":"BreadcrumbList","@id":"https:\/\/mmmake.com\/en\/blog\/mastering-unit-testing-in-abap-a-guide-to-software-quality\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mmmake.com\/en\/"},{"@type":"ListItem","position":2,"name":"Mastering Unit Testing in ABAP: a guide to software quality"}]},{"@type":"WebSite","@id":"https:\/\/mmmake.com\/en\/#website","url":"https:\/\/mmmake.com\/en\/","name":"mmmake.com","description":"","publisher":{"@id":"https:\/\/mmmake.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mmmake.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/mmmake.com\/en\/#organization","name":"mmmake.com","url":"https:\/\/mmmake.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/mmmake.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/mmmake.com\/wp-content\/uploads\/2023\/08\/mmmake-logo.jpg","contentUrl":"https:\/\/mmmake.com\/wp-content\/uploads\/2023\/08\/mmmake-logo.jpg","width":538,"height":290,"caption":"mmmake.com"},"image":{"@id":"https:\/\/mmmake.com\/en\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/mmmake.com\/en\/#\/schema\/person\/c3491e431067cf396bf47a98774e1f2e","name":"Lev Lourie"}]}},"_links":{"self":[{"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/posts\/16009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/comments?post=16009"}],"version-history":[{"count":4,"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/posts\/16009\/revisions"}],"predecessor-version":[{"id":16096,"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/posts\/16009\/revisions\/16096"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/media\/15912"}],"wp:attachment":[{"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/media?parent=16009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/categories?post=16009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mmmake.com\/en\/wp-json\/wp\/v2\/tags?post=16009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}