{"id":3688,"date":"2014-04-01T10:47:47","date_gmt":"2014-04-01T13:47:47","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=3688"},"modified":"2014-04-01T13:31:03","modified_gmt":"2014-04-01T16:31:03","slug":"improve-your-test-readability-using-the-xunit-structure","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2014\/04\/improve-your-test-readability-using-the-xunit-structure\/","title":{"rendered":"Improve your test readability using the xUnit structure"},"content":{"rendered":"
TL;DR:<\/strong> 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.<\/p>\n The 4 phases of a test: setup, exercise, verify and teardown<\/p>\n<\/div>\n One of the main reasons to write tests is to have an automated way to check if your code is doing what you expect it to do. That means, trying to verify its correctness. Your test suite acts as a safety net that guarantees your software will continue to work as expected while you refactor, build new features or fix bugs. That’s amazing! But, throughout the years, software developers discovered that tests can be even more than a safety net.<\/p>\n A test is an example of how to use your code, not just a way to verify its correctness. Seeing tests as examples of how to use your code changes a little bit the priorities you have when writing them. If the test should serve as an example, then it should be easy to read and understand. Therefore, you should also focus on test readability, not just test “executability” (I know, weird word). One way to improve readability of a piece of text (or code) is to write it in a way the readers are used to, a structure that they expect, some standard way… Let’s think about that.<\/p>\n Back at your school days, you learned that when writing an essay, you’re supposed to structure it in: introduction, body and conclusion. Why? Because that structure helps you to better express your ideas. That means, it helps the reader to understand your message. Is there any equivalent of that for automated tests writing? In fact, there is. It’s called the xUnit structure.<\/p>\n First, let’s see a test that can have its readability improved:<\/p>\n One can understand the test above, but still, it’s not easy to quickly scan the test and see that it has logical parts. Those parts would be the the xUnit phases<\/a>.<\/p>\n The standard xUnit test structure is composed of 4 phases: setup, exercise, verify and teardown.<\/p>\n Now, let’s re-organize the test above making explicit that there are different logical parts:<\/p>\n It’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 One 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>\n Using 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 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}]}}<\/p>\n
People don’t write tests to be read, they write them to be executed<\/h3>\n
Write tests as examples of how to use your code<\/h3>\n
Structure your tests using the xUnit standard<\/h3>\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 stack.push(1)\n stack.push(2)\n expect(stack.top).to eq(2)\n end\n end\nend\n<\/pre>\n
\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>\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>\n
Why care about test readability?<\/h3>\n