{"id":200,"date":"2009-06-14T00:38:28","date_gmt":"2009-06-13T23:38:28","guid":{"rendered":"http:\/\/blog.eweibel.net\/?p=200"},"modified":"2010-11-13T15:57:49","modified_gmt":"2010-11-13T14:57:49","slug":"how-to-structure-code-in-an-unit-test","status":"publish","type":"post","link":"https:\/\/blog.eweibel.net\/?p=200","title":{"rendered":"How to structure code in an unit test"},"content":{"rendered":"<p>When you create your unit tests for a method in the SUT (software under test) you will ask yourself how to structure the code in the test method.<br \/>I saw two kind of syntaxes which help to structure the code in a unit test method (well, actually there are at least three, but the third syntax is just chaos, so this is definitely not the way how to do it).<\/p>\n<p><strong>SEVT-Syntax<\/strong><\/p>\n<p>[code language=&#8221;csharp&#8221;]<br \/>\n[TestFixture]<br \/>\npublic class ThingTest<br \/>\n{<br \/>\n    [Test]<br \/>\n    public void TestSomething()<br \/>\n    {<br \/>\n        \/\/ Setup<\/p>\n<p>        \/\/ Exercise<\/p>\n<p>        \/\/ Verify<\/p>\n<p>        \/\/ Teardown<br \/>\n    }<br \/>\n}[\/code]<\/p>\n<p>I found this syntax in the <a href=\"http:\/\/www.amazon.de\/xUnit-Test-Patterns-Refactoring-Signature\/dp\/0131495054\/ref=sr_1_1?ie=UTF8&amp;s=books-intl-de&amp;qid=1244931345&amp;sr=8-1\">xUnit Test Patterns<\/a> book by Gerard Meszaros. Obviously this syntax is inspired by classic unit frameworks (like <a class=\"zem_slink\" href=\"http:\/\/junit.org\" title=\"JUnit\" rel=\"homepage\">junit<\/a> or <a class=\"zem_slink\" href=\"http:\/\/www.nunit.com\/\" title=\"NUnit\" rel=\"homepage\">nunit<\/a>). There is one thing, which could be critical: this syntax could be misused when in the teardown block exist code, which has to run always (in other words, which has to be correctly in teardown-method). But when an assert fails, the teardown block would not be invoked. To prevent this case, you have to do extra effort (try\/finally construct).<br \/>Gerard Meszaros named this usage of the syntax Four-Phase Test. He also showed, that the last phase (the teardown block) could be done by a teardown-method.<\/p>\n<p><strong>AAA-Syntax<\/strong><\/p>\n<p>[code language=&#8221;csharp&#8221;]<br \/>\n[TestFixture]<br \/>\npublic class ThingTest<br \/>\n{<br \/>\n    [Test]<br \/>\n    public void TestSomething()<br \/>\n    {<br \/>\n        \/\/ Arrange<\/p>\n<p>        \/\/ Act<\/p>\n<p>        \/\/ Assert<br \/>\n    }<br \/>\n} [\/code]<\/p>\n<p>This syntax I found on the blog of William Wake and <a href=\"http:\/\/ayende.com\/Blog\/archive\/2008\/05\/16\/Rhino-Mocks--Arrange-Act-Assert-Syntax.aspx\">ayende<\/a>. I prefer this syntax, because it sounds good and therefore I don&#8217;t forget it. I like also the words which are more precise and there are no competition with the setup- or teardown-methods. It is also clear where to put the asserts.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>The most important thing is that you structure your code in your unit-test. It is also very important that you test only one concern per test method. A syntax helps you to create readable, maintainable and specific (one test-concern per test method) tests. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you create your unit tests for a method in the SUT (software under test) you will ask yourself how to structure the code in the test method.I saw two kind of syntaxes which help to structure the code in a unit test method (well, actually there are at least three, but the third syntax is just chaos, so this is definitely not the way how to do it). SEVT-Syntax [code language=&#8221;csharp&#8221;] [TestFixture] public class ThingTest { [Test] public void&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/blog.eweibel.net\/?p=200\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13,5,11,10],"tags":[],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-net","category-good-practices","category-java","category-testing"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/plOV9-3e","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":376,"url":"https:\/\/blog.eweibel.net\/?p=376","url_meta":{"origin":200,"position":0},"title":"Mocking frameworks in .Net","author":"Patrick","date":"30. Mar 2010","format":false,"excerpt":"A few month ago I played with some mocking frameworks in .Net. There are already some comparisons available (here, here or here). In this blog post I want to show which frameworks are available and which one fits best for agile development. You could download the source code from github.com.\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":487,"url":"https:\/\/blog.eweibel.net\/?p=487","url_meta":{"origin":200,"position":1},"title":"WordPress and SyntaxHighlighter","author":"Patrick","date":"8. Jan 2010","format":false,"excerpt":"I often post code in my blog posts, so the SyntaxHighlighter by is very useful. But there are several plug-ins for WordPress, the question is which one is the most comfortable and has the most configuration options: Google Syntax Highlighter for Wordpress Syntax Highlighter and Code Colorizer for Wordpress SyntaxHighlighter\u2026","rel":"","context":"In &quot;Good practices&quot;","block_context":{"text":"Good practices","link":"https:\/\/blog.eweibel.net\/?cat=5"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76,"url":"https:\/\/blog.eweibel.net\/?p=76","url_meta":{"origin":200,"position":2},"title":"Code generation for unit testing","author":"Patrick","date":"10. Mar 2009","format":false,"excerpt":"There exists a project at Microsoft which generate unit tests based of source code. The name of this interesting project is Pex.At the first time when I heard about this project I was skeptical. Where's the value of generated tests? Doesn't it break the \"test first\" approach? After I saw\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":34,"url":"https:\/\/blog.eweibel.net\/?p=34","url_meta":{"origin":200,"position":3},"title":"Are 100% code coverage reasonable?","author":"Patrick","date":"2. Mar 2009","format":false,"excerpt":"When you use a code coverage tool one of the first question is what is a good code coverage. Recently I listened to different podcasts (stackoverflow, scott hanselman) where they discuss this topic. I wasn't really surprised that there wasn't one unique opinion.One opinion was that 100% is a good\u2026","rel":"","context":"In &quot;Software architecture&quot;","block_context":{"text":"Software architecture","link":"https:\/\/blog.eweibel.net\/?cat=4"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":303,"url":"https:\/\/blog.eweibel.net\/?p=303","url_meta":{"origin":200,"position":4},"title":"From NUnit to MSTest","author":"Patrick","date":"30. Jun 2009","format":false,"excerpt":"Last week I migrated several projects from NUnit to MSTest. The developers use the Developer version of Microsoft Visual Studio Team System, so they have integrated unit-test support for MSTest. In this post I show you all the problems and work I had to migrate the tests from NUnit to\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":942,"url":"https:\/\/blog.eweibel.net\/?p=942","url_meta":{"origin":200,"position":5},"title":"Enums and inheritance in .Net","author":"Patrick","date":"9. Feb 2011","format":false,"excerpt":"In one of my current projects I had the following code (I simplified the code a bit): public string ConnectionString { get { switch(this.Importer) { case Importer.SqlServer: return \"Server=localhost;Database=Northwind\"; case Importer.SqlServerOleDb: return\"Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind\"; default: throw new NotSupportedException( string.Format(\"Importer {0} is not supported yet.\", this.Importer)); } } } After running\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"CodeCoverage","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/CodeCoverage_thumb.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/CodeCoverage_thumb.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/CodeCoverage_thumb.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200"}],"version-history":[{"count":65,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":887,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions\/887"}],"wp:attachment":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}