Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
What is "application code that displays UI"? Are you talking about the C in MVC? –  Martin Wickman Dec 17 '10 at 21:32
    
I am talking about any kind of graphical interface - JavaScript/HTML, C++ with MFC, WinForms, WPF, etc... –  Justin Ethier Dec 17 '10 at 21:35
    
MVVM is easy to unit test. See MVVM Command Pattern. –  Amir Rezaei Dec 17 '10 at 23:03
    
@amir - Do you have any references or additional information? –  Justin Ethier Dec 20 '10 at 3:52
add comment

2 Answers

up vote 8 down vote accepted

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.

share|improve this answer
    
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
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.