{"id":8972,"date":"2021-05-24T08:30:00","date_gmt":"2021-05-24T06:30:00","guid":{"rendered":"https:\/\/rubikscode.net\/?p=8972"},"modified":"2021-05-25T15:04:06","modified_gmt":"2021-05-25T13:04:06","slug":"test-driven-development-tdd-with-python","status":"publish","type":"post","link":"https:\/\/rubikscode.net\/2021\/05\/24\/test-driven-development-tdd-with-python\/","title":{"rendered":"Test Driven Development (TDD) with Python"},"content":{"rendered":"<p>[et_pb_section fb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221; _builder_version=&#8221;3.22&#8243;][et_pb_row admin_label=&#8221;row&#8221; _builder_version=&#8221;3.25&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221;][et_pb_column type=&#8221;4_4&#8243; _builder_version=&#8221;3.25&#8243; custom_padding=&#8221;|||&#8221; custom_padding__hover=&#8221;|||&#8221;][et_pb_code _builder_version=&#8221;4.4.6&#8243;]<!-- Begin Mailchimp Signup Form --><!-- [et_pb_line_break_holder] --><link href=\"\/\/cdn-images.mailchimp.com\/embedcode\/classic-10_7.css\" rel=\"stylesheet\" type=\"text\/css\"><!-- [et_pb_line_break_holder] --><\/p>\n<style type=\"text\/css\"><!-- [et_pb_line_break_holder] -->\t#mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }<!-- [et_pb_line_break_holder] -->\t\/* Add your own Mailchimp form style overrides in your site stylesheet or in this style block.<!-- [et_pb_line_break_holder] -->\t   We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. *\/<!-- [et_pb_line_break_holder] --><\/style>\n<p><!-- [et_pb_line_break_holder] --><\/p>\n<div id=\"mc_embed_signup\"><!-- [et_pb_line_break_holder] --><\/p>\n<form action=\"https:\/\/rubikscode.us3.list-manage.com\/subscribe\/post?u=a175fa8b52c303e69c38f64b5&#038;id=f1f074045f\" method=\"post\" id=\"mc-embedded-subscribe-form\" name=\"mc-embedded-subscribe-form\" class=\"validate\" target=\"_blank\" novalidate><!-- [et_pb_line_break_holder] -->    <\/p>\n<div id=\"mc_embed_signup_scroll\"><!-- [et_pb_line_break_holder] -->\t<\/p>\n<h4>The code that accompanies this article can be received after subscription<\/h4>\n<p><!-- [et_pb_line_break_holder] -->      <input type=\"hidden\" name=\"OPTIN\" value=\"TDDPython\"><!-- [et_pb_line_break_holder] --><\/p>\n<div class=\"indicates-required\"><span class=\"asterisk\">*<\/span> indicates required<\/div>\n<p><!-- [et_pb_line_break_holder] --><\/p>\n<div class=\"mc-field-group\"><!-- [et_pb_line_break_holder] -->\t<label for=\"mce-EMAIL\">Email Address  <span class=\"asterisk\">*<\/span><!-- [et_pb_line_break_holder] --><\/label><!-- [et_pb_line_break_holder] -->\t<input type=\"email\" value=\"\" name=\"EMAIL\" class=\"required email\" id=\"mce-EMAIL\"><!-- [et_pb_line_break_holder] --><\/div>\n<p><!-- [et_pb_line_break_holder] -->\t<\/p>\n<div id=\"mce-responses\" class=\"clear\"><!-- [et_pb_line_break_holder] -->\t\t<\/p>\n<div class=\"response\" id=\"mce-error-response\" style=\"display:none\"><\/div>\n<p><!-- [et_pb_line_break_holder] -->\t\t<\/p>\n<div class=\"response\" id=\"mce-success-response\" style=\"display:none\"><\/div>\n<p><!-- [et_pb_line_break_holder] -->\t<\/div>\n<p>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--><!-- [et_pb_line_break_holder] -->    <\/p>\n<div style=\"position: absolute; left: -5000px;\" aria-hidden=\"true\"><input type=\"text\" name=\"b_a175fa8b52c303e69c38f64b5_f1f074045f\" tabindex=\"-1\" value=\"\"><\/div>\n<p><!-- [et_pb_line_break_holder] -->    <\/p>\n<div class=\"clear\"><input type=\"submit\" value=\"Subscribe\" name=\"subscribe\" id=\"mc-embedded-subscribe\" class=\"button\"><\/div>\n<p><!-- [et_pb_line_break_holder] -->    <\/div>\n<p><!-- [et_pb_line_break_holder] --><\/form>\n<p><!-- [et_pb_line_break_holder] --><\/div>\n<p><!-- [et_pb_line_break_holder] --><script type='text\/javascript' src='\/\/s3.amazonaws.com\/downloads.mailchimp.com\/js\/mc-validate.js'><\/script><script type='text\/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='OPTIN';ftypes[1]='text';}(jQuery));var $mcj = jQuery.noConflict(true);<\/script><!-- [et_pb_line_break_holder] --><!--End mc_embed_signup-->[\/et_pb_code][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Bugs! Every application has it and no matter how careful you are you will create one sooner or later. Especially if you are working on large enterprise software. Now, I know a bunch of people have a &#8220;My code is clean and bugless&#8221; attitude, but creating bug-free code is extremely hard if not impossible. However, that doesn&#8217;t mean that you should give up and just write spaghetti code that just doesn&#8217;t work. We &#8211; <strong>developers<\/strong>, should give our best to write <strong>high-quality code<\/strong>. High-quality means a low number of bugs, among other things.<\/p>\n<p style=\"text-align: justify;\"><!-- \/divi:paragraph --><\/p>\n<p style=\"text-align: justify;\"><!-- divi:paragraph -->Apart from that, having a bug in production is extremely expensive. You probably know that comparison, where a bug found during development is <strong>100 times cheaper<\/strong> than finding the same bug during production. So, we should focus on finding our bugs as soon as possible. Our first line of defense is <strong>testing<\/strong>. That is a scary metric.<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243; text_text_color=&#8221;#6b6b6b&#8221; border_width_bottom=&#8221;1px&#8221; border_color_bottom=&#8221;#6b6b6b&#8221;][\/et_pb_text][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/05\/comercial.png&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/05\/comercial.png&#038;#8221<\/a>; alt=&#8221;Ultimate Guide to Machine Learning with Python&#8221; title_text=&#8221;comercial&#8221; url=&#8221;<a href=\"https:\/\/rubikscode.net\/ultimate-guide-to-machine-learning-with-python\/&#038;#8221\">https:\/\/rubikscode.net\/ultimate-guide-to-machine-learning-with-python\/&#038;#8221<\/a>; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;427px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243; text_text_color=&#8221;#6b6b6b&#8221; border_width_bottom=&#8221;1px&#8221; border_color_bottom=&#8221;#6b6b6b&#8221;]<\/p>\n<p style=\"text-align: center;\"><span>This bundle of e-books is specially crafted for\u00a0<\/span><strong>beginners<\/strong><span>.<br \/> <\/span><span>Everything from Python basics to the deployment of Machine Learning algorithms to production in one place.<br \/> Become a Machine Learning Superhero\u00a0<\/span><a href=\"https:\/\/rubikscode.net\/ultimate-guide-to-machine-learning-with-python\/\"><strong>TODAY<\/strong><span>!<\/span><\/a><\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">We could test our applications manually, just by running them and clicking around. However, this approach has many <strong>pitfalls<\/strong>. The first one is that it is time-consuming, which basically means expensive. This way of testing makes regression testing extremely hard too.<\/p>\n<p style=\"text-align: justify;\">Imagine that you&#8217;ve just added a new feature to your application. You will have to make sure that this new feature didn&#8217;t break any of the old functionalities, which means testing your whole application from the beginning. Again, time-consuming and costly. What is the solution? <strong>Automated testing<\/strong> of course.<\/p>\n<p style=\"text-align: justify;\">In this article we cover:<\/p>\n<ol>\n<li style=\"text-align: justify;\"><a href=\"#at\"><strong>Automated Tests<\/strong><\/a><\/li>\n<li style=\"text-align: justify;\"><a href=\"#ut\"><strong>Unit Tests<\/strong><\/a><\/li>\n<li style=\"text-align: justify;\"><a href=\"#tdd\"><strong>What is Test-Driven Development?<\/strong><\/a><\/li>\n<li style=\"text-align: justify;\"><a href=\"#rick\"><strong>Solving a problem with TDD and Python<\/strong><\/a><\/li>\n<li style=\"text-align: justify;\"><a href=\"#mo\"><strong>Mock Objects<\/strong><\/a><\/li>\n<li style=\"text-align: justify;\"><a href=\"#pa\"><strong>Patching<\/strong><\/a><\/li>\n<\/ol>\n<p>The video version of the article:<strong>\u00a0<\/strong><\/p>\n<p>[\/et_pb_text][et_pb_video src=&#8221;<a href=\"https:\/\/youtu.be\/Ps3ThtscuCo&#038;#8221\" rel=\"nofollow\">https:\/\/youtu.be\/Ps3ThtscuCo&#038;#8221<\/a>; _builder_version=&#8221;4.4.6&#8243;][\/et_pb_video][et_pb_text module_id=&#8221;at&#8221; _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h2 style=\"text-align: justify;\">1. Automated Tests<\/h2>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">As we could see, manual testing is not really working for us especially if we want to detect issues early during the development phase. So, we decided to automate our tests. Based on the level of abstraction that tests are done they can be:<\/p>\n<p style=\"text-align: justify;\"><!-- \/divi:paragraph --><\/p>\n<p style=\"text-align: justify;\"><!-- divi:list --><\/p>\n<ul style=\"text-align: justify;\">\n<li><strong>Unit Tests<\/strong> &#8211; It is a piece of a code that invokes another piece of code (unit) and checks if an output of that action is the same as the desired output.<\/li>\n<li><strong>Integration Tests<\/strong> &#8211; It is testing a unit without full control over all parties in the test. They are using one or more of its outside dependencies, such as a database, threads, network, time, etc.<\/li>\n<li><strong>Functional Tests<\/strong> &#8211; It is testing a slice of a functionality of the system, observing it as a black box and verifying the output of the system to the functional specification.<\/li>\n<li><strong>Acceptance Tests<\/strong> &#8211; It is testing does the system fulfill expected business and contract requirements. It is considering the system as a black box.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><!-- \/divi:list --><\/p>\n<p style=\"text-align: justify;\"><!-- divi:image {\"id\":8984,\"align\":\"center\"} --><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\" style=\"text-align: justify;\"><\/figure>\n<\/div>\n<p>[\/et_pb_text][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/05\/Featured.png&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/05\/Featured.png&#038;#8221<\/a>; title_text=&#8221;Featured&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">These definitions are a bit loose since everyone is using different names for different types of tests. For example, some people use the name &#8220;developer&#8217;s tests&#8221; instead of &#8220;unit tests&#8221;, because those are tests that developers can read and understand. However, we will not go down that rabbit hole, important thing is that you get the point.<\/p>\n<p style=\"text-align: justify;\">In the image above, you can see the so-called Pyramid of Tests. It displays the number of tests that we should have in our application per type of test. We can see that we have the largest number of unit tests. For the purpose of this article, we will consider only this type of test, since they are crucial for the TDD process, as we will see in a little bit.<\/p>\n<p>[\/et_pb_text][et_pb_text module_id=&#8221;ut&#8221; _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h2 style=\"text-align: justify;\">2. Unit Tests<\/h2>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">As previously mentioned unit tests are testing the functionality of a unit. This brings us to a philosophical question about what exactly is &#8220;unit&#8221;? A <strong>unit <\/strong>is the set of actions between the invocation of a method in the system and a single noticeable output of that system. The equally philosophical answer, right? The important thing to understand here is that the unit test is a piece of code that tests another piece of code. Over the years, this type of test turned out to be one of the <strong>best tools<\/strong> for increasing software quality. They were introduced by Kent Back in Smalltalk back in the 1970s and since then they are used in pretty much any programming language.<\/p>\n<p style=\"text-align: justify;\"><!-- \/divi:paragraph --><\/p>\n<p style=\"text-align: justify;\"><!-- divi:paragraph -->So, how can we write unit tests in Python? Unit tests are always written using some sort of unit test framework. For Python that is module <em class=\"\"><g class=\"gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"6\" data-gr-id=\"6\">unittest<\/g><\/em>. Here is how we can use this module to write our first tests:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CgpjbGFzcyBGaXJzdFRlc3RDbGFzcyh1bml0dGVzdC5UZXN0Q2FzZSk6CgogICAgZGVmIHRlc3RfdXBwZXIoc2VsZik6CiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbCgncnViaWtzIGNvZGUnLnVwcGVyKCksICdSVUJJS1MgQ09ERScpCiAgICAgICAgCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICB1bml0dGVzdC5tYWluKCk=&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CgpjbGFzcyBGaXJzdFRlc3RDbGFzcyh1bml0dGVzdC5UZXN0Q2FzZSk6CgogICAgZGVmIHRlc3RfdXBwZXIoc2VsZik6CiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbCgncnViaWtzIGNvZGUnLnVwcGVyKCksICdSVUJJS1MgQ09ERScpCiAgICAgICAgCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICB1bml0dGVzdC5tYWluKCk=[\/et_pb_dmb_code_snippet][et_pb_code _builder_version=&#8221;4.4.6&#8243;][\/et_pb_code][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">First, we create the class <em>FirstTestClass<\/em>, which is inheriting <em>TestCase<\/em> from the <em><g class=\"gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling\" id=\"4\" data-gr-id=\"4\">unittest<\/g><\/em> module. Using this inheritance we are defining a test class that contains our test methods or test cases. These test cases will be registered within <em><g class=\"gr_ gr_968 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling\" id=\"968\" data-gr-id=\"968\"><g class=\"gr_ gr_971 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep\" id=\"971\" data-gr-id=\"971\">unittest<\/g><\/g><\/em> module and we will be able to run them later. In this class, we are having only one test case <em>test_upper<\/em>.<\/p>\n<p style=\"text-align: justify;\"><!-- \/divi:paragraph --><\/p>\n<p style=\"text-align: justify;\"><!-- divi:paragraph -->This method uses function <em>asserEqual<\/em> to verify that the call of the <em>upper<\/em> method on the string really returns the same string with all caps. The module <em><g class=\"gr_ gr_164 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"164\" data-gr-id=\"164\">unittest<\/g><\/em> has a lot of these functions that start with the word <em>assert<\/em>.<br \/> Essentially, every test method should call one of these methods to verify the results and so the test runner can accumulate all test results and produce a report. Finally, at the end of this file, we are calling <em>unitest.main<\/em>. This will run all registered tests. Here is what we get when we run this:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IEU6XHB5dGhvbl90ZGQ+cHl0aG9uIHNpbXBsZV91bml0X3Rlc3RzLnB5CiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQogUmFuIDEgdGVzdCBpbiAwLjAwMHMKIE9L&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IEU6XHB5dGhvbl90ZGQ+cHl0aG9uIHNpbXBsZV91bml0X3Rlc3RzLnB5CiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQogUmFuIDEgdGVzdCBpbiAwLjAwMHMKIE9L[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">If we want to know which tests cases are called you can just add <em>-v<\/em> as an argument:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IEU6XHB5dGhvbl90ZGQ+cHl0aG9uIHNpbXBsZV91bml0X3Rlc3RzLnB5IC12CiB0ZXN0X3VwcGVyIChtYWluLkZpcnN0VGVzdENsYXNzKSDigKYgb2sKIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIAogUmFuIDEgdGVzdCBpbiAwLjAwMHMKIE9L&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IEU6XHB5dGhvbl90ZGQ+cHl0aG9uIHNpbXBsZV91bml0X3Rlc3RzLnB5IC12CiB0ZXN0X3VwcGVyIChtYWluLkZpcnN0VGVzdENsYXNzKSDigKYgb2sKIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIAogUmFuIDEgdGVzdCBpbiAwLjAwMHMKIE9L[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">As we can see we run our one test case, and got the result that it passes, ie. the condition that we check with <em>assertEqual<\/em> is true. Congratulations, you&#8217;ve just run your first test with Python! Now, let&#8217;s see how we can test some functionality that we made. Take a look at this code:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIGdldF9ncmVldGluZ3MoKToKICAgIHJldHVybiAnSGVsbG8gV29ybGQhJw==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIGdldF9ncmVldGluZ3MoKToKICAgIHJldHVybiAnSGVsbG8gV29ybGQhJw==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">It is a very simple function <em>get_greetings<\/em> which is just returning the\u00a0<em>&#8216;Hello World!&#8217;<\/em> string. This is how we test it:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gaGVsbG93b3JsZCBpbXBvcnQgZ2V0X2dyZWV0aW5ncwoKY2xhc3MgSGVsbG93b3JsZFRlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIAogICAgZGVmIHRlc3RfZ2V0X2hlbGxvd29ybGQoc2VsZik6CiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbChnZXRfZ3JlZXRpbmdzKCksICdIZWxsbyBXb3JsZCEnKQoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gaGVsbG93b3JsZCBpbXBvcnQgZ2V0X2dyZWV0aW5ncwoKY2xhc3MgSGVsbG93b3JsZFRlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIAogICAgZGVmIHRlc3RfZ2V0X2hlbGxvd29ybGQoc2VsZik6CiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbChnZXRfZ3JlZXRpbmdzKCksICdIZWxsbyBXb3JsZCEnKQoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Pretty easy, right? We just import function from the file, write a\u00a0<g class=\"gr_ gr_10 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep\" id=\"10\" data-gr-id=\"10\">class<\/g> that inherits <em><g class=\"gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"5\" data-gr-id=\"5\">unittest<\/g>.TestCase<\/em> and verify the result using <em>assertEqual<\/em> within test method <em>test_get_helloworld<\/em>. The output looks like this:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IEU6XHB5dGhvbl90ZGQ+cHl0aG9uIHRlc3RzX2hlbGxvd29ybGQucHkgLXYKIHRlc3RfZ2V0X2hlbGxvd29ybGQgKG1haW4uSGVsbG93b3JsZFRlc3RzKSDigKYgb2sKIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIAogUmFuIDEgdGVzdCBpbiAwLjAwMXMKIE9L&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IEU6XHB5dGhvbl90ZGQ+cHl0aG9uIHRlc3RzX2hlbGxvd29ybGQucHkgLXYKIHRlc3RfZ2V0X2hlbGxvd29ybGQgKG1haW4uSGVsbG93b3JsZFRlc3RzKSDigKYgb2sKIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIAogUmFuIDEgdGVzdCBpbiAwLjAwMXMKIE9L[\/et_pb_dmb_code_snippet][et_pb_text module_id=&#8221;tdd&#8221; _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h2 style=\"text-align: justify;\">4. What is Test Driven Development?<\/h2>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><a rel=\"noreferrer noopener\" aria-label=\"Test Driven Development (TDD) (opens in a new tab)\" href=\"https:\/\/www.packtpub.com\/application-development\/introducing-test-driven-development-c-video\" target=\"_blank\">Test Driven Development (TDD)<\/a> is an evolutionary approach to building and designing software solutions. It is consisting of small cycles in which we are writing a unit test, that will initially fail, and then implementing the minimum amount of code to pass that test. After that code can be refactored to follow some good principles. Refactoring has a safety net, because we wrote the tests already, so we can reshape our solution stress-free.<\/p>\n<p style=\"text-align: justify;\"><!-- \/divi:paragraph --><\/p>\n<p style=\"text-align: justify;\"><!-- divi:paragraph -->These three important steps of TDD are easy to remember by using something that I like to call the <strong>TDD mantra<\/strong>. It goes like this: <strong>Red &#8211; Green &#8211; Refactor<\/strong>. Red is corresponding with the phase in which we write a test that will fail. Then we implement the code to make the previously written test pass meaning it is &#8211; Green. And finally, we refactor our code &#8211; and we don&#8217;t really have a color for that one. So there you go, TDD Mantra &#8211; Red, Green, Refactor.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/05\/tddmantra.png&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/05\/tddmantra.png&#038;#8221<\/a>; alt=&#8221;TDD Process&#8221; title_text=&#8221;tddmantra&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">You might wonder what is the difference between just writing unit tests for your code and TDD? In general, we are using unit tests in both cases. The crucial difference between TDD and traditional testing is <strong>the moment<\/strong> in which we are writing the tests. When we are writing code using TDD we first write the tests and then the code itself, and not another way around. <!-- \/divi:paragraph --> <!-- divi:paragraph -->The benefit of this approach is that we are minimizing the possibility of forgetting to write tests for some part of the code. Ideally, we end up with the code that is fully tested upfront and solutions that are implemented using TDD usually have 90%-100% of code covered with tests.<\/p>\n<p style=\"text-align: justify;\">Of course, when our code is tested it is less likely that we have a bug in our system. Another important difference is that we are writing <strong>small chunks of code<\/strong> to satisfy our test. This way the process itself drives our design and forces us to keep things simple. <!-- \/divi:paragraph --> <!-- divi:paragraph -->By using TDD we avoid creating over complicated designs and overengineered systems. Arguably this is the <strong>biggest benefit<\/strong> of this approach. When we use it we end up with clearer design and API. This approach also forces you to design classes properly and to follow good code principles like <a rel=\"noreferrer noopener\" aria-label=\"SOLID (opens in a new tab)\" href=\"https:\/\/rubikscode.net\/2017\/05\/07\/single-responsibility-principle-on-different-levels-of-abstraction\/\" target=\"_blank\">SOLID<\/a> and DRY. Personally, I find this way of development as a great procrastination killer and a great <strong>motivator<\/strong>. It kinda keeps you in the zone. <!-- \/divi:paragraph --> <!-- divi:heading --><\/p>\n<h2><\/h2>\n<p>[\/et_pb_text][et_pb_text module_id=&#8221;rick&#8221; _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h2 style=\"text-align: justify;\">4. Solving a problem using TDD<\/h2>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p>Before we proceed let&#8217;s examine what kind of problem we are trying to solve. Do you guys like TV show Rick and Morty? I love it. The show follows the adventures of cynical mad scientist Rick Sanchez and his grandson Morty Smith. Rick owns a portal gun and takes Morty to different dimensions\/universes. Different versions of these characters inhabit those other dimensions. The Citadel is the place where Ricks and Mortys have formed a society built by their counterparts from an infinite amount of realities. We are in luck because we have a request from The Citadel for one Python module. Here are the user stories:<\/p>\n<ul>\n<li>A user is able to assign Ricks and Mortys a universe number<\/li>\n<li>A user is able to add residents to the Citadel<\/li>\n<li>A user is able to turn all Ricks with assigned Mortys to pickles (watch s03e03)<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/3.png&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/3.png&#038;#8221<\/a>; alt=&#8221;TDD Process&#8221; title_text=&#8221;3&#8243; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h3>4.1 First User Story<\/h3>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Ok, let&#8217;s start from the first user story and work our TDD magic to the last user story. The first user story tells us that we should have two classes, one for Rick and one for Morty. However, since we are using TDD, we write the unit tests first. We implement <em>Rick<\/em> test class like this:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gcmljayBpbXBvcnQgUmljawoKY2xhc3MgUmlja1Rlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2sudW5pdmVyc2UsIDExMSkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gcmljayBpbXBvcnQgUmljawoKY2xhc3MgUmlja1Rlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2sudW5pdmVyc2UsIDExMSkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">\u00a0<!-- divi:paragraph -->Of <g class=\"gr_ gr_3 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep\" id=\"3\" data-gr-id=\"3\">course,<\/g> if we run this test we will get an error saying that Rick class is not existing:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PQogRVJST1I6IHRlc3RfdW5pdmVyc2UgKG1haW4uUmlja1Rlc3RzKQogVHJhY2ViYWNrIChtb3N0IHJlY2VudCBjYWxsIGxhc3QpOgogICBGaWxlICJyaWNrX3Rlc3RzLnB5IiwgbGluZSA1LCBpbiB0ZXN0X3VuaXZlcnNlCiAgICAgcmljayA9IFJpY2soMTExKQogTmFtZUVycm9yOiBuYW1lICdSaWNrJyBpcyBub3QgZGVmaW5lZAogCiBSYW4gMSB0ZXN0IGluIDAuMDAxcwogRkFJTEVEIChlcnJvcnM9MSk=&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PQogRVJST1I6IHRlc3RfdW5pdmVyc2UgKG1haW4uUmlja1Rlc3RzKQogVHJhY2ViYWNrIChtb3N0IHJlY2VudCBjYWxsIGxhc3QpOgogICBGaWxlICJyaWNrX3Rlc3RzLnB5IiwgbGluZSA1LCBpbiB0ZXN0X3VuaXZlcnNlCiAgICAgcmljayA9IFJpY2soMTExKQogTmFtZUVycm9yOiBuYW1lICdSaWNrJyBpcyBub3QgZGVmaW5lZAogCiBSYW4gMSB0ZXN0IGluIDAuMDAxcwogRkFJTEVEIChlcnJvcnM9MSk=[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">We need to define the class and initialize it through the constructor with the value for the <em>universe<\/em>:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;Y2xhc3MgUmljayhvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHVuaXZlcnNlKToKICAgICAgICBzZWxmLnVuaXZlcnNlID0gdW5pdmVyc2U=&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]Y2xhc3MgUmljayhvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHVuaXZlcnNlKToKICAgICAgICBzZWxmLnVuaXZlcnNlID0gdW5pdmVyc2U=[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Now, when we re-run the tests, we get this:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IHRlc3RfdW5pdmVyc2UgKG1haW4uUmlja1Rlc3RzKSDigKYgb2sKIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tICAKIFJhbiAxIHRlc3QgaW4gMC4wMDBzCiBPSw==&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IHRlc3RfdW5pdmVyc2UgKG1haW4uUmlja1Rlc3RzKSDigKYgb2sKIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tICAKIFJhbiAxIHRlc3QgaW4gMC4wMDBzCiBPSw==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">We are following the same pattern for Morty. The first the failing test:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gbW9ydHkgaW1wb3J0IE1vcnR5CgpjbGFzcyBNb3J0eVRlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIG1vcnR5ID0gTW9ydHkoMTExKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwobW9ydHkudW5pdmVyc2UsIDExMSkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gbW9ydHkgaW1wb3J0IE1vcnR5CgpjbGFzcyBNb3J0eVRlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIG1vcnR5ID0gTW9ydHkoMTExKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwobW9ydHkudW5pdmVyc2UsIDExMSkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Followed by the implementation:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;Y2xhc3MgTW9ydHkob2JqZWN0KToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCB1bml2ZXJzZSk6CiAgICAgICAgc2VsZi51bml2ZXJzZSA9IHVuaXZlcnNl&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]Y2xhc3MgTW9ydHkob2JqZWN0KToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCB1bml2ZXJzZSk6CiAgICAgICAgc2VsZi51bml2ZXJzZSA9IHVuaXZlcnNl[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">\u00a0And passed test:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IHRlc3RfdW5pdmVyc2UgKG1haW4uTW9ydHlUZXN0cykg4oCmIG9rCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gMSB0ZXN0IGluIDAuMDAwcwogT0s=&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IHRlc3RfdW5pdmVyc2UgKG1haW4uTW9ydHlUZXN0cykg4oCmIG9rCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gMSB0ZXN0IGluIDAuMDAwcwogT0s=[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"560\" height=\"315\" data-attachment-id=\"8994\" data-permalink=\"https:\/\/rubikscode.net\/2021\/05\/24\/test-driven-development-tdd-with-python\/giphy\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/giphy.gif\" data-orig-size=\"560,315\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"giphy\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/giphy-300x169.gif\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/giphy.gif\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/giphy.gif\" alt=\"\" class=\"wp-image-8994\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h3>4.2 Second User Story<\/h3>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>You might notice that this \u201cdance\u201d seems unnatural at first. It takes some time to get used to it, but once you do it is enchanting. You will wonder how you were able to do it another way for years. Ok, so we implemented our first user story. Let\u2019s move onto the second one. This one is pretty easy as well \u2013 \u201cA user is able to add residents to the Citadel\u201d. However, if we want to add residents, this means that\u00a0the\u00a0<\/span><em>Citadel<\/em><span>\u00a0class should have some sort of list or array of residents. Let\u2019s first make a function that will return all residents. We write a test for\u00a0<\/span><em>Citadel<\/em><span>\u00a0class:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gY2l0YWRlbCBpbXBvcnQgQ2l0YWRlbAoKY2xhc3MgQ2l0YWRlbFRlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X2dldF9hbGxfcmVzaWRlbnRzKHNlbGYpOgogICAgICAgIGNpdGFkZWwgPSBDaXRhZGVsKCkKICAgICAgICByZXNpZGVudHMgPSBjaXRhZGVsLmdldF9hbGxfcmVzaWRlbnRzKCkKICAgICAgICBzZWxmLmFzc2VydENvdW50RXF1YWwocmVzaWRlbnRzLCBbXSkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gY2l0YWRlbCBpbXBvcnQgQ2l0YWRlbAoKY2xhc3MgQ2l0YWRlbFRlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X2dldF9hbGxfcmVzaWRlbnRzKHNlbGYpOgogICAgICAgIGNpdGFkZWwgPSBDaXRhZGVsKCkKICAgICAgICByZXNpZGVudHMgPSBjaXRhZGVsLmdldF9hbGxfcmVzaWRlbnRzKCkKICAgICAgICBzZWxmLmFzc2VydENvdW50RXF1YWwocmVzaWRlbnRzLCBbXSkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>This test fails because\u00a0<\/span><em>Citadel<\/em><span>\u00a0implementation doesn\u2019t exist yet. Since this seems a little bit more complicated than previous implementations, we write something like this and try to make our test pass:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;Y2xhc3MgQ2l0YWRlbChvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYuX3Jlc2lkZW50cyA9IFtdCiAgICAgICAgCiAgICBkZWYgZ2V0X2FsbF9yZXNpZGVudHMoc2VsZik6CiAgICAgICAgcGFzcw==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243; hover_enabled=&#8221;0&#8243;]Y2xhc3MgQ2l0YWRlbChvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYuX3Jlc2lkZW50cyA9IFtdCiAgICAgICAgCiAgICBkZWYgZ2V0X2FsbF9yZXNpZGVudHMoc2VsZik6CiAgICAgICAgcGFzcw==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>As you can see, we defined private field\u00a0<\/span><em>__residents__<\/em><span>\u00a0and added method\u00a0<\/span><em>get_all_residents<\/em><span>\u00a0which is not doing anything at the moment. This way we can run our test, but it fails again:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PQogRVJST1I6IHRlc3RfZ2V0X2FsbF9yZXNpZGVudHMgKG1haW4uQ2l0YWRlbFRlc3RzKQogVHJhY2ViYWNrIChtb3N0IHJlY2VudCBjYWxsIGxhc3QpOgogICBGaWxlICJjaXRhZGVsX3Rlc3RzLnB5IiwgbGluZSAxMCwgaW4gdGVzdF9nZXRfYWxsX3Jlc2lkZW50cwogICAgIHNlbGYuYXNzZXJ0Q291bnRFcXVhbChyZXNpZGVudHMsIDApCiAgIEZpbGUgIkM6XFVzZXJzXE5pa29sYVZhbmphXEFuYWNvbmRhM1xsaWJcdW5pdHRlc3RcY2FzZS5weSIsIGxpbmUgMTE2NSwgaW4gYXNzZXJ0Q291bnRFcXVhbAogICAgIGZpcnN0X3NlcSwgc2Vjb25kX3NlcSA9IGxpc3QoZmlyc3QpLCBsaXN0KHNlY29uZCkKIFR5cGVFcnJvcjogJ05vbmVUeXBlJyBvYmplY3QgaXMgbm90IGl0ZXJhYmxlCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gMSB0ZXN0IGluIDAuMDAycwoKIEZBSUxFRCAoZXJyb3JzPTEp&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PQogRVJST1I6IHRlc3RfZ2V0X2FsbF9yZXNpZGVudHMgKG1haW4uQ2l0YWRlbFRlc3RzKQogVHJhY2ViYWNrIChtb3N0IHJlY2VudCBjYWxsIGxhc3QpOgogICBGaWxlICJjaXRhZGVsX3Rlc3RzLnB5IiwgbGluZSAxMCwgaW4gdGVzdF9nZXRfYWxsX3Jlc2lkZW50cwogICAgIHNlbGYuYXNzZXJ0Q291bnRFcXVhbChyZXNpZGVudHMsIDApCiAgIEZpbGUgIkM6XFVzZXJzXE5pa29sYVZhbmphXEFuYWNvbmRhM1xsaWJcdW5pdHRlc3RcY2FzZS5weSIsIGxpbmUgMTE2NSwgaW4gYXNzZXJ0Q291bnRFcXVhbAogICAgIGZpcnN0X3NlcSwgc2Vjb25kX3NlcSA9IGxpc3QoZmlyc3QpLCBsaXN0KHNlY29uZCkKIFR5cGVFcnJvcjogJ05vbmVUeXBlJyBvYmplY3QgaXMgbm90IGl0ZXJhYmxlCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gMSB0ZXN0IGluIDAuMDAycwoKIEZBSUxFRCAoZXJyb3JzPTEp[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>So, in order to fix this we have to return that private field through the method:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;Y2xhc3MgQ2l0YWRlbChvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYuX3Jlc2lkZW50cyA9IFtdCiAgICAgICAgCiAgICBkZWYgZ2V0X2FsbF9yZXNpZGVudHMoc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYuX3Jlc2lkZW50cw==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243; hover_enabled=&#8221;0&#8243;]Y2xhc3MgQ2l0YWRlbChvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYuX3Jlc2lkZW50cyA9IFtdCiAgICAgICAgCiAgICBkZWYgZ2V0X2FsbF9yZXNpZGVudHMoc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYuX3Jlc2lkZW50cw==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Re-run the test:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IHRlc3RfZ2V0X2FsbF9yZXNpZGVudHMgKG1haW4uQ2l0YWRlbFRlc3RzKSDigKYgb2sKIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tICAKIFJhbiAxIHRlc3QgaW4gMC4wMDFzCiBPSw==&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IHRlc3RfZ2V0X2FsbF9yZXNpZGVudHMgKG1haW4uQ2l0YWRlbFRlc3RzKSDigKYgb2sKIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tICAKIFJhbiAxIHRlc3QgaW4gMC4wMDFzCiBPSw==[\/et_pb_dmb_code_snippet][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/Z4aV.gif&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/Z4aV.gif&#038;#8221<\/a>; alt=&#8221;TDD Process&#8221; title_text=&#8221;Z4aV&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Hooray! Test passes and we are making progress. Still, functionality that satisfies the second user story is not implemented. That is why we write another test so that the complete\u00a0<\/span><em>Citadel<span>\u00a0<\/span><\/em><span>test class now looks like this:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gY2l0YWRlbCBpbXBvcnQgQ2l0YWRlbApmcm9tIHJpY2sgaW1wb3J0IFJpY2sKZnJvbSBtb3J0eSBpbXBvcnQgTW9ydHkKCmNsYXNzIENpdGFkZWxUZXN0cyh1bml0dGVzdC5UZXN0Q2FzZSk6CiAgICBkZWYgdGVzdF9nZXRfYWxsX3Jlc2lkZW50cyhzZWxmKToKICAgICAgICBjaXRhZGVsID0gQ2l0YWRlbCgpCiAgICAgICAgcmVzaWRlbnRzID0gY2l0YWRlbC5nZXRfYWxsX3Jlc2lkZW50cygpCiAgICAgICAgc2VsZi5hc3NlcnRDb3VudEVxdWFsKHJlc2lkZW50cywgW10pCiAgICAgICAgCiAgICBkZWYgdGVzdF9hZGRfcmVzaWRlbnQoc2VsZik6CiAgICAgICAgY2l0YWRlbCA9IENpdGFkZWwoKQogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBtb3J0eSA9IE1vcnR5KDExMSkKICAgICAgICAKICAgICAgICBjaXRhZGVsLmFkZF9yZXNpZGVudChyaWNrKQogICAgICAgIGNpdGFkZWwuYWRkX3Jlc2lkZW50KG1vcnR5KQogICAgICAgIHJlc2lkZW50cyA9IGNpdGFkZWwuZ2V0X2FsbF9yZXNpZGVudHMoKQogICAgICAgIAogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmVzaWRlbnRzWzBdLCByaWNrKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmVzaWRlbnRzWzFdLCBtb3J0eSkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gY2l0YWRlbCBpbXBvcnQgQ2l0YWRlbApmcm9tIHJpY2sgaW1wb3J0IFJpY2sKZnJvbSBtb3J0eSBpbXBvcnQgTW9ydHkKCmNsYXNzIENpdGFkZWxUZXN0cyh1bml0dGVzdC5UZXN0Q2FzZSk6CiAgICBkZWYgdGVzdF9nZXRfYWxsX3Jlc2lkZW50cyhzZWxmKToKICAgICAgICBjaXRhZGVsID0gQ2l0YWRlbCgpCiAgICAgICAgcmVzaWRlbnRzID0gY2l0YWRlbC5nZXRfYWxsX3Jlc2lkZW50cygpCiAgICAgICAgc2VsZi5hc3NlcnRDb3VudEVxdWFsKHJlc2lkZW50cywgW10pCiAgICAgICAgCiAgICBkZWYgdGVzdF9hZGRfcmVzaWRlbnQoc2VsZik6CiAgICAgICAgY2l0YWRlbCA9IENpdGFkZWwoKQogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBtb3J0eSA9IE1vcnR5KDExMSkKICAgICAgICAKICAgICAgICBjaXRhZGVsLmFkZF9yZXNpZGVudChyaWNrKQogICAgICAgIGNpdGFkZWwuYWRkX3Jlc2lkZW50KG1vcnR5KQogICAgICAgIHJlc2lkZW50cyA9IGNpdGFkZWwuZ2V0X2FsbF9yZXNpZGVudHMoKQogICAgICAgIAogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmVzaWRlbnRzWzBdLCByaWNrKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmVzaWRlbnRzWzFdLCBtb3J0eSkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Running this will fail because we don\u2019t have\u00a0<\/span><em>add_residents<\/em><span>\u00a0method. So, let\u2019s implement it:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;Y2xhc3MgQ2l0YWRlbChvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYuX3Jlc2lkZW50cyA9IFtdCiAgICAgICAgCiAgICBkZWYgZ2V0X2FsbF9yZXNpZGVudHMoc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYuX3Jlc2lkZW50cwogICAgCiAgICBkZWYgYWRkX3Jlc2lkZW50KHNlbGYsIHJlc2lkZW50KToKICAgICAgICBzZWxmLl9yZXNpZGVudHMuYXBwZW5kKHJlc2lkZW50KQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243; hover_enabled=&#8221;0&#8243;]Y2xhc3MgQ2l0YWRlbChvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYuX3Jlc2lkZW50cyA9IFtdCiAgICAgICAgCiAgICBkZWYgZ2V0X2FsbF9yZXNpZGVudHMoc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYuX3Jlc2lkZW50cwogICAgCiAgICBkZWYgYWRkX3Jlc2lkZW50KHNlbGYsIHJlc2lkZW50KToKICAgICAgICBzZWxmLl9yZXNpZGVudHMuYXBwZW5kKHJlc2lkZW50KQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Now when we run the test we get this:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IHRlc3RfYWRkX3Jlc2lkZW50IChtYWluLkNpdGFkZWxUZXN0cykg4oCmIG9rCiB0ZXN0X2dldF9hbGxfcmVzaWRlbnRzIChtYWluLkNpdGFkZWxUZXN0cykg4oCmIG9rCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gMiB0ZXN0cyBpbiAwLjAwMnMKIE9L&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IHRlc3RfYWRkX3Jlc2lkZW50IChtYWluLkNpdGFkZWxUZXN0cykg4oCmIG9rCiB0ZXN0X2dldF9hbGxfcmVzaWRlbnRzIChtYWluLkNpdGFkZWxUZXN0cykg4oCmIG9rCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gMiB0ZXN0cyBpbiAwLjAwMnMKIE9L[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h3>4.3 Third User Story<\/h3>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Awesome! We finished two out of three user stories. However, the last one is the trickiest. Let\u2019s examine it \u2013 A user is able to turn all Ricks with assigned Mortys to pickles. We can detect several tasks within this one sentence. We should be able to assign Morty to a Rick, meaning we need to extend both of those classes. We should be able to turn Rick into a pickle, as well. Finally, we should be able to do that for all Ricks in the Citadel with assigned Morties. So, let\u2019s proceed in that order. First, we extend Morty with\u00a0the\u00a0<\/span><em>is_assigned<\/em><span>\u00a0field. Extended\u00a0<\/span><em>Morty<\/em><span>\u00a0test class looks like this:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gbW9ydHkgaW1wb3J0IE1vcnR5CgpjbGFzcyBNb3J0eVRlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIG1vcnR5ID0gTW9ydHkoMTExKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwobW9ydHkudW5pdmVyc2UsIDExMSkKICAgIAogICAgZGVmIHRlc3RfaXNfYXNzaWduZWQoc2VsZik6CiAgICAgICAgbW9ydHkgPSBNb3J0eSgxMTEpCiAgICAgICAgc2VsZi5hc3NlcnRGYWxzZShtb3J0eS5pc19hc3NpZ25lZCkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gbW9ydHkgaW1wb3J0IE1vcnR5CgpjbGFzcyBNb3J0eVRlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIG1vcnR5ID0gTW9ydHkoMTExKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwobW9ydHkudW5pdmVyc2UsIDExMSkKICAgIAogICAgZGVmIHRlc3RfaXNfYXNzaWduZWQoc2VsZik6CiAgICAgICAgbW9ydHkgPSBNb3J0eSgxMTEpCiAgICAgICAgc2VsZi5hc3NlcnRGYWxzZShtb3J0eS5pc19hc3NpZ25lZCkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>The new test of the class will fail. We have to extend the\u00a0<\/span><em>Morty<\/em><span>\u00a0class implementation as well:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;Y2xhc3MgTW9ydHkob2JqZWN0KToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCB1bml2ZXJzZSk6CiAgICAgICAgc2VsZi51bml2ZXJzZSA9IHVuaXZlcnNlCiAgICAgICAgc2VsZi5pc19hc3NpZ25lZCA9IEZhbHNl&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]Y2xhc3MgTW9ydHkob2JqZWN0KToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCB1bml2ZXJzZSk6CiAgICAgICAgc2VsZi51bml2ZXJzZSA9IHVuaXZlcnNlCiAgICAgICAgc2VsZi5pc19hc3NpZ25lZCA9IEZhbHNl[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Run the test again:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IHRlc3RfaXNfYXNzaWduZWQgKG1haW4uTW9ydHlUZXN0cykg4oCmIG9rCiB0ZXN0X3VuaXZlcnNlIChtYWluLk1vcnR5VGVzdHMpIOKApiBvawogLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gIAogUmFuIDIgdGVzdHMgaW4gMC4wMDJzCiBPSw==&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IHRlc3RfaXNfYXNzaWduZWQgKG1haW4uTW9ydHlUZXN0cykg4oCmIG9rCiB0ZXN0X3VuaXZlcnNlIChtYWluLk1vcnR5VGVzdHMpIOKApiBvawogLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gIAogUmFuIDIgdGVzdHMgaW4gMC4wMDJzCiBPSw==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Ok, we are getting closer. Now\u00a0<\/span><em>Rick<\/em><span>\u00a0class should be extended so\u00a0<\/span><em>Morty<\/em><span>\u00a0can be assigned to\u00a0<\/span><em>Rick.<\/em><span>\u00a0We extend the\u00a0<\/span><em>Rick<\/em><span>\u00a0test class:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gcmljayBpbXBvcnQgUmljawpmcm9tIG1vcnR5IGltcG9ydCBNb3J0eQoKY2xhc3MgUmlja1Rlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2sudW5pdmVyc2UsIDExMSkKICAgICAgICAKICAgIGRlZiB0ZXN0X2hhc19tb3J0eShzZWxmKToKICAgICAgICByaWNrID0gUmljaygxMTEpCiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbChyaWNrLm1vcnR5LCBOb25lKQogICAgICAgIAppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgdW5pdHRlc3QubWFpbigp&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gcmljayBpbXBvcnQgUmljawpmcm9tIG1vcnR5IGltcG9ydCBNb3J0eQoKY2xhc3MgUmlja1Rlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2sudW5pdmVyc2UsIDExMSkKICAgICAgICAKICAgIGRlZiB0ZXN0X2hhc19tb3J0eShzZWxmKToKICAgICAgICByaWNrID0gUmljaygxMTEpCiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbChyaWNrLm1vcnR5LCBOb25lKQogICAgICAgIAppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgdW5pdHRlc3QubWFpbigp[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><g class=\"gr_ gr_4 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep\" id=\"4\" data-gr-id=\"4\">Test<\/g><span>\u00a0is failing because we are missing\u00a0<\/span><em><g class=\"gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace\" id=\"8\" data-gr-id=\"8\">morty<\/g><\/em><span>\u00a0field in\u00a0<\/span><em>Rick<\/em><span>\u00a0class. Let\u2019s extend\u00a0<\/span><em>Rick<\/em><span>:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;Y2xhc3MgUmljayhvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHVuaXZlcnNlKToKICAgICAgICBzZWxmLnVuaXZlcnNlID0gdW5pdmVyc2UKICAgICAgICBzZWxmLm1vcnR5ID0gTm9uZQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]Y2xhc3MgUmljayhvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHVuaXZlcnNlKToKICAgICAgICBzZWxmLnVuaXZlcnNlID0gdW5pdmVyc2UKICAgICAgICBzZWxmLm1vcnR5ID0gTm9uZQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Ignite tests again:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IHRlc3RfaGFzX21vcnR5IChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiB0ZXN0X3VuaXZlcnNlIChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gMiB0ZXN0cyBpbiAwLjAwMXMKIE9L&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IHRlc3RfaGFzX21vcnR5IChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiB0ZXN0X3VuaXZlcnNlIChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gMiB0ZXN0cyBpbiAwLjAwMXMKIE9L[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Nice, now let\u2019s extend tests for\u00a0<\/span><em>assign<\/em><span>\u00a0method, through which we will assign Morty to Rick:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gcmljayBpbXBvcnQgUmljawpmcm9tIG1vcnR5IGltcG9ydCBNb3J0eQoKY2xhc3MgUmlja1Rlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2sudW5pdmVyc2UsIDExMSkKICAgICAgICAKICAgIGRlZiB0ZXN0X2hhc19tb3J0eShzZWxmKToKICAgICAgICByaWNrID0gUmljaygxMTEpCiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbChyaWNrLm1vcnR5LCBOb25lKQogICAgICAgIAogICAgZGVmIHRlc3RfYXNzaW5nX21vcnR5KHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBtb3J0eSA9IE1vcnR5KDExMSkKICAgICAgICAKICAgICAgICByaWNrLmFzc2lnbihtb3J0eSkKICAgICAgICAKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2subW9ydHksIG1vcnR5KQogICAgICAgIHNlbGYuYXNzZXJ0VHJ1ZShtb3J0eS5pc19hc3NpZ25lZCkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gcmljayBpbXBvcnQgUmljawpmcm9tIG1vcnR5IGltcG9ydCBNb3J0eQoKY2xhc3MgUmlja1Rlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2sudW5pdmVyc2UsIDExMSkKICAgICAgICAKICAgIGRlZiB0ZXN0X2hhc19tb3J0eShzZWxmKToKICAgICAgICByaWNrID0gUmljaygxMTEpCiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbChyaWNrLm1vcnR5LCBOb25lKQogICAgICAgIAogICAgZGVmIHRlc3RfYXNzaW5nX21vcnR5KHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBtb3J0eSA9IE1vcnR5KDExMSkKICAgICAgICAKICAgICAgICByaWNrLmFzc2lnbihtb3J0eSkKICAgICAgICAKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2subW9ydHksIG1vcnR5KQogICAgICAgIHNlbGYuYXNzZXJ0VHJ1ZShtb3J0eS5pc19hc3NpZ25lZCkKICAgICAgICAKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHVuaXR0ZXN0Lm1haW4oKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>As you can see we are checking two things after Morty is assigned. When we extend\u00a0<\/span><em>Rick<\/em><span>\u00a0class to support these changes it looks like this:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;Y2xhc3MgUmljayhvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHVuaXZlcnNlKToKICAgICAgICBzZWxmLnVuaXZlcnNlID0gdW5pdmVyc2UKICAgICAgICBzZWxmLm1vcnR5ID0gTm9uZQogICAgICAgIAogICAgZGVmIGFzc2lnbihzZWxmLCBtb3J0eSk6CiAgICAgICAgc2VsZi5tb3J0eSA9IG1vcnR5CiAgICAgICAgbW9ydHkuaXNfYXNzaWduZWQgPSBUcnVl&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]Y2xhc3MgUmljayhvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHVuaXZlcnNlKToKICAgICAgICBzZWxmLnVuaXZlcnNlID0gdW5pdmVyc2UKICAgICAgICBzZWxmLm1vcnR5ID0gTm9uZQogICAgICAgIAogICAgZGVmIGFzc2lnbihzZWxmLCBtb3J0eSk6CiAgICAgICAgc2VsZi5tb3J0eSA9IG1vcnR5CiAgICAgICAgbW9ydHkuaXNfYXNzaWduZWQgPSBUcnVl[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>And when we re-run the tests for\u00a0<\/span><em>Rick<\/em><span>\u00a0class:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IHRlc3RfYXNzaW5nX21vcnR5IChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiB0ZXN0X2hhc19tb3J0eSAobWFpbi5SaWNrVGVzdHMpIOKApiBvawogdGVzdF91bml2ZXJzZSAobWFpbi5SaWNrVGVzdHMpIOKApiBvawogLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gIAogUmFuIDMgdGVzdHMgaW4gMC4wMDJzCiBPSw==&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IHRlc3RfYXNzaW5nX21vcnR5IChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiB0ZXN0X2hhc19tb3J0eSAobWFpbi5SaWNrVGVzdHMpIOKApiBvawogdGVzdF91bml2ZXJzZSAobWFpbi5SaWNrVGVzdHMpIOKApiBvawogLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gIAogUmFuIDMgdGVzdHMgaW4gMC4wMDJzCiBPSw==[\/et_pb_dmb_code_snippet][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/giphy-2.gif&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/giphy-2.gif&#038;#8221<\/a>; alt=&#8221;TDD Process&#8221; title_text=&#8221;giphy (2)&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Don\u2019t give up on me now, we are halfway through the third user story! We need to make Rick \u201cpickable\u201d and turn all Ricks with assigned Mortys in the Citadel into pickles (I never thought I would write down a sentence like this :)). To the\u00a0<\/span><em>Rick<\/em><span>\u00a0test class!<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gcmljayBpbXBvcnQgUmljawpmcm9tIG1vcnR5IGltcG9ydCBNb3J0eQoKY2xhc3MgUmlja1Rlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2sudW5pdmVyc2UsIDExMSkKICAgICAgICAKICAgIGRlZiB0ZXN0X2hhc19tb3J0eShzZWxmKToKICAgICAgICByaWNrID0gUmljaygxMTEpCiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbChyaWNrLm1vcnR5LCBOb25lKQogICAgICAgIAogICAgZGVmIHRlc3RfYXNzaW5nX21vcnR5KHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBtb3J0eSA9IE1vcnR5KDExMSkKICAgICAgICAKICAgICAgICByaWNrLmFzc2lnbihtb3J0eSkKICAgICAgICAKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2subW9ydHksIG1vcnR5KQogICAgICAgIHNlbGYuYXNzZXJ0VHJ1ZShtb3J0eS5pc19hc3NpZ25lZCkKICAgICAgICAKICAgIGRlZiB0ZXN0X2hhc19pc19waWNrbGUoc2VsZik6CiAgICAgICAgcmljayA9IFJpY2soMTExKQogICAgICAgIHNlbGYuYXNzZXJ0RmFsc2Uocmljay5pc19waWNrbGUpCiAgICAgICAgCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICB1bml0dGVzdC5tYWluKCk=&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gcmljayBpbXBvcnQgUmljawpmcm9tIG1vcnR5IGltcG9ydCBNb3J0eQoKY2xhc3MgUmlja1Rlc3RzKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiB0ZXN0X3VuaXZlcnNlKHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2sudW5pdmVyc2UsIDExMSkKICAgICAgICAKICAgIGRlZiB0ZXN0X2hhc19tb3J0eShzZWxmKToKICAgICAgICByaWNrID0gUmljaygxMTEpCiAgICAgICAgc2VsZi5hc3NlcnRFcXVhbChyaWNrLm1vcnR5LCBOb25lKQogICAgICAgIAogICAgZGVmIHRlc3RfYXNzaW5nX21vcnR5KHNlbGYpOgogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBtb3J0eSA9IE1vcnR5KDExMSkKICAgICAgICAKICAgICAgICByaWNrLmFzc2lnbihtb3J0eSkKICAgICAgICAKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2subW9ydHksIG1vcnR5KQogICAgICAgIHNlbGYuYXNzZXJ0VHJ1ZShtb3J0eS5pc19hc3NpZ25lZCkKICAgICAgICAKICAgIGRlZiB0ZXN0X2hhc19pc19waWNrbGUoc2VsZik6CiAgICAgICAgcmljayA9IFJpY2soMTExKQogICAgICAgIHNlbGYuYXNzZXJ0RmFsc2Uocmljay5pc19waWNrbGUpCiAgICAgICAgCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICB1bml0dGVzdC5tYWluKCk=[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>This seems familiar. Test\u00a0<\/span><em>test_has_is_pickle<\/em><span>\u00a0fails because, well,\u00a0the\u00a0<\/span><em>Rick<\/em><span>\u00a0class still has no field\u00a0<\/span><em>is_pickle<\/em><span>.\u00a0<\/span><em>Rick<\/em><span>\u00a0class needs to be extended for that:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IHRlc3RfYXNzaW5nX21vcnR5IChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiB0ZXN0X2hhc19pc19waWNrbGUgKG1haW4uUmlja1Rlc3RzKSDigKYgb2sKIHRlc3RfaGFzX21vcnR5IChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiB0ZXN0X3VuaXZlcnNlIChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gNCB0ZXN0cyBpbiAwLjAwM3MKIE9L&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IHRlc3RfYXNzaW5nX21vcnR5IChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiB0ZXN0X2hhc19pc19waWNrbGUgKG1haW4uUmlja1Rlc3RzKSDigKYgb2sKIHRlc3RfaGFzX21vcnR5IChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiB0ZXN0X3VuaXZlcnNlIChtYWluLlJpY2tUZXN0cykg4oCmIG9rCiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAgCiBSYW4gNCB0ZXN0cyBpbiAwLjAwM3MKIE9L[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Awesome! Now, to the\u00a0<\/span><em>Citadel<\/em><span>\u00a0test class. We add one large test:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IHVuaXR0ZXN0CmZyb20gY2l0YWRlbCBpbXBvcnQgQ2l0YWRlbApmcm9tIHJpY2sgaW1wb3J0IFJpY2sKZnJvbSBtb3J0eSBpbXBvcnQgTW9ydHkKCmNsYXNzIENpdGFkZWxUZXN0cyh1bml0dGVzdC5UZXN0Q2FzZSk6CiAgICBkZWYgdGVzdF9nZXRfYWxsX3Jlc2lkZW50cyhzZWxmKToKICAgICAgICBjaXRhZGVsID0gQ2l0YWRlbCgpCiAgICAgICAgcmVzaWRlbnRzID0gY2l0YWRlbC5nZXRfYWxsX3Jlc2lkZW50cygpCiAgICAgICAgc2VsZi5hc3NlcnRDb3VudEVxdWFsKHJlc2lkZW50cywgW10pCiAgICAgICAgCiAgICBkZWYgdGVzdF9hZGRfcmVzaWRlbnQoc2VsZik6CiAgICAgICAgY2l0YWRlbCA9IENpdGFkZWwoKQogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBtb3J0eSA9IE1vcnR5KDExMSkKICAgICAgICAKICAgICAgICBjaXRhZGVsLmFkZF9yZXNpZGVudChyaWNrKQogICAgICAgIGNpdGFkZWwuYWRkX3Jlc2lkZW50KG1vcnR5KQogICAgICAgIHJlc2lkZW50cyA9IGNpdGFkZWwuZ2V0X2FsbF9yZXNpZGVudHMoKQogICAgICAgIAogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmVzaWRlbnRzWzBdLCByaWNrKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmVzaWRlbnRzWzFdLCBtb3J0eSkKICAgICAgICAKICAgIGRlZiB0ZXN0X3BpY2xlX3JpY2tzX3dpdGhfbW9ydGllcyhzZWxmKToKICAgICAgICBjaXRhZGVsID0gQ2l0YWRlbCgpCiAgICAgICAgcmljayA9IFJpY2soMTExKQogICAgICAgIG1vcnR5ID0gTW9ydHkoMTExKQogICAgICAgIHJpY2suYXNzaWduKG1vcnR5KQogICAgICAgIAogICAgICAgIGNpdGFkZWwuYWRkX3Jlc2lkZW50KHJpY2spCiAgICAgICAgY2l0YWRlbC5hZGRfcmVzaWRlbnQobW9ydHkpCiAgICAgICAgCiAgICAgICAgY2l0YWRlbC5waWNsZV9yaWNrc193aXRoX21vcnRpZXMoKQogICAgICAgIHJlc2lkZW50cyA9IGNpdGFkZWwuZ2V0X2FsbF9yZXNpZGVudHMoKQogICAgICAgIAogICAgICAgIHNlbGYuYXNzZXJ0VHJ1ZShyZXNpZGVudHNbMF0uaXNfcGlja2xlKQogICAgICAgIAppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgdW5pdHRlc3QubWFpbigp&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IHVuaXR0ZXN0CmZyb20gY2l0YWRlbCBpbXBvcnQgQ2l0YWRlbApmcm9tIHJpY2sgaW1wb3J0IFJpY2sKZnJvbSBtb3J0eSBpbXBvcnQgTW9ydHkKCmNsYXNzIENpdGFkZWxUZXN0cyh1bml0dGVzdC5UZXN0Q2FzZSk6CiAgICBkZWYgdGVzdF9nZXRfYWxsX3Jlc2lkZW50cyhzZWxmKToKICAgICAgICBjaXRhZGVsID0gQ2l0YWRlbCgpCiAgICAgICAgcmVzaWRlbnRzID0gY2l0YWRlbC5nZXRfYWxsX3Jlc2lkZW50cygpCiAgICAgICAgc2VsZi5hc3NlcnRDb3VudEVxdWFsKHJlc2lkZW50cywgW10pCiAgICAgICAgCiAgICBkZWYgdGVzdF9hZGRfcmVzaWRlbnQoc2VsZik6CiAgICAgICAgY2l0YWRlbCA9IENpdGFkZWwoKQogICAgICAgIHJpY2sgPSBSaWNrKDExMSkKICAgICAgICBtb3J0eSA9IE1vcnR5KDExMSkKICAgICAgICAKICAgICAgICBjaXRhZGVsLmFkZF9yZXNpZGVudChyaWNrKQogICAgICAgIGNpdGFkZWwuYWRkX3Jlc2lkZW50KG1vcnR5KQogICAgICAgIHJlc2lkZW50cyA9IGNpdGFkZWwuZ2V0X2FsbF9yZXNpZGVudHMoKQogICAgICAgIAogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmVzaWRlbnRzWzBdLCByaWNrKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmVzaWRlbnRzWzFdLCBtb3J0eSkKICAgICAgICAKICAgIGRlZiB0ZXN0X3BpY2xlX3JpY2tzX3dpdGhfbW9ydGllcyhzZWxmKToKICAgICAgICBjaXRhZGVsID0gQ2l0YWRlbCgpCiAgICAgICAgcmljayA9IFJpY2soMTExKQogICAgICAgIG1vcnR5ID0gTW9ydHkoMTExKQogICAgICAgIHJpY2suYXNzaWduKG1vcnR5KQogICAgICAgIAogICAgICAgIGNpdGFkZWwuYWRkX3Jlc2lkZW50KHJpY2spCiAgICAgICAgY2l0YWRlbC5hZGRfcmVzaWRlbnQobW9ydHkpCiAgICAgICAgCiAgICAgICAgY2l0YWRlbC5waWNsZV9yaWNrc193aXRoX21vcnRpZXMoKQogICAgICAgIHJlc2lkZW50cyA9IGNpdGFkZWwuZ2V0X2FsbF9yZXNpZGVudHMoKQogICAgICAgIAogICAgICAgIHNlbGYuYXNzZXJ0VHJ1ZShyZXNpZGVudHNbMF0uaXNfcGlja2xlKQogICAgICAgIAppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgdW5pdHRlc3QubWFpbigp[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Here is the explanation. We create all necessary objects, assign Morty to Rick, add both objects into Citadel and call a method that should turn all Ricks with Mortys into pickles. Cool, let\u2019s reflect that in\u00a0<\/span><em>Citadel<\/em><span>\u00a0class implementation:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZnJvbSByaWNrIGltcG9ydCBSaWNrCgpjbGFzcyBDaXRhZGVsKG9iamVjdCk6CiAgICBkZWYgX19pbml0X18oc2VsZik6CiAgICAgICAgc2VsZi5fcmVzaWRlbnRzID0gW10KICAgICAgICAKICAgIGRlZiBnZXRfYWxsX3Jlc2lkZW50cyhzZWxmKToKICAgICAgICByZXR1cm4gc2VsZi5fcmVzaWRlbnRzCiAgICAKICAgIGRlZiBhZGRfcmVzaWRlbnQoc2VsZiwgcmVzaWRlbnQpOgogICAgICAgIHNlbGYuX19yZXNpZGVudHNfXy5hcHBlbmQocmVzaWRlbnQpCiAgICAgICAgCiAgICBkZWYgcGljbGVfcmlja3Nfd2l0aF9tb3J0aWVzKHNlbGYpOgogICAgICAgIGZvciByZXNpZGVudCBpbiBzZWxmLl9yZXNpZGVudHM6CiAgICAgICAgICAgIGlmIGlzaW5zdGFuY2UocmVzaWRlbnQsIFJpY2spOgogICAgICAgICAgICAgICAgaWYgcmVzaWRlbnQubW9ydHk6IHJlc2lkZW50LmlzX3BpY2tsZSA9IFRydWU=&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243; hover_enabled=&#8221;0&#8243;]ZnJvbSByaWNrIGltcG9ydCBSaWNrCgpjbGFzcyBDaXRhZGVsKG9iamVjdCk6CiAgICBkZWYgX19pbml0X18oc2VsZik6CiAgICAgICAgc2VsZi5fcmVzaWRlbnRzID0gW10KICAgICAgICAKICAgIGRlZiBnZXRfYWxsX3Jlc2lkZW50cyhzZWxmKToKICAgICAgICByZXR1cm4gc2VsZi5fcmVzaWRlbnRzCiAgICAKICAgIGRlZiBhZGRfcmVzaWRlbnQoc2VsZiwgcmVzaWRlbnQpOgogICAgICAgIHNlbGYuX19yZXNpZGVudHNfXy5hcHBlbmQocmVzaWRlbnQpCiAgICAgICAgCiAgICBkZWYgcGljbGVfcmlja3Nfd2l0aF9tb3J0aWVzKHNlbGYpOgogICAgICAgIGZvciByZXNpZGVudCBpbiBzZWxmLl9yZXNpZGVudHM6CiAgICAgICAgICAgIGlmIGlzaW5zdGFuY2UocmVzaWRlbnQsIFJpY2spOgogICAgICAgICAgICAgICAgaWYgcmVzaWRlbnQubW9ydHk6IHJlc2lkZW50LmlzX3BpY2tsZSA9IFRydWU=[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Alright, let\u2019s re-run the tests:<\/span><\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;IHRlc3RfYWRkX3Jlc2lkZW50IChtYWluLkNpdGFkZWxUZXN0cykg4oCmIG9rCiB0ZXN0X2dldF9hbGxfcmVzaWRlbnRzIChtYWluLkNpdGFkZWxUZXN0cykg4oCmIG9rCiB0ZXN0X3BpY2xlX3JpY2tzX3dpdGhfbW9ydGllcyAobWFpbi5DaXRhZGVsVGVzdHMpIOKApiBvawogLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gIAogUmFuIDMgdGVzdHMgaW4gMC4wMDJzCiBPSw==&#8221; language=&#8221;basic&#8221; style=&#8221;dark&#8221; _builder_version=&#8221;4.4.6&#8243; body_text_color=&#8221;#dd6c38&#8243;]IHRlc3RfYWRkX3Jlc2lkZW50IChtYWluLkNpdGFkZWxUZXN0cykg4oCmIG9rCiB0ZXN0X2dldF9hbGxfcmVzaWRlbnRzIChtYWluLkNpdGFkZWxUZXN0cykg4oCmIG9rCiB0ZXN0X3BpY2xlX3JpY2tzX3dpdGhfbW9ydGllcyAobWFpbi5DaXRhZGVsVGVzdHMpIOKApiBvawogLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gIAogUmFuIDMgdGVzdHMgaW4gMC4wMDJzCiBPSw==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><span>Woooohoooo! The tests are passing and we completed our third and final user story!<\/span><\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/anigif_sub-buzz-22010-1521220575-14.gif&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/anigif_sub-buzz-22010-1521220575-14.gif&#038;#8221<\/a>; alt=&#8221;TDD Process&#8221; title_text=&#8221;anigif_sub-buzz-22010-1521220575-14&#8243; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text module_id=&#8221;mo&#8221; _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h2 style=\"text-align: justify;\">5. Mock Objects<\/h2>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">We need to know how to mock objects in order to fully master TDD. So what would this mean? Well, in Object-Oriented programming <strong>mock objects<\/strong> are defined as simulated objects. They mimic the behavior of real objects in controlled ways. Why would anyone do that? Well, the whole point of unit testing is isolating certain functionality (unit) and test <strong>just that<\/strong>. We are trying to remove all other dependencies.<\/p>\n<p style=\"text-align: justify;\"><!-- \/divi:paragraph --> <!-- divi:paragraph --><\/p>\n<p style=\"text-align: justify;\">Sometimes this can be a database or file system, so using mock objects we don&#8217;t step into the world of integration tests. Apart from that, this would mean that we need to take care of the data in the database before and after every test, and that is something we want to avoid.\u00a0Other times dependency can be just some other class or function. This certainly is not helping us with our goal &#8211; the isolation of the unit. That is why we define mock objects.<\/p>\n<p>A mock object should be used in the situations when:<\/p>\n<p><!-- \/divi:paragraph --> <!-- divi:list --><\/p>\n<ul>\n<li>The real object is slow<\/li>\n<li>The real object rarely and is difficult to produce artificially<\/li>\n<li>The real object produces non-deterministic results<\/li>\n<li>The real object does not yet exist (often the case in TDD )<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/04\/undraw_personal_file_222m.png&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/04\/undraw_personal_file_222m.png&#038;#8221<\/a>; alt=&#8221;TDD Process&#8221; title_text=&#8221;undraw_personal_file_222m&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h3>5.1 Mock Objects in Python<\/h3>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">For the purpose of this article, we use the <em><strong>unittest<\/strong><\/em><strong> module<\/strong>. To be more specific, we use the <em><strong>Mock<\/strong><\/em><strong> class<\/strong>. Essentially, this class is the core class using which we can create stubs in our test methods. After performing an action on these objects, you can check various details which can help you determine if your functionality is behaving properly. The cool trick is that with the objects of <em>Mock<\/em> class you can mock any function, variable, or object. Check out this test:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3Qoc2VsZik6CiAgbSA9IG1vY2suTW9jaygpCiAgYXNzZXJ0IGlzaW5zdGFuY2UobS5maWVsZCwgbW9jay5Nb2NrKQogIGFzc2VydCBpc2luc3RhbmNlKG0uZmllbGQyLCBtb2NrLk1vY2spCiAgYXNzZXJ0IGlzaW5zdGFuY2UobSgpLCBtb2NrLk1vY2spCiAgYXNzZXJ0IG0uZmllbGQgaXMgbm90IG0uZmllbGQyIGlzIG5vdCBtKCk=&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3Qoc2VsZik6CiAgbSA9IG1vY2suTW9jaygpCiAgYXNzZXJ0IGlzaW5zdGFuY2UobS5maWVsZCwgbW9jay5Nb2NrKQogIGFzc2VydCBpc2luc3RhbmNlKG0uZmllbGQyLCBtb2NrLk1vY2spCiAgYXNzZXJ0IGlzaW5zdGFuY2UobSgpLCBtb2NrLk1vY2spCiAgYXNzZXJ0IG0uZmllbGQgaXMgbm90IG0uZmllbGQyIGlzIG5vdCBtKCk=[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Note that you can define and access any <em>&#8216;field&#8217;<\/em> of a <em>Mock<\/em> object which returns another object of a <em>Mock<\/em> class. Apart from that, you can observe this object as function as well. This means that you can prepare this mock object in any way you want. For example, you can <strong>assign <\/strong>some value to the fields of this object. It can be achieved either by assigning this value directly or using <em>configure_mock<\/em>:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfZmllbGRzKHNlbGYpOgogIG0gPSBtb2NrLk1vY2soKQogIG0ucnViaWtzID0gJ2NvZGUnCiAgc2VsZi5hc3NlcnRFcXVhbChtLnJ1YmlrcywgJ2NvZGUnKQoKICBtLmNvbmZpZ3VyZV9tb2NrKHRkZD0ncnVsZXonKQogIHNlbGYuYXNzZXJ0RXF1YWwobS50ZGQsICdydWxleicp&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfZmllbGRzKHNlbGYpOgogIG0gPSBtb2NrLk1vY2soKQogIG0ucnViaWtzID0gJ2NvZGUnCiAgc2VsZi5hc3NlcnRFcXVhbChtLnJ1YmlrcywgJ2NvZGUnKQoKICBtLmNvbmZpZ3VyZV9tb2NrKHRkZD0ncnVsZXonKQogIHNlbGYuYXNzZXJ0RXF1YWwobS50ZGQsICdydWxleicp[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h3>5.2 Mocking Functions in Python<\/h3>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Of course, you can configure <strong>mock methods<\/strong> as well. For example, if you want your mock object to have a method that returns certain value, you can do it like this:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfZnVuY3Rpb25zKHNlbGYpOgogIG0gPSBtb2NrLk1vY2soKQogIG0uZ2V0X2VsZXZlbi5yZXR1cm5fdmFsdWUgPSAxMQogIHNlbGYuYXNzZXJ0RXF1YWwobS5nZXRfZWxldmVuKCksIDExKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfZnVuY3Rpb25zKHNlbGYpOgogIG0gPSBtb2NrLk1vY2soKQogIG0uZ2V0X2VsZXZlbi5yZXR1cm5fdmFsdWUgPSAxMQogIHNlbGYuYXNzZXJ0RXF1YWwobS5nZXRfZWxldmVuKCksIDExKQ==[\/et_pb_dmb_code_snippet][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2020\/04\/undraw_programmer_imem.png&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2020\/04\/undraw_programmer_imem.png&#038;#8221<\/a>; alt=&#8221;TDD Process&#8221; title_text=&#8221;undraw_programmer_imem&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">You see that we used <em><g class=\"gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep\" id=\"6\" data-gr-id=\"6\">return_value<\/g><\/em> option. This is pretty simple, however, sometimes we want to test non-happy paths. For example, we want our method to <strong>throw an <\/strong><g class=\"gr_ gr_80 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep\" id=\"80\" data-gr-id=\"80\"><strong>exception<\/strong><\/g>. That can be configured like this:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfZXhjZXB0aW9uKHNlbGYpOgogICAgbSA9IG1vY2suTW9jaygpCiAgICBtLnRob3dfZXhjZXB0aW9uLnNpZGVfZWZmZWN0ID0gUnVudGltZUVycm9yKCdPaCBubyEnKQoKICAgIHRyeToKICAgICAgICBtLnRob3dfZXhjZXB0aW9uKCkKICAgIGV4Y2VwdCBSdW50aW1lRXJyb3I6CiAgICAgICAgYXNzZXJ0IFRydWUKICAgIGVsc2U6CiAgICAgICAgYXNzZXJ0IEZhbHNl&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfZXhjZXB0aW9uKHNlbGYpOgogICAgbSA9IG1vY2suTW9jaygpCiAgICBtLnRob3dfZXhjZXB0aW9uLnNpZGVfZWZmZWN0ID0gUnVudGltZUVycm9yKCdPaCBubyEnKQoKICAgIHRyeToKICAgICAgICBtLnRob3dfZXhjZXB0aW9uKCkKICAgIGV4Y2VwdCBSdW50aW1lRXJyb3I6CiAgICAgICAgYXNzZXJ0IFRydWUKICAgIGVsc2U6CiAgICAgICAgYXNzZXJ0IEZhbHNl[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">This <em>side_effect<\/em> option is a <g class=\"gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep\" id=\"6\" data-gr-id=\"6\">really<\/g> important feature. We can do other things with it too. If you want to return a different value on <strong>each call<\/strong> of the function this option got you covered:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfbXVsdGlwbGVfcmV0dXJuKHNlbGYpOgogICAgbSA9IG1vY2suTW9jaygpCiAgICBtLmdldF9lbGV2ZW4ucmV0dXJuX3ZhbHVlID0gMTEKCiAgICBtLmdldF9lbGV2ZW4uc2lkZV9lZmZlY3QgPSBbMTIsIDEzLCAxNF0KICAgIHNlbGYuYXNzZXJ0RXF1YWwobS5nZXRfZWxldmVuKCksIDEyKQogICAgc2VsZi5hc3NlcnRFcXVhbChtLmdldF9lbGV2ZW4oKSwgMTMpCiAgICBzZWxmLmFzc2VydEVxdWFsKG0uZ2V0X2VsZXZlbigpLCAxNCk=&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfbXVsdGlwbGVfcmV0dXJuKHNlbGYpOgogICAgbSA9IG1vY2suTW9jaygpCiAgICBtLmdldF9lbGV2ZW4ucmV0dXJuX3ZhbHVlID0gMTEKCiAgICBtLmdldF9lbGV2ZW4uc2lkZV9lZmZlY3QgPSBbMTIsIDEzLCAxNF0KICAgIHNlbGYuYXNzZXJ0RXF1YWwobS5nZXRfZWxldmVuKCksIDEyKQogICAgc2VsZi5hc3NlcnRFcXVhbChtLmdldF9lbGV2ZW4oKSwgMTMpCiAgICBzZWxmLmFzc2VydEVxdWFsKG0uZ2V0X2VsZXZlbigpLCAxNCk=[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Using <em>Mock<\/em> class we can verify that some method has been <strong>called <\/strong>as well:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfdmVyaWZ5X2NhbGxlZChzZWxmKToKICAgIG0gPSBtb2NrLk1vY2soKQogICAgbS5jYWxsZWRfZnVuY3Rpb24ucmV0dXJuX3ZhbHVlID0gMTEKICAgIG0uY2FsbGVkX2Z1bmN0aW9uKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uLmFzc2VydF9jYWxsZWQoKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfdmVyaWZ5X2NhbGxlZChzZWxmKToKICAgIG0gPSBtb2NrLk1vY2soKQogICAgbS5jYWxsZWRfZnVuY3Rpb24ucmV0dXJuX3ZhbHVlID0gMTEKICAgIG0uY2FsbGVkX2Z1bmN0aW9uKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uLmFzc2VydF9jYWxsZWQoKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">For that matter, we can check if some function has been called once or <strong>multiple times<\/strong>:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfdmVyaWZ5X2NhbGxlZF9vbmNlKHNlbGYpOgogICAgbSA9IG1vY2suTW9jaygpCiAgICBtLmNhbGxlZF9mdW5jdGlvbi5yZXR1cm5fdmFsdWUgPSAxMQogICAgbS5jYWxsZWRfZnVuY3Rpb24oKQogICAgbS5jYWxsZWRfZnVuY3Rpb24uYXNzZXJ0X2NhbGxlZF9vbmNlKCkKCmRlZiB0ZXN0X3ZlcmlmeV9jYWxsZWRfbXVsdGlwbGUoc2VsZik6CiAgICBtID0gbW9jay5Nb2NrKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uLnJldHVybl92YWx1ZSA9IDExCiAgICBtLmNhbGxlZF9mdW5jdGlvbigpCiAgICBtLmNhbGxlZF9mdW5jdGlvbigpCiAgICBtLmNhbGxlZF9mdW5jdGlvbigpCiAgICBzZWxmLmFzc2VydEVxdWFsKG0uY2FsbGVkX2Z1bmN0aW9uLmNhbGxfY291bnQsIDMp&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfdmVyaWZ5X2NhbGxlZF9vbmNlKHNlbGYpOgogICAgbSA9IG1vY2suTW9jaygpCiAgICBtLmNhbGxlZF9mdW5jdGlvbi5yZXR1cm5fdmFsdWUgPSAxMQogICAgbS5jYWxsZWRfZnVuY3Rpb24oKQogICAgbS5jYWxsZWRfZnVuY3Rpb24uYXNzZXJ0X2NhbGxlZF9vbmNlKCkKCmRlZiB0ZXN0X3ZlcmlmeV9jYWxsZWRfbXVsdGlwbGUoc2VsZik6CiAgICBtID0gbW9jay5Nb2NrKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uLnJldHVybl92YWx1ZSA9IDExCiAgICBtLmNhbGxlZF9mdW5jdGlvbigpCiAgICBtLmNhbGxlZF9mdW5jdGlvbigpCiAgICBtLmNhbGxlZF9mdW5jdGlvbigpCiAgICBzZWxmLmFzc2VydEVxdWFsKG0uY2FsbGVkX2Z1bmN0aW9uLmNhbGxfY291bnQsIDMp[\/et_pb_dmb_code_snippet][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2020\/03\/undraw_metrics_gtu7.png&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2020\/03\/undraw_metrics_gtu7.png&#038;#8221<\/a>; alt=&#8221;TDD Process&#8221; title_text=&#8221;undraw_metrics_gtu7&#8243; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">This is very useful in situations in which we inject one object into another and then we need to verify that some functions of the first object have been called. Finally, whenever we want to reset some of these internal counters we can use <em>reset_mock<\/em>. Note that this way we are only <strong>resting interactions<\/strong>, but not the configuration itself:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfcmVzZXRfbW9jayhzZWxmKToKICAgIG0gPSBtb2NrLk1vY2soKQogICAgbS5jYWxsZWRfZnVuY3Rpb24ucmV0dXJuX3ZhbHVlID0gMTEKICAgIG0uY2FsbGVkX2Z1bmN0aW9uKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uLnJlc2V0X21vY2soKTsKICAgIHNlbGYuYXNzZXJ0RXF1YWwobS5jYWxsZWRfZnVuY3Rpb24uY2FsbF9jb3VudCwgMCk=&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfcmVzZXRfbW9jayhzZWxmKToKICAgIG0gPSBtb2NrLk1vY2soKQogICAgbS5jYWxsZWRfZnVuY3Rpb24ucmV0dXJuX3ZhbHVlID0gMTEKICAgIG0uY2FsbGVkX2Z1bmN0aW9uKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uKCkKICAgIG0uY2FsbGVkX2Z1bmN0aW9uLnJlc2V0X21vY2soKTsKICAgIHNlbGYuYXNzZXJ0RXF1YWwobS5jYWxsZWRfZnVuY3Rpb24uY2FsbF9jb3VudCwgMCk=[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Ok, these are some <strong>simple behaviors<\/strong> that we can do with <em>Mock<\/em> class. However, we still haven&#8217;t answered a question of how we can mock classes that are we wrote or classes that come from some third-party library. These things are done using patching.<\/p>\n<p>[\/et_pb_text][et_pb_text module_id=&#8221;pa&#8221; _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h2 style=\"text-align: justify;\">6. Patching<\/h2>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\"><strong>Patching <\/strong>is quite simple. Basically, we use it to mock parts of your system that shouldn&#8217;t be called in our unit tests. In a nutshell, we isolate unit that we want to test. We can mock pretty much anything this way. Let&#8217;s we observe this piece of code inside of <em>path.py<\/em> file:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IG9zCgpkZWYgY3VycmVudF9wYXRoKCk6CiAgICB2YWx1ZSA9IG9zLmdldGN3ZCgpCiAgICBwcmludCh2YWx1ZSkKICAgIHJldHVybiB2YWx1ZQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IG9zCgpkZWYgY3VycmVudF9wYXRoKCk6CiAgICB2YWx1ZSA9IG9zLmdldGN3ZCgpCiAgICBwcmludCh2YWx1ZSkKICAgIHJldHVybiB2YWx1ZQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">In our unit tests for <em>current_path<\/em> function, we don&#8217;t really want to call <em>os<\/em> method because it is not essential for our functionality, and also the return<br \/>value might differ between environments. This is some third-party\/import we talked about which we want to simulate and remove that particular <strong>dependency<\/strong>. This is where the <g class=\"gr_ gr_94 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep\" id=\"94\" data-gr-id=\"94\"><em>patch<\/em><\/g> method gets into the picture:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfcGF0Y2hfaW1wb3J0KHNlbGYpOgogICAgd2l0aCBtb2NrLnBhdGNoKCdwYXRoLm9zJykgYXMgb3NfbW9ja2VkOgogICAgICAgIGN1cnJlbnRfcGF0aCgpCiAgICAgICAgb3NfbW9ja2VkLmdldGN3ZC5hc3NlcnRfY2FsbGVkX29uY2UoKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfcGF0Y2hfaW1wb3J0KHNlbGYpOgogICAgd2l0aCBtb2NrLnBhdGNoKCdwYXRoLm9zJykgYXMgb3NfbW9ja2VkOgogICAgICAgIGN1cnJlbnRfcGF0aCgpCiAgICAgICAgb3NfbW9ja2VkLmdldGN3ZC5hc3NlcnRfY2FsbGVkX29uY2UoKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Using the <em>patch<\/em> method, we create a mock object that will handle this call &#8211; <em>os_mocked<\/em>. Note the way we called patch on <em>&#8216;path.os&#8217;<\/em>. This way we can mock any object from any <em>import<\/em>. The other way we can write this is:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;QG1vY2sucGF0Y2goJ3BhdGgub3MnKSAgICAgICAgCmRlZiB0ZXN0X3BhdGNoX2ltcG9ydDIoc2VsZiwgb3NfbW9ja2VkKToKICAgIGN1cnJlbnRfcGF0aCgpCiAgICBvc19tb2NrZWQuZ2V0Y3dkLmFzc2VydF9jYWxsZWRfb25jZSgp&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]QG1vY2sucGF0Y2goJ3BhdGgub3MnKSAgICAgICAgCmRlZiB0ZXN0X3BhdGNoX2ltcG9ydDIoc2VsZiwgb3NfbW9ja2VkKToKICAgIGN1cnJlbnRfcGF0aCgpCiAgICBvc19tb2NrZWQuZ2V0Y3dkLmFzc2VydF9jYWxsZWRfb25jZSgp[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Pay attention to the additional parameter that we pass into the test method. This additional parameter holds our <strong>mock object<\/strong>. In this article, we will use this convention. The same approach we can use if we want to mock <strong>context managers<\/strong>. Let&#8217;s extend our <em>path.py<\/em> with the function for reading from a file:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;aW1wb3J0IG9zCgpkZWYgY3VycmVudF9wYXRoKCk6CiAgICByZXR1cm4gb3MuZ2V0Y3dkKCkKCmRlZiByZWFkX2ZpbGUoKToKICAgIHdpdGggb3BlbigndGVzdC50eHQnKSBhcyBmaWxlOgogICAgICAgIGNvbnRlbnRzID0gZmlsZS5yZWFkKCkKICAgICAgICAKICAgIHJldHVybiBjb250ZW50cw==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]aW1wb3J0IG9zCgpkZWYgY3VycmVudF9wYXRoKCk6CiAgICByZXR1cm4gb3MuZ2V0Y3dkKCkKCmRlZiByZWFkX2ZpbGUoKToKICAgIHdpdGggb3BlbigndGVzdC50eHQnKSBhcyBmaWxlOgogICAgICAgIGNvbnRlbnRzID0gZmlsZS5yZWFkKCkKICAgICAgICAKICAgIHJldHVybiBjb250ZW50cw==[\/et_pb_dmb_code_snippet][et_pb_image src=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2020\/03\/undraw_personal_goals_edgd.png&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2020\/03\/undraw_personal_goals_edgd.png&#038;#8221<\/a>; alt=&#8221;TDD Process&#8221; title_text=&#8221;undraw_personal_goals_edgd&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.4.6&#8243; max_height=&#8221;420px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">Now, we want to mock <em>open<\/em> context manager, because we don&#8217;t want to really open the file in our unit test. This can take a while and we can load a lot of data in memory. So, we can do something like this:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;QG1vY2sucGF0Y2goJ3BhdGgub3BlbicpICAgICAgICAKZGVmIHRlc3RfcGF0Y2hfY29udGV4dF9tYW5hZ2VyKHNlbGYsIG9wZW5fbW9ja2VkKToKICAgIG9wZW5fbW9ja2VkLnJldHVybl92YWx1ZS5fX2VudGVyX18ucmV0dXJuX3ZhbHVlID0gU3RyaW5nSU8oJ3Rlc3QnKQogICAgc2VsZi5hc3NlcnRFcXVhbChyZWFkX2ZpbGUoKSwgJ3Rlc3QnKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]QG1vY2sucGF0Y2goJ3BhdGgub3BlbicpICAgICAgICAKZGVmIHRlc3RfcGF0Y2hfY29udGV4dF9tYW5hZ2VyKHNlbGYsIG9wZW5fbW9ja2VkKToKICAgIG9wZW5fbW9ja2VkLnJldHVybl92YWx1ZS5fX2VudGVyX18ucmV0dXJuX3ZhbHVlID0gU3RyaW5nSU8oJ3Rlc3QnKQogICAgc2VsZi5hc3NlcnRFcXVhbChyZWFkX2ZpbGUoKSwgJ3Rlc3QnKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">We use <em>StringIO<\/em> as a return value of <em>open<\/em> context manager mock. There\u2019s nothing we haven&#8217;t already seen here except the <em>__enter__<\/em>method.<\/p>\n<p style=\"text-align: justify;\"><!-- \/divi:paragraph --> <!-- divi:paragraph --><\/p>\n<p style=\"text-align: justify;\">Patching can be applied to the classes from our system as well. If you remember, in the previous chapter, we developed some funny Rick and Morty classes. One of the functionalities was that we can assign Morty to a Rick. Here is how those classes look like, once again:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;Y2xhc3MgUmljayhvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHVuaXZlcnNlKToKICAgICAgICBzZWxmLnVuaXZlcnNlID0gdW5pdmVyc2UKICAgICAgICBzZWxmLm1vcnR5ID0gTm9uZQogICAgICAgIHNlbGYuaXNfcGlja2xlID0gRmFsc2UKICAgICAgICAKICAgIGRlZiBhc3NpZ24oc2VsZiwgbW9ydHkpOgogICAgICAgIHNlbGYubW9ydHkgPSBtb3J0eQogICAgICAgIG1vcnR5LmlzX2Fzc2lnbmVkID0gVHJ1ZQoKY2xhc3MgTW9ydHkob2JqZWN0KToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCB1bml2ZXJzZSk6CiAgICAgICAgc2VsZi51bml2ZXJzZSA9IHVuaXZlcnNlCiAgICAgICAgc2VsZi5pc19hc3NpZ25lZCA9IEZhbHNl&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]Y2xhc3MgUmljayhvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHVuaXZlcnNlKToKICAgICAgICBzZWxmLnVuaXZlcnNlID0gdW5pdmVyc2UKICAgICAgICBzZWxmLm1vcnR5ID0gTm9uZQogICAgICAgIHNlbGYuaXNfcGlja2xlID0gRmFsc2UKICAgICAgICAKICAgIGRlZiBhc3NpZ24oc2VsZiwgbW9ydHkpOgogICAgICAgIHNlbGYubW9ydHkgPSBtb3J0eQogICAgICAgIG1vcnR5LmlzX2Fzc2lnbmVkID0gVHJ1ZQoKY2xhc3MgTW9ydHkob2JqZWN0KToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCB1bml2ZXJzZSk6CiAgICAgICAgc2VsZi51bml2ZXJzZSA9IHVuaXZlcnNlCiAgICAgICAgc2VsZi5pc19hc3NpZ25lZCA9IEZhbHNl[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">As a reminder here is how the test in Rick tests for assignment process looks like:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfYXNzaW5nX21vcnR5KHNlbGYpOgogICAgcmljayA9IFJpY2soMTExKQogICAgbW9ydHkgPSBNb3J0eSgxMTEpCgogICAgcmljay5hc3NpZ24obW9ydHkpCgogICAgc2VsZi5hc3NlcnRFcXVhbChyaWNrLm1vcnR5LCBtb3J0eSkKICAgIHNlbGYuYXNzZXJ0VHJ1ZShtb3J0eS5pc19hc3NpZ25lZCk=&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfYXNzaW5nX21vcnR5KHNlbGYpOgogICAgcmljayA9IFJpY2soMTExKQogICAgbW9ydHkgPSBNb3J0eSgxMTEpCgogICAgcmljay5hc3NpZ24obW9ydHkpCgogICAgc2VsZi5hc3NlcnRFcXVhbChyaWNrLm1vcnR5LCBtb3J0eSkKICAgIHNlbGYuYXNzZXJ0VHJ1ZShtb3J0eS5pc19hc3NpZ25lZCk=[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">This might not be the best approach. Namely, we create objects of two different classes in the same test. Some may argue that this is more an integration test than a unit test. Others may say that this is a unit test if we define the whole assignment process as a unit. Both might be right. However, let&#8217;s say that we want to isolate functionality in the\u00a0<em>Rick<\/em> class more. That would mean that we need to mock the\u00a0<em><g class=\"gr_ gr_469 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep\" id=\"469\" data-gr-id=\"469\">Morty<\/g><\/em> object. This is how we can modify this test to better fit these principles:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;QG1vY2sucGF0Y2goJ21vcnR5Lk1vcnR5JykKZGVmIHRlc3RfYXNzaW5nX21vcnR5KHNlbGYsIG1vcnR5KToKICAgIHJpY2sgPSBSaWNrKDExMSkKCiAgICByaWNrLmFzc2lnbihtb3J0eSkKCiAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2subW9ydHksIG1vcnR5KQogICAgc2VsZi5hc3NlcnRUcnVlKG1vcnR5LmlzX2Fzc2lnbmVkKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]QG1vY2sucGF0Y2goJ21vcnR5Lk1vcnR5JykKZGVmIHRlc3RfYXNzaW5nX21vcnR5KHNlbGYsIG1vcnR5KToKICAgIHJpY2sgPSBSaWNrKDExMSkKCiAgICByaWNrLmFzc2lnbihtb3J0eSkKCiAgICBzZWxmLmFzc2VydEVxdWFsKHJpY2subW9ydHksIG1vcnR5KQogICAgc2VsZi5hc3NlcnRUcnVlKG1vcnR5LmlzX2Fzc2lnbmVkKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p>In a nutshell, we mock <em>Morty<\/em> object using the <em>patch<\/em> <g class=\"gr_ gr_21 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-del replaceWithoutSep\" id=\"21\" data-gr-id=\"21\">co<\/g>ncept, and checked all the same things using this mock object.<\/p>\n<p style=\"text-align: justify;\"><!-- \/divi:paragraph --> <!-- divi:paragraph --><\/p>\n<p>Another cool thing that we can do with <em>Mock<\/em> class is to mock just certain aspects of some class, ie. <strong>mock partially<\/strong>. Let&#8217;s mock just the mentioned <em>assign<\/em> method in <em>Rick<\/em> class:<\/p>\n<p>[\/et_pb_text][et_pb_dmb_code_snippet code=&#8221;ZGVmIHRlc3RfcGFydGlhbF9tb2NrKHNlbGYpOgogICAgd2l0aCBtb2NrLnBhdGNoLm9iamVjdChSaWNrLCAnYXNzaWduJywgcmV0dXJuX3ZhbHVlPTMzMyk6CiAgICAgICAgcmljayA9IFJpY2soMTExKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmljay5hc3NpZ24oKSwgMzMzKQ==&#8221; language=&#8221;python&#8221; linenums=&#8221;on&#8221; _builder_version=&#8221;4.4.6&#8243;]ZGVmIHRlc3RfcGFydGlhbF9tb2NrKHNlbGYpOgogICAgd2l0aCBtb2NrLnBhdGNoLm9iamVjdChSaWNrLCAnYXNzaWduJywgcmV0dXJuX3ZhbHVlPTMzMyk6CiAgICAgICAgcmljayA9IFJpY2soMTExKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmljay5hc3NpZ24oKSwgMzMzKQ==[\/et_pb_dmb_code_snippet][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p>This way you can isolate units even further.<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<h2 style=\"text-align: justify;\">Conclusion<\/h2>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p style=\"text-align: justify;\">In this article, we went through several concepts. We explored what kind of automated tests exists. We focused on the unit tests since they are the backbone of Test Driven Development, which we also explained. Finally, we implemented one solution using this technique. You might notice that we haven\u2019t done a lot of refactoring because the examples were pretty straightforward. However, we could notice how this way of development is driving our implementation, and how it forces us to write clean and testable code.<\/p>\n<p style=\"text-align: justify;\">Mock objects are an essential step if we want to properly isolate units that we want to test. With that, it is crucial to Test Driven Development. In this article, we got a chance to see how the\u00a0<em>Mock<\/em> class from the <em><g class=\"gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling\" id=\"7\" data-gr-id=\"7\">unittest<\/g><\/em> module works and how to utilize it. We also had a chance to see various scenarios of mocking and how the <em>patch<\/em> method is used in these situations. We explored the ways to mock third-party classes, context managers, classes that we wrote, and even how to mock some classes partially.<\/p>\n<p style=\"text-align: justify;\"><!-- \/divi:paragraph --> <!-- divi:paragraph --><\/p>\n<p style=\"text-align: justify;\">Thank you for reading!<\/p>\n<p>[\/et_pb_text][et_pb_team_member name=&#8221;Nikola M. Zivkovic&#8221; position=&#8221;CAIO at Rubik&#8217;s Code&#8221; image_url=&#8221;<a href=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/04\/LAD08605-1-scaled.jpg&#038;#8221\">https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/04\/LAD08605-1-scaled.jpg&#038;#8221<\/a>; twitter_url=&#8221;<a href=\"https:\/\/twitter.com\/NMZivkovic&#038;#8221\" rel=\"nofollow\">https:\/\/twitter.com\/NMZivkovic&#038;#8221<\/a>; linkedin_url=&#8221;<a href=\"https:\/\/www.linkedin.com\/in\/nmzivkovic\/&#038;#8221\" rel=\"nofollow\">https:\/\/www.linkedin.com\/in\/nmzivkovic\/&#038;#8221<\/a>; _builder_version=&#8221;4.4.6&#8243;]<\/p>\n<p>Nikola M. Zivkovic a CAIO at <a href=\"rubikscode.net\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Rubik&#8217;s Code<\/strong><\/a> and the author of books: <a href=\"https:\/\/rubikscode.net\/ultimate-guide-to-machine-learning-with-python\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Ultimate Guide to Machine Learning<\/strong><\/a> and <a href=\"https:\/\/rubikscode.net\/deep-learning-for-programmers\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Deep Learning for Programmers<\/strong><\/a>. He loves knowledge sharing, and he is an experienced speaker. You can find him speaking at <span>meetups, conferences, and as a guest lecturer at the University of Novi Sad.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>[\/et_pb_team_member][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[et_pb_section fb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221; _builder_version=&#8221;3.22&#8243;][et_pb_row admin_label=&#8221;row&#8221; _builder_version=&#8221;3.25&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221;][et_pb_column type=&#8221;4_4&#8243; _builder_version=&#8221;3.25&#8243; custom_padding=&#8221;|||&#8221; custom_padding__hover=&#8221;|||&#8221;][et_pb_code _builder_version=&#8221;4.4.6&#8243;] The code that accompanies this article can be received after subscription * indicates required Email Address * [\/et_pb_code][et_pb_text _builder_version=&#8221;4.4.6&#8243;] Bugs! Every application has it and no matter how careful you are you will create one sooner or later. Especially if you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":17267,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"<!-- wp:paragraph -->\n<p>Bugs! Every application has it and no matter how careful you are you will create one sooner or later. Especially if you are working on a large enterprise software. Now, I know a bunch of people will have \"My code is clean and bugless\" attitude, but creating bug free code is extremely hard if not impossible. However, that doesn't mean that you should give up and just write spaghetti code that just doesn't work. We - <strong>developers<\/strong>, should give our best to write <strong>high-quality code<\/strong>. High-quality means a low number of bugs, among other things. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Apart from that, having a bug in production is extremely expensive. You probably know that comparison, where bug found during development is <strong>100 times cheaper<\/strong> than finding the same bug during production. So, we should focus on finding our bugs as soon as possible. Our first line of defense is <strong>testing<\/strong>.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":8983,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/1.png\" alt=\"\" class=\"wp-image-8983\"\/><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>We could test our applications manually, just by running them and clicking around. However, this approach has many <strong>pitfalls<\/strong>. First one is that it is time-consuming, which basically means expensive. This way of testing makes regression testing extremely hard too. Imagine that you've just added a new feature into your application. You will have to make sure that this new feature didn't break any of the old functionalities, which means testing your whole application from the beginning. Again, time-consuming and costly. What is the solution? <strong>Automated testing<\/strong> of course.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>Automated T<g class=\"gr_ gr_15 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"15\" data-gr-id=\"15\">ests<\/g><\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:image {\"id\":8985} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/4.png\" alt=\"\" class=\"wp-image-8985\"\/><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>As we could see, manual testing is not really working for us especially if we want to detect issues early during development phase. So, we decided to automate our tests. Based on the level of abstraction that tests are done they can be:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list -->\n<ul><li><strong>Unit Tests<\/strong> -  It is a piece of a code that invokes another piece of code (unit) and checks if an output of that action is the same as the desired output. <\/li><li><strong>Integration Tests<\/strong> - It is testing a unit without full control over all parties in the test. They are using one or more of its outside dependencies, such as a database, threads, network, time, etc.<\/li><li><strong>Functional Tests<\/strong> - It is testing a slice of a functionality of the system, observing it as a black box and verifying the output of the system to the functional specification.<\/li><li><strong>Acceptance Tests<\/strong> - It is testing does the system fulfill expected business and contract requirements. It is considering the system as a black box.<\/li><\/ul>\n<!-- \/wp:list -->\n\n<!-- wp:image {\"id\":8984,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/2.png\" alt=\"\" class=\"wp-image-8984\"\/><figcaption>Pyramid of Tests<\/figcaption><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>These definitions are a bit lose, but you get the point. In the image above, you can see the so-called Pyramid of Tests. It displays the number of tests that we should have in our application per type of test. We can see that we have the largest number of unit tests. For the purpose of this article, we will consider only this type of tests, since they are crucial for the TDD process, as we will see in a little bit. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>Unit Tests<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:image {\"id\":8986} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/5.png\" alt=\"\" class=\"wp-image-8986\"\/><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>As previously mentioned unit tests are testing the functionality of a unit. This brings us to a philosophical question about what exactly is \"unit\"? A <strong>unit <\/strong>is the set of actions between the invocation of a method in the system and a single noticeable output of that system. The equally philosophical answer, right? The important thing to understand here is that the unit test is a piece of code that tests another piece of code. Over the years, this type of tests turned out to be one of the <strong>best tools<\/strong> for increasing software quality. They were introduced by Kent Back in Smalltalk back in the 1970s and since then they are used in pretty much any programming language.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>So, how can we write unit tests in Python? Unit tests are always written using some sort of unit test framework. For Python that is module <em class=\"\"><g class=\"gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"6\" data-gr-id=\"6\">unittest<\/g><\/em>. Here is how we can use this module to write our first tests:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/e161ca4d83b39105bc94cb50f7fcf829<\/p>\n\n<!-- wp:paragraph -->\n<p>First, we create class <em>FirstTestClass<\/em>, which is inheriting <em>TestCase<\/em> from the <em><g class=\"gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling\" id=\"4\" data-gr-id=\"4\">unittest<\/g><\/em> module. Using this inheritance we are defining test class which contains our tests methods or test cases. These test cases will be registered within <em><g class=\"gr_ gr_968 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling\" id=\"968\" data-gr-id=\"968\"><g class=\"gr_ gr_971 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep\" id=\"971\" data-gr-id=\"971\">unittest<\/g><\/g><\/em> module and we will be able to run them later. In this class, we are having only one test case <em>test_upper<\/em>. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>This method uses function <em>asserEqual<\/em> to verify that the call of the <em>upper<\/em> method on the string really returns the same string with all caps. The module <em><g class=\"gr_ gr_164 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"164\" data-gr-id=\"164\">unittest<\/g><\/em> has a lot of these functions that start with the word <em>assert<\/em>.  <br>Essentially, every test method should call one of these methods to verify the results and so the test runner can accumulate all test results and produce a report. Finally, at the end of this file, we are calling <em>unitest.main<\/em>. This will run all registered tests. Here is what we get when we run this:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> E:python_tdd&gt;python simple_unit_tests.py<br> --------------------------------------------------<br> Ran 1 test in 0.000s<br> OK<br><\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>If we want to know which tests cases are called you can just add <em>-v<\/em> as an argument:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> <br> E:python_tdd&gt;python simple_unit_tests.py -v<br> test_upper (<strong>main<\/strong>.FirstTestClass) \u2026 ok<br> -------------------------------------------------- <br> Ran 1 test in 0.000s<br> OK <\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>As we can see we run our one test case, and got the result that it passes, ie. the condition that we check with <em>assertEqual<\/em> is true. Congratulations, you've just run your first test with Python! Now, let's see how we can test some functionality that we made. Take a look at this code:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/d7d0764e70053a69f97ad8c4c8a2b8ef<\/p>\n\n<!-- wp:paragraph -->\n<p>It is very simple function <em>get_greetings<\/em> which is just returning <em>'Hello World!'<\/em> string. This is how we test it:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/600d224160e598bfe325a83d24a1b8c9<\/p>\n\n<!-- wp:paragraph -->\n<p>Pretty easy, right? We just import function from the file, write <g class=\"gr_ gr_10 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep\" id=\"10\" data-gr-id=\"10\">class<\/g> that inherits <em><g class=\"gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"5\" data-gr-id=\"5\">unittest<\/g>.TestCase<\/em> and verify the result using <em>assertEqual<\/em> within test method <em>test_get_helloworld<\/em>. Output looks like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> E:python_tdd&gt;python tests_helloworld.py -v<br> test_get_helloworld (<strong>main<\/strong>.HelloworldTests) \u2026 ok<br> -------------------------------------------------- <br> Ran 1 test in 0.001s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:heading -->\n<h2>What is Test Driven Development?<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:image {\"id\":8987} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/6.png\" alt=\"\" class=\"wp-image-8987\"\/><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p><a rel=\"noreferrer noopener\" aria-label=\"Test Driven Development (TDD) (opens in a new tab)\" href=\"https:\/\/www.packtpub.com\/application-development\/introducing-test-driven-development-c-video\" target=\"_blank\">Test Driven Development (TDD)<\/a> is an evolutionary approach to building and designing software solutions. It is consisting of small cycles in which we are writing a unit test, that will initially fail, and then implementing the minimum amount of code to pass that test. After that code can be refactored to follow some good principles. Refactoring has a safety net, because we wrote the tests already, so we can reshape our solution stress-free. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>These three important steps of TDD are easy to remember by using something that I like to call the <strong>TDD mantra<\/strong>. It goes like this: <strong>Red - Green - Refactor<\/strong>. Red is corresponding with the phase in which we write a test that will fail. Then we implement the code to make previously written test pass meaning it is - Green. And finally we refactor our code - and we don't really have a color for that one. So there you go, TDD Mantra - Red, Green, Refactor.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":8991,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/TDD-Python-2.png\" alt=\"\" class=\"wp-image-8991\"\/><figcaption>TDD Process | TDD Mantra<\/figcaption><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>You might wonder what is the difference between just writing unit tests for your code and TDD? In general, we are using unit tests in both cases. The crucial difference between TDD and traditional testing is <strong>the moment<\/strong> in which we are writing the tests. When we are writing code using TDD we first write the tests and then the code itself, and not another way around.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>The benefit of this approach is that we are minimizing the possibility of forgetting to write tests for some part of the code. Ideally, we end up with the code that is fully tested upfront and solutions that are implemented using TDD usually have 90%-100% of code covered with tests. Of course, when our code is tested it is less likely that we have a bug in our system. Another important difference is that we are writing <strong>small chunks of code<\/strong> to satisfy our test. This way the process itself drives our design and forces us to keep things simple. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>By using TDD we avoid creating over complicated designs and overengineered systems. Arguably this is the <strong>biggest benefit<\/strong> of this approach. When we use it we end up with clearer design and API. This approach also forces you to design classes properly and to follow good code principles like <a rel=\"noreferrer noopener\" aria-label=\"SOLID (opens in a new tab)\" href=\"https:\/\/rubikscode.net\/2017\/05\/07\/single-responsibility-principle-on-different-levels-of-abstraction\/\" target=\"_blank\">SOLID<\/a> and DRY. Personally, I find this way of development as a great procrastination killer and a great <strong>motivator<\/strong>. It kinda keeps you in the zone.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>Solving a problem using TDD<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:image {\"id\":8988} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/7.png\" alt=\"\" class=\"wp-image-8988\"\/><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>Before we proceed let's examine what kind of problem we are trying to solve. Do you guys like TV show <em>Rick and Morty<\/em>? I love it. The show follows the adventures of cynical mad scientist Rick Sanchez and his grandson Morty Smith. Rick owns a portal gun and takes Morty to different dimensions\/universes. Different versions of these characters inhabit those other dimensions. The Citadel is the place where Ricks and Mortys have formed a society built by their counterparts from an infinite amount of realities. We are in luck because we have a request from The Citadel for one Python module. Here are the user stories:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list -->\n<ul><li>A user is able to assign Ricks and Mortys a universe number<\/li><li>A user is able to add residents to the Citadel<\/li><li>A user is able to turn all Ricks with assigned Mortys to pickles (watch s03e03)<\/li><\/ul>\n<!-- \/wp:list -->\n\n<!-- wp:image {\"id\":8989} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/3.png\" alt=\"\" class=\"wp-image-8989\"\/><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>Ok, let's start from the first user story and work our TDD magic to the last user story. The first user story tells us that we should have two classes, one for Rick and one for Morty. However, since we are using TDD, we write the unit tests first. We implement <em>Rick<\/em> test class like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/bd5b7b29b42d43456801ef2dc53473f6<\/p>\n\n<!-- wp:paragraph -->\n<p>Of <g class=\"gr_ gr_3 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep\" id=\"3\" data-gr-id=\"3\">course<\/g> if we run this test we will get an error saying that Rick class is not existing:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">======================================================================<br> ERROR: test_universe (<strong>main<\/strong>.RickTests)<br> Traceback (most recent call last):<br>   File \"rick_tests.py\", line 5, in test_universe<br>     rick = Rick(111)<br> NameError: name 'Rick' is not defined<br> <br> Ran 1 test in 0.001s<br> FAILED (errors=1)<br><\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>We need to define the class and initialize it through the constructor with the value for the <em>universe<\/em>:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/efc10faf1d47afae547e6a93c0f2597a<\/p>\n\n<!-- wp:paragraph -->\n<p>Now, when we re-run the tests, we get this:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> test_universe (<strong>main<\/strong>.RickTests) \u2026 ok<br> --------------------------------------------------  <br> Ran 1 test in 0.000s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>We are following the same pattern for Morty. First the failing test:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/a3462a3bedf28672d0d8d5fa56efcf88<\/p>\n\n<!-- wp:paragraph -->\n<p>Followed by the implementation:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/4d3a243f423443b8c9b12f0c400d7e2d<\/p>\n\n<!-- wp:paragraph -->\n<p>And passed test:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> test_universe (<strong>main<\/strong>.MortyTests) \u2026 ok<br> --------------------------------------------------  <br> Ran 1 test in 0.000s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:image {\"id\":8994,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/giphy.gif\" alt=\"\" class=\"wp-image-8994\"\/><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>You might notice that this \"dance\" seems unnatural at first. It takes some time to get used to it, but once you do it is enchanting. You will wonder how you were able to do it another way for years. Ok, so we implemented our first user story. Let's move onto the second one. This one is pretty easy as well - \"A user is able to add residents to the Citadel\". However, if we want to add residents, this means that <em>Citadel<\/em> class should have some sort of the list or array of residents. Let's first make a function that will return all residents. We write a test for <em>Citadel<\/em> class:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/0e6af5fe96c2c4dcb5eb983e8b55ec15<\/p>\n\n<!-- wp:paragraph -->\n<p>This test fails, because <em>Citadel<\/em> implementation doesn't exist yet. Since this seems a little bit more complicated than previous implementations, we write something like this and try to make our test pass:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/44a84e71aa4b6801d48378a0613d5e47<\/p>\n\n<!-- wp:paragraph -->\n<p>As you can see, we defined private field <em>__residents__<\/em> and added method <em>get_all_residents<\/em> which is not doing anything at the moment. This way we can run our test, but it fails again:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">======================================================================<br> ERROR: test_get_all_residents (<strong>main<\/strong>.CitadelTests)<br> Traceback (most recent call last):<br>   File \"citadel_tests.py\", line 10, in test_get_all_residents<br>     self.assertCountEqual(residents, 0)<br>   File \"C:UsersNikolaVanjaAnaconda3libunittestcase.py\", line 1165, in assertCountEqual<br>     first_seq, second_seq = list(first), list(second)<br> TypeError: 'NoneType' object is not iterable<br> --------------------------------------------------  <br> Ran 1 test in 0.002s<br><br> FAILED (errors=1)<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>So, in order to fix this we have to return that private field through the method:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/d8232cf71652f405f503accddd126780<\/p>\n\n<!-- wp:paragraph -->\n<p>Re-run the test:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> test_get_all_residents (<strong>main<\/strong>.CitadelTests) \u2026 ok<br> --------------------------------------------------  <br> Ran 1 test in 0.001s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>Hooray! Test passes and we are making progress. Still, functionality that satisfies second user story is not implemented. That is why we write another test so that the complete <em>Citadel <\/em>test class now looks like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/7fc3bae797487fb331a96b22555cdeb4<\/p>\n\n<!-- wp:paragraph -->\n<p>Running this will fail, because we don't have <em>add_residents<\/em> method. So, let's implement it:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/7bb51327f4eb6af2c3fe72c2bf123ebf<\/p>\n\n<!-- wp:paragraph -->\n<p>Now when we run the test we get this:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> test_add_resident (<strong>main<\/strong>.CitadelTests) \u2026 ok<br> test_get_all_residents (<strong>main<\/strong>.CitadelTests) \u2026 ok<br> --------------------------------------------------  <br> Ran 2 tests in 0.002s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:image {\"id\":8995,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/i0.wp.com\/rubikscode.net\/wp-content\/uploads\/2019\/03\/Z4aV.gif?fit=720%2C406&amp;ssl=1\" alt=\"\" class=\"wp-image-8995\"\/><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>Awesome! We finished two out of three user stories. However, the last one is the trickiest. Let's examine it - A user is able to turn all Ricks with assigned Mortys to pickles. We can detect several tasks within this one sentence. We should be able to assign Morty to a Rick, meaning we need to extend both of those classes. We should be able to turn Rick into a pickle, as well. Finally, we should be able to do that for all Ricks in the Citadel with assigned Morties. So, let's proceed in that order. First, we extend Morty with <em>is_assigned<\/em> field. Extended <em>Morty<\/em> test class looks like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/f3b4c9c9ff7417d99c45307e84c785d7<\/p>\n\n<!-- wp:paragraph -->\n<p>The new test of the class will fail. We have to extend the <em>Morty<\/em> class implementation as well:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/e16edc8863b6c01eae8ec7cac25e6ee7<\/p>\n\n<!-- wp:paragraph -->\n<p>Run the test again:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> test_is_assigned (<strong>main<\/strong>.MortyTests) \u2026 ok<br> test_universe (<strong>main<\/strong>.MortyTests) \u2026 ok<br> --------------------------------------------------  <br> Ran 2 tests in 0.002s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>Ok, we are getting closer. Now <em>Rick<\/em> class should be extended so <em>Morty<\/em> can be assigned to <em>Rick.<\/em> We extend the <em>Rick<\/em> test class:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/7507a531e40bbfdcd21a9839522beadd<\/p>\n\n<!-- wp:paragraph -->\n<p><g class=\"gr_ gr_4 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep\" id=\"4\" data-gr-id=\"4\">Test<\/g> is failing because we are missing <em><g class=\"gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace\" id=\"8\" data-gr-id=\"8\">morty<\/g><\/em> field in <em>Rick<\/em> class. Let's extend <em>Rick<\/em>:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/dd6ab990e0b8f9444b85934f8d297a43<\/p>\n\n<!-- wp:paragraph -->\n<p>Ignite tests again:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> test_has_morty (<strong>main<\/strong>.RickTests) \u2026 ok<br> test_universe (<strong>main<\/strong>.RickTests) \u2026 ok<br> --------------------------------------------------  <br> Ran 2 tests in 0.001s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>Nice, now let's extend tests for <em>assign<\/em> method, through which we will assign Morty to Rick:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/f2559246fda7918baec80848a082a677<\/p>\n\n<!-- wp:paragraph -->\n<p>As you can see we are checking two things after Morty is assigned. When we extend <em>Rick<\/em> class to support these changes it looks like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/a1736a2c576b23aafe4318a4e982a2d8<\/p>\n\n<!-- wp:paragraph -->\n<p>And when we re-run the tests for <em>Rick<\/em> class:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> test_assing_morty (<strong>main<\/strong>.RickTests) \u2026 ok<br> test_has_morty (<strong>main<\/strong>.RickTests) \u2026 ok<br> test_universe (<strong>main<\/strong>.RickTests) \u2026 ok<br> --------------------------------------------------  <br> Ran 3 tests in 0.002s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:image {\"id\":8998,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/giphy-2.gif\" alt=\"\" class=\"wp-image-8998\"\/><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>Don't give up on me now, we are halfway through the third user story! We need to make Rick \"pickable\" and turn all Ricks with assigned Mortys in the Citadel into pickles (I never thought I would write down a sentence like this :)). To the <em>Rick<\/em> test class!<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/9bcc490147bfd3ac07b21fe74f220c73<\/p>\n\n<!-- wp:paragraph -->\n<p>This seems familiar. Test <em>test_has_is_pickle<\/em> fails because, well, <em>Rick<\/em> class still has no field <em>is_pickle<\/em>. <em>Rick<\/em> class needs to be extended for that:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/8beaa93020d2cb53fdd3041dac4aa337<\/p>\n\n<!-- wp:paragraph -->\n<p>Run the tests again:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> test_assing_morty (<strong>main<\/strong>.RickTests) \u2026 ok<br> test_has_is_pickle (<strong>main<\/strong>.RickTests) \u2026 ok<br> test_has_morty (<strong>main<\/strong>.RickTests) \u2026 ok<br> test_universe (<strong>main<\/strong>.RickTests) \u2026 ok<br> --------------------------------------------------  <br> Ran 4 tests in 0.003s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>Awesome! Now, to the <em>Citadel<\/em> test class. We add one large test:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/d5c266383ad53ef4efa9149545c9e51d<\/p>\n\n<!-- wp:paragraph -->\n<p>Here is the explanation. We create all necessary objects, assign Morty to Rick, add both objects into Citadel and call method that should turn all Ricks with Mortys into pickles. Cool, let's reflect that in <em>Citadel<\/em> class implementation:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/ecf8bbc0bb7a16b736c8c959e8f7fa2e<\/p>\n\n<!-- wp:paragraph -->\n<p>Alright, let's re-run the tests:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"> test_add_resident (<strong>main<\/strong>.CitadelTests) \u2026 ok<br> test_get_all_residents (<strong>main<\/strong>.CitadelTests) \u2026 ok<br> test_picle_ricks_with_morties (<strong>main<\/strong>.CitadelTests) \u2026 ok<br> --------------------------------------------------  <br> Ran 3 tests in 0.002s<br> OK<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>Woooohoooo! The tests are passing and we completed our third and final user story!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":9000,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/03\/anigif_sub-buzz-22010-1521220575-14.gif\" alt=\"\" class=\"wp-image-9000\"\/><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:heading -->\n<h2>Conclusion<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>In this article, we went through several concepts. We explored what kind of automated tests exists. We focused on the unit tests since they are the backbone of Test Driven Development, which we also explained. Finally, we implemented one solution using this technique. You might notice that we haven't done a lot of refactoring because the examples were pretty straight forward. However, we could notice how this way of development is driving our implementation, and how it forces us to write clean and testable code. What we haven't explored in this article is the concept of mocking, which you should check out if you want to be proficient in TDD.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Thanks for reading!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:separator -->\n<hr class=\"wp-block-separator\"\/>\n<!-- \/wp:separator -->\n\n<!-- wp:paragraph -->\n<p> Read more posts from the author at&nbsp;<strong><a href=\"https:\/\/rubikscode.net\/\">Rubik\u2019s Code<\/a><\/strong>. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:separator -->\n<hr class=\"wp-block-separator\"\/>\n<!-- \/wp:separator -->","_et_gb_content_width":"","advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[832],"tags":[608227756,608227675,608227758,608227748,1821740,5270568,2301,608227754,608227753,608227757,608227755],"class_list":["post-8972","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-automated-tests","tag-python","tag-rick-and-morty","tag-software","tag-software-craft","tag-software-craftsmanship","tag-software-development","tag-tdd","tag-test-driven-development","tag-testing","tag-unit-tests"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/rubikscode.net\/wp-content\/uploads\/2021\/05\/tddfeatured.png","jetpack_shortlink":"https:\/\/wp.me\/p8G8I0-2kI","jetpack_likes_enabled":false,"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/posts\/8972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/comments?post=8972"}],"version-history":[{"count":99,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/posts\/8972\/revisions"}],"predecessor-version":[{"id":17379,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/posts\/8972\/revisions\/17379"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/media\/17267"}],"wp:attachment":[{"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/media?parent=8972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/categories?post=8972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/tags?post=8972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}