Teardown:<\/strong> basically where you clean up stuff in order to get your system back to the initial state.<\/li>\n<\/ul>\nNow, let’s re-organize the test above making explicit that there are different logical parts:<\/p>\n
\ndescribe Stack do\n describe \"#push\" do\n it \"puts an element at the top of the stack\" do\n # setup\n stack = Stack.new\n\n # exercise\n stack.push(1)\n stack.push(2)\n\n # verify\n expect(stack.top).to eq(2)\n end\n end\nend\n<\/pre>\nIt’s easier to scan, isn’t it?<\/p>\n
About the comments, no, we don’t need them. I added them in order to make the example clear. Let’s remove them and keep this structure:<\/p>\n
\ndescribe Stack do\n describe \"#push\" do\n it \"puts an element at the top of the stack\" do\n stack = Stack.new\n\n stack.push(1)\n stack.push(2)\n\n expect(stack.top).to eq(2)\n end\n end\nend\n<\/pre>\nOne can say that we just added two line breaks, that’s true. But that’s just the how<\/em>, not the what<\/em>. The what<\/em> is: improving test readability. The how<\/em> is: structuring the code based on the xUnit four-phase standard, by adding two line breaks. Got it?<\/p>\nUsing a standard structure to ease the communication of an idea is not something new. As an example, Rails does that when it generates a standard directory structure. When entering on a new Rails project and scanning it, you know your way and where stuff are because you already expect a defined structure and you are used to it. It’s not something completely new, you’re used to that structure. I could also say that even Ruby uses that concept when it talks about the “principle of least surprise”, but maybe I would be going too far. So, let’s get that wrapped up.<\/p>\n
Why care about test readability?<\/h3>\n
So, why should I care about all of that stuff? I mean, isn’t just having my test suite on green enough? No.<\/p>\n
Test readability will be really important in a lot of situations. Like when a test gets red, someone needs to fix it. In order to do that, one needs to understand what the test is about. If the test is well structured and easy to read, they can fix it faster.<\/p>\n
Also, if you think about your tests as examples of how to use your code, someone that is trying to use a class that you wrote, can see how it’s done in the tests. The test readability will be equally important here too.<\/p>\n
So, what about you, how do you improve your test’s quality? How do you improve your test’s readability?<\/p>\n","protected":false},"excerpt":{"rendered":"
TL;DR: test quality is not just about verifying correctly whether your code works, it’s also about making your test easy to read and understand. You can do that by structuring your test using the four-phases xUnit standard. The 4 phases of a test: setup, exercise, verify and teardown People don’t write tests to be read, … \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[96],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/3688"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=3688"}],"version-history":[{"count":47,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/3688\/revisions"}],"predecessor-version":[{"id":3847,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/3688\/revisions\/3847"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=3688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=3688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=3688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}