Unit-testing – How to unit test Application and UI code

applicationsunit testing

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?

Best Answer

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.

Related Topic