10

I love writing unit tests and agree they are an excellent way to test code, prevent regressions, etc. However, I find myself unable to write them much as the vast majority of the code I work on day-in and day-out is application code that displays a UI to the user. Is there a good way of unit testing application code? What are the best practices here?

I am not looking for a specific answer such as a framework, etc. But rather, in general, how do you approach this problem?

Justin Ethier
  • 881
  • 7
  • 19

2 Answers2

8

To test the actual user interface, Selenium works well.

If you want actual unit tests, your strategy is to push as much of the logic back from the actual user interface as possible, typically in a ViewModel object. You can then write unit tests against the ViewModel object.

In other words, put as little logic in the actual UI as possible, so that the unit testing can take place elsewhere.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • That's good design for SoC there shouldn't be Domain/business logic in the UI. A lot of client logic - interactivity/validation can go in the UI with JavaScript, which is unit testable. – StuperUser Mar 10 '11 at 12:00
1

I have had success with unit testing my application with MSTest and testing my Javascript with QUnit and picking up the results of those tests with a single MSTest Unit test that uses WatiN (I was suggested Selenium, which has the ability to record tests too, but found WatiN better for automating cleanly).

StuperUser
  • 6,133
  • 1
  • 28
  • 56