{"id":1238,"date":"2021-03-04T08:47:55","date_gmt":"2021-03-04T07:47:55","guid":{"rendered":"http:\/\/mateuszglowinski.pl\/?p=1238"},"modified":"2021-03-04T11:27:14","modified_gmt":"2021-03-04T10:27:14","slug":"how-to-write-good-unit-tests","status":"publish","type":"post","link":"https:\/\/mateuszglowinski.pl\/index.php\/2021\/03\/04\/how-to-write-good-unit-tests\/","title":{"rendered":"How to write good unit tests?"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"1238\" class=\"elementor elementor-1238\" data-elementor-settings=\"[]\">\n\t\t\t\t\t\t<div class=\"elementor-inner\">\n\t\t\t\t\t\t\t<div class=\"elementor-section-wrap\">\n\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-6d0d002 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"6d0d002\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t\t\t<div class=\"elementor-row\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-bfff0a9\" data-id=\"bfff0a9\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-column-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t<div class=\"elementor-widget-wrap\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-c217ac5 elementor-widget elementor-widget-text-editor\" data-id=\"c217ac5\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\"><h4>What is unit testing?<\/h4><p>Unit testing is a type of software testing where individual units or components of a software are tested. The purpose is to validate that each unit of the software code performs as expected.<\/p><h4>Why to use unit tests?<\/h4><p>The most important advantage, which I can think of is that unit tests are making our work faster and less buggy. <span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">Imagine that<\/span><\/span> you have done some code-refactoring. It is enough to run unit tests to have <span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">approximate<\/span><\/span> feedback <span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">about the success of our changes. <\/span><\/span>In case of new future, the unit tests can help with finding bugs early.<\/p><h4>How to write <span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">high-quality<\/span><\/span> unit tests?<\/h4><p>To wirte <span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">high-quality<\/span><\/span> unit tests it is recommended to follow <strong>FIRST<\/strong> rules, which are:<\/p><p style=\"padding-left: 80px;\"><strong>F <\/strong>(fast) &#8211; unit tests should be fast, not to make developers tired of executing them.<\/p><p style=\"padding-left: 80px;\"><strong>I <\/strong>(isolated) &#8211; unit test should test only small and single piece of code.<\/p><p style=\"padding-left: 80px;\"><strong>R <\/strong>(repeatable) &#8211; unit test result should always be the same and not related to other tests or outside world.<\/p><p style=\"padding-left: 80px;\"><strong>S <\/strong>(self-validating) &#8211; unit tests should be started automatically.<\/p><p style=\"padding-left: 80px;\"><strong>T <\/strong>(timely) &#8211; unit test should be added\/updated just before\/after code adding\/refactoring.<\/p><h4>What scope does &#8222;unit&#8221; exactly mean?<\/h4><p>A unit may be an individual function, method, procedure, module or object. This kind of test should never be confused with integration test, which are responsible for testing how different pieces of the system work together.<\/p><h4><span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">What to look for when writing unit tests?<\/span><\/span><\/h4><p>Unit test should cover <a href=\"https:\/\/en.wikipedia.org\/wiki\/Happy_path\">happy path<\/a>, exception cases and also boundary conditions.<\/p><p>Boundary conditions might be hard to notice and due to that easy to omit. High-quality unit tests are covering all scenarios, following <strong>CORRECT<\/strong> rules might help with that.<\/p><p style=\"padding-left: 80px;\"><strong>C<\/strong> (conformance) &#8211; unit test ensures that a tested software meets a defined set of standards e.g. before saving e-mail it should be valdiated.<\/p><p style=\"padding-left: 80px;\"><strong>O <\/strong>(ordering) &#8211; unit test should check behaviour of unit with different set of data e.g. with ordered and unordered data.<\/p><p style=\"padding-left: 80px;\"><strong>R<\/strong> (range) &#8211; <span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">unit test should check what will happen when passing minimum\/maximum values and also invalid value to unit e.g. passing -1 as a person age.<\/span><\/span><\/p><p style=\"padding-left: 80px;\"><strong>R<\/strong> (reference) &#8211; all prerequisites before starting a test are met e.g. log history should be shown only for logged user.<\/p><p style=\"padding-left: 80px;\"><strong>E<\/strong> (existance) &#8211; unit test should check what will happen when passing NULL or 0 value to unit.<\/p><p style=\"padding-left: 80px;\"><strong>C<\/strong> (cardinality) &#8211; unit test should check size of unit result e.g. after cancelling a shoping cart, a cart should be empty.<\/p><p style=\"padding-left: 80px;\"><strong>T<\/strong> (time) &#8211; unit tests should check if everything is executed in order e.g. restoring a backup before creating it. This point is also really important in multithreading programming, where race-condition may take place.<\/p><h4>What is required code coverage?<\/h4><p>Code coverage\u00a0<span class=\"ILfuVd\"><span class=\"hgKElc\"> is a software testing metric that determines the number of lines of code<b> <\/b>that is successfully validated under a test procedure, which in turn, helps in analyzing how comprehensively a software is verified.<\/span><\/span><\/p><p><strong>Required test coverage is 80%.<\/strong> This number ensures that our code was <span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">sufficiently covered by the unit tests.<\/span><\/span> <span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">Some may ask why not 100%. Well, generally there are some code-parts which do not require testing e.g. getters and setters.<\/span><\/span><\/p><h4><span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">What must not be forgotten?<\/span><\/span><\/h4><p>Always remember to update unit test related to code change. Unit test which is out-of-date can be misleading for other developers or even for you in near future.<\/p><\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>What is unit testing? Unit testing is a type of software testing where individual units or components of a software are tested. The purpose is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29],"tags":[32],"_links":{"self":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1238"}],"collection":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/comments?post=1238"}],"version-history":[{"count":73,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1238\/revisions"}],"predecessor-version":[{"id":1318,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1238\/revisions\/1318"}],"wp:attachment":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/categories?post=1238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/tags?post=1238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}