19

Can I use TDD for gui application? How to do it.

3 Answers 3

33

The answer which has evolved over the last few years is, you don't apply TDD to the GUI, you design the GUI in such as way that there's a layer just underneath you can develop with TDD. The Gui is reduced to a trivial mapping of controls to the ViewModel, often with framework bindings, and so is ignored for TDD.

This is known variously as the Presentation Model (Fowler) the Model-View-ViewModel and DataModel-View-ViewModel architecture.

This approach removes the GUI layer from TDD and unit testing. It does not mean the GUI is never tested but just acknowledges that it is not cost effective to pursue automated GUI testing, particularly as part of TDD. Integration and user testing should cover the GUI.

Josh Smith's 2009 WPF article is a detailed explanation of MVVM with some testing.

More recently, Houssem Dellai's 2016 video Creating Unit Tests for Xamarin Forms Apps shows a XAML UI with bound ViewModel and walks through creating a unit test project

2
  • Great answer, thanks. Note, though, that the link to good discussions is pretty much useless - it points to a search for ViewModel which is a massive area that includes a huge number of discussions that have nothing to do with TDD. Links to specific discussions would be useful. I have been digging into this topic heavily over the past few days and there is sadly little discussion of this issue. Feb 25, 2017 at 22:27
  • 2
    When I wrote my answer, 8 years ago, that search was indeed a good one but it was a bit silly to expect it to stay that way.
    – Andy Dent
    Feb 27, 2017 at 14:45
5

Why not. Here is a good article, from object mentor.

Found another blog post, TDD - Introduction to Moq. Its related to C# and VB.NET.

Checking out Myth and Misconception regarding TDD is a must.

Here is a book related to .NET, TDD in Microsoft .NET.

1

Chad Myers has a nice walk through testing the controller:

http://www.chadmyers.com/Blog/archive/2007/11/27/tdd-with-asp.net-mvc-3.5-extensions.aspx

1
  • Link is dead. You should really copy the gist of the content into your answer for the answer to stay relevant over time...
    – thoni56
    Sep 28, 2018 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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