<p>Unit testing and ASP.NET web applications are an ambiguous point in my group. More often than not, good testing practices fall through the cracks and web applications end up going live for several years with no tests. </p>

<p>The cause of this pain point generally revolves around the hassle of writing UI automation mid-development. </p>

<p>How do you or your organization integrate best TDD practices with web application development?</p>

[{'answer_id': 18615, 'author': 'nachojammers', 'author_id': 1911, 'author_profile': 'https://Stackoverflow.com/users/1911', 'pm_score': 2, 'selected': False, 'text': "<p>I layer out the application and at least unit test from the presenter/controller (whichever is your preference, mvc/mvp) to the data layer. That way I have good test coverage over most of the code that is written.</p>\n\n<p>I have looked at FitNesse, Watin and Selenium as options to automate the UI testing but I haven't got around to using these on any projects yet, so we stick with human testing. FitNesse was the one I was leaning toward but I couldn't introduce this as well as introducing TDD (does that make me bad? I hope not!).</p>\n"}, {'answer_id': 18624, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 2, 'selected': False, 'text': '<p>This is a good question, one that I will be subscribing too :)</p>\n\n<p>I am still relatively new to web dev, and I too am looking at a lot of code that is largely untested.</p>\n\n<p>For me, I keep the UI <strong>as light as possible</strong> (normally only a few lines of code) and test the crap out of everything else. At least I can then have some confidence that everything that makes it to the UI is as correct as it can be.</p>\n\n<p>Is it perfect? Perhaps not, but at least it as still quite highly automated and the core code (where most of the "magic" happens) still has pretty good coverage..</p>\n'}, {'answer_id': 18771, 'author': 'Johnno Nolan', 'author_id': 1116, 'author_profile': 'https://Stackoverflow.com/users/1116', 'pm_score': 5, 'selected': True, 'text': '<p>Unit testing will be achievable if you <strong>separate your layers</strong> appropriately. As Rob Cooper implied, <em>don\'t put any logic in your WebForm other than logic to manage your presentation</em>. All other stuff logic and persistence layers should be kept in separate classes and then you can test those individually.</p>\n\n<p>To test the GUI some people like <a href="http://selenium.openqa.org/" rel="noreferrer">selenium</a>. Others complain that is a pain to set up.</p>\n'}, {'answer_id': 31203, 'author': 'Thomas Eyde', 'author_id': 3282, 'author_profile': 'https://Stackoverflow.com/users/3282', 'pm_score': 2, 'selected': False, 'text': '<p>A common practice is to move all the code you can out of the codebehind and into an object you can test in isolation. Such code will usually follow the MVP or MVC design patterns. If you search on "Rhino Igloo" you will probably find the link to its Subversion repository. That code is worth a study, as it demonstrate one of the best MVP implementations on Web Forms that I have seen.</p>\n\n<p>Your codebehind will, when following this pattern, do two things:</p>\n\n<ol>\n<li>Transit all user actions to the presenter.</li>\n<li>Render data provided by the presenter.</li>\n</ol>\n\n<p>Unit testing the presenter should be trivial.</p>\n\n<p>Update: Rhino Igloo can be found here: <a href="https://svn.sourceforge.net/svnroot/rhino-tools/trunk/rhino-igloo/" rel="nofollow noreferrer">https://svn.sourceforge.net/svnroot/rhino-tools/trunk/rhino-igloo/</a></p>\n'}, {'answer_id': 149952, 'author': 'Mephisztoe', 'author_id': 23369, 'author_profile': 'https://Stackoverflow.com/users/23369', 'pm_score': 0, 'selected': False, 'text': '<p>There have been tries on getting Microsoft\'s free UI Automation (included in .NET Framework 3.0) to work with web applications (ASP.NET). A german company called Artiso happens to have written a blog entry that explains how to achieve that (<a href="http://www.artiso.com/ProBlog/PermaLink,guid,1a01608b-e5fa-4ad9-8c4f-f672a69c3537.aspx" rel="nofollow noreferrer">link</a>).</p>\n\n<p>However, their blogpost also links an MSDN Webcasts that explains the UI Automation Framework with winforms and after I had a look at this, I noticed you need the AutomationId to get a reference to the respecting controls. However, in web applications, the controls do not have an AutomationId. </p>\n\n<p>I asked Thomas Schissler (Artiso) about this and he explained that this was a major drawback on InternetExplorer. He referenced an older technology of Microsoft (<a href="http://msdn.microsoft.com/en-us/library/ms971310.aspx" rel="nofollow noreferrer">MSAA</a>) and was hoping himself that IE8 will do this better.</p>\n\n<p>However, I was also giving Watin a try and it seems to work pretty well. I even liked Wax, which allows to implement simple testcases via Microsoft Excel worksheets.</p>\n'}, {'answer_id': 223427, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://sm-art.biz/Ivonna.aspx" rel="nofollow noreferrer">Ivonna</a> can unit test your views. I\'d still recommend moving most of the code to other parts. However, some code just <em>belongs</em> there, like references to controls or control event handlers.</p>\n'}, {'answer_id': 1481684, 'author': 'Andrei Taranchenko', 'author_id': 53687, 'author_profile': 'https://Stackoverflow.com/users/53687', 'pm_score': 2, 'selected': False, 'text': "<p>I would generally avoid testing that involves relying on UI elements. I favor integration testing, which tests everything from your database layer up to the view layer (but not the actual layout). </p>\n\n<p>Try to start a test suite <strong>before</strong> writing a line of actual code in a new project, since it's harder to write tests later.</p>\n\n<p>Choose carefully what you test - don't mindlessly write tests for everything. Sometimes it's a boring task, so don't make it harder. If you write too many tests, you risk abandoning that task under the weight of time-consuming maintenance. </p>\n\n<p>Try to bundle as much functionality as possible into a single test. That way, if something goes wrong, the errors will propagate anyway. For example, if you have a digest-generating class - test the actual output, not every single helper function.</p>\n\n<p><strong>Don't trust yourself</strong>. Assume that you will always make mistakes, and so you write tests to make your life easier, not harder.</p>\n\n<p>If you are not feeling good about writing tests, you are probably doing it wrong ;)</p>\n"}]
['https://Stackoverflow.com/questions/18601', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1473493/']