Afraz Ali
Afraz Ali

Reputation: 2762

How to get started with TDD using Winforms

I have read/watched so much about TDD & BDD recently that I really want to master it. I have been a developer that only writes code and then tests it from outside (like we always started). The problem seems to be in getting up and running with TDD. I want to just create a simple Winform app in which I want to show a list of something lets say products. I just don't know where to get started, should I write a test for controller first? the controller needs reference to view and service and so on so forth. ASP.Net MVC is built for testing therefore it is a bit easy to get started but Winforms are a real pain. Kindly give me some Videos (Most preffered) that show TDD in Winforms.

I have watched tons of videos that show you testing a class or feature but how do you test UI that does not support testing?

In short I want to know if anyone has been doing TDD for a while, how does he/she do it in Winforms?

I have written loads of code that I just delete because I get stuck, Please help!

Upvotes: 4

Views: 2394

Answers (3)

Ikaso
Ikaso

Reputation: 2268

The controller is a good starting point. Extract the View public api to an interface an test the interaction of the controller with that interface using a Mocking Framework. You can do the same for the service layer too. Also, I would take the bottom-up approach for this application and unit test the lower level layers before adding tests to the UI layer. On the UI layer I will write a set of Acceptance Tests using a BDD framework and use mocking frameworks to make those tests lightweight and to reduce the number of tests.

Good luck!

Upvotes: 0

hattenn
hattenn

Reputation: 4399

I'm new to TDD too, and just like you I'm trying to learn. Here's what I've come up with during my searches, maybe it will help you too:

  1. Check out Roy Osherove's website, and also his book "The Art of Unit Testing". Check out the videos, there are some great ones, especially the "Pair Programming" section. His book was the single best book that I've read about unit testing and TDD, and the only one that I've read from cover to cover.
  2. Check out some TDD katas. There are some on the web page I've suggested in the previous item. Check out how other people are solving the katas. It's really helpful.
  3. Read about dependency injection.
  4. If you want to TDD in WinForms, check out the MVP pattern. As far as I know, it's the de facto patten for separating UI from business code in WinForms.

Good luck with your search.

Upvotes: 3

Lunivore
Lunivore

Reputation: 17677

Here's how I do it for any kind of UI, be it Web or Winforms or WPF or Swing in Java or even a web service interface that's going to be used by another system.

  • Write the UI, and hard-code any data behind it.
  • Write a controller, and move the hard-coded data to the controller. The controller should just be presenting the data in a form that the UI can understand.

Your controller now has no behavior; just static data - but it's got the right API for the UI, and now you know how the UI wants to use it.

  • Write an example that shows how the UI will use the controller in the most basic form.
  • Make the example work.
  • Write another example that shows how the controller behaves differently in a different context.
  • Make the example work.

Those are your unit tests! If you know that your controller is going to need some other classes to collaborate with, you can mock those out too.

Once your collaborators are mocked out, you already know how the controller wants to use them. Again, you have the API for those collaborators already, because the controller is using them.

This is what we're doing when we talk about "outside-in" in BDD.

Upvotes: 4

Related Questions