Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended thing for a TDD newbie

Tags:

tdd

I'm very new to TDD world. I have a few questions regarding TDD.

  1. Do I have to do test-first in TDD? I heard that TDD is not about test. It's about design. I'm agreed that it's good to do test-first but what I like to know is that is it still TDD if we follow the test-last approach?

  2. Shall we prefer to use BDD over TDD? I used to list out the specification of my task first and I try to write the test case based on my specification. Is it wrong approach? Do you guys prefer using BDD or TDD for your development?

  3. Mocking? Some people from my team used to say that they are praticsing TDD. But they never follow test-first approach. they never mock the data. Do we have to mock the data in TDD?

  4. "Using Mock Library" Vs "creating the mock class with data manually". Do you prefer to use mock library or create the mock classes with some mock data?

  5. Any recommended book for TDD or BDD? I read Kent Beck's classic Test-Driven Development - By Example. I found that this book is published in very early stage of TDD so some of the things in this book are not a bit outdated.

like image 904
Mark Avatar asked Oct 20 '09 03:10

Mark


2 Answers

1). Do I have to do test-first in TDD? I heard that TDD is not about test. It's about design. I'm agreed that it's good to do test-first but what I like to know is that is it still TDD if we follow the test-last approach?

Yes! Strictly speaking TDD is Test-Driven Development. So the development is driven by the test. So you test first, then develop program to pass all tests.

2). Shall we prefer to use BDD over TDD? I used to list out the specification of my task first and I try to write the test case based on my specification. Is it wrong approach? Do you guys prefer using BDD or TDD for your development?

I think you should balance them. Use other technique to provide overall design first as best as time provide (do risk management to find appropriate time you should spend on designing) (Find a paper about "RUP essential". It give quite a good idea about balancing agile and less-agile). Identify the most critical parts then creates test and develop to pass the test.

3).Mocking? Some people from my team used to say that they are praticsing TDD. But they never follow test-first approach. they never mock the data. Do we have to mock the data in TDD?

Test-first and mocking is not the same thing. Mocking allows code to be more testable as well as be testable when other part (which this code relies on) does not exist. So if there is no such dependency (IF!!), then you can to not mock them. (Read "Working Effectively with Legacy Code" about Seam point for more details).

4). "Using Mock Library" Vs "creating the mock class with data manually". Do you prefer to use mock library or create the mock classes with some mock data?

I think it just like using someone-else library or create yourown. Totally depends on the situation and many factors. For example, if you project is big and you can find appropriate mock library, use it.

5). Any recommended book for TDD or BDD? I read Kent Beck's classic Test-Driven Development - By Example. I found that this book is published in very early stage of TDD so some of the things in this book are not a bit outdated.

There are list of books on TDD here.

Hope this helps.

like image 84
NawaMan Avatar answered Jan 02 '23 20:01

NawaMan


  1. Yes, it is about design, but this design methodology does involve writing tests first. People follow this rule with varying degrees of strictness, but most people I know who practice TDD tend to believe that it's better to follow the rule.
  2. BDD has been described as TDD done right. The difference is minimal. Essentially BDD just makes the point about tests as specifications more explicit.
  3. There is a great deal of disagreement about the usefulness of mocks. I personally prefer to test interfaces, and I avoid placing expectations in a mock. That said, testing in isolation is still a good idea for a wide variety of reasons, not the least of which is test speed. There's nothing more annoying than refactoring a piece of code which still conforms exactly to the previous interface, having a fully-working end result, and yet all the tests fail because the expectations on the mock weren't met anymore. Bad mock usage results in testing of implementation details rather than ensuring that the work performed is correct.
  4. See #3. I prefer to just use a stub with no expectations or alternatively an integration test.
  5. Test-Driven Development: A Practical Guide by Dave Astels. Highly recommended.
like image 41
Bob Aman Avatar answered Jan 02 '23 20:01

Bob Aman