<p>So I know that unit testing is a must. I get the idea that TDD is the way to go when adding new modules. Even if, in practice, I don't actually do it. A bit like commenting code, really. </p>

<p>The real thing is, I'm struggling to get my head around how to unit-test the UI and more generally objects that generate events: user controls, asynchronous database operations, etc. </p>

<p>So much of my code relates to UI events that I can't quite see how to even start the unit testing. </p>

<p>There must be some primers and starter docs out there? Some hints and tips? </p>

<p>I'm generally working in C# (2.0 and 3.5) but I'm not sure that this is strictly relevant to the question. </p>

[{'answer_id': 37842, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 0, 'selected': False, 'text': '<p>You should separate logic and presentation. Using MVP(Model-View-Presenter)/MVC (Model-View-Controller) patterns you can unit test you logic without relying on UI events.\nAlso you can use <a href="http://www.codeplex.com/white" rel="nofollow noreferrer">White framework</a> to simulate user input.\nI would highly recommend you to visit Microsoft\'s <a href="http://msdn.microsoft.com/en-us/practices/default.aspx" rel="nofollow noreferrer">Patterns&amp;Practices developer center</a>, especially take a look at composite application block and Prism - you can get a lot of information on test driven design.</p>\n'}, {'answer_id': 37848, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 0, 'selected': False, 'text': "<p>The parts of your application that talk to the outside world (ie UI, database etc.) are always a problem when unit-testing. The way around this is actually not to test those layers but make them as thin as possible. For the UI you can use a humble dialog or a view that doesn't do anything worth testing and then put all the logic in a controller or presenter class. You can then use a mocking framework or write your own mock objects to make fake versions of the views to test the logic in the presenters or controller. On the database side you can do something similar.</p>\n\n<p>Testing events is not impossible. You can for example subscribe an anonymous method to the event that throws an exception if the event is thrown or counts the number of times the event is thrown.</p>\n"}, {'answer_id': 37853, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 3, 'selected': True, 'text': "<p>the thing to remember is that unit testing is about testing the units of code you write. Your unit tests shouldn't test that clicking a button raises an event, but that the code being executed by that click event does as it's supposed to.</p>\n\n<p>What you're really wanting to do is test the underlying code does what it should so that your UI layers can execute that code with confidence. </p>\n"}, {'answer_id': 37919, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://wiki.c2.com/?TheHumbleDialogBox" rel="nofollow noreferrer">Read this</a> if you\'re struggling with UI Testing  </p>\n\n<p>Manually test UI stuff where benefit to cost in automating it is minimal. Test everything under the UI skin ruthlessly. Use Humble Dialog, MVC or variants to keep logic and UI distinct and loosely coupled.</p>\n'}]
['https://Stackoverflow.com/questions/37832', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2902/']