TDD - One test at a time or make a batch?

萝らか妹 提交于 2019-11-30 20:24:07
  • Write just enough of a test for the test to fail, usually this is because your code does not compile
  • write just enough code to make your test pass
  • repeat

These are the rules of TDD. This means that you are only ever writing one unit test and one bit of code at a time and doing this iteratively.

The point of TDD is not writing tests, it is using your tests to drive out the design. The idea is that once all your tests pass and they cover all the functionality you know that your code is also complete. Without tests, how do you know when you are done. So, when you feel that you have tested everything, stop.

I'd vote for the first one as YAGNI comes to mind with why the second one could be problematic as that can take a long time to write all the tests first.

I write a single test first, then make it pass, then I refactor.

Working to make a single test pass allows me to focus on the capability the test is demonstrating.

When I have several ideas for the next test, I write then as test placeholder and/or as comments, eg:

// test_foo_with_negative_value(void)

Then I pick up one, implemented it ... I can either pick up the simplest one, to progress, or a totally different one, to see if my design can handle such as case.

Normally you would want to develop one test at a time. In a scientific setting, you isolate one variable and test on that variable. The same should apply to programming. First, solve one problem, and then expand onto other problems.

There are many reasons for this method is beneficial to the development process:

  1. Easy to Debug
  2. Focus on one idea at a time, not really complicated
  3. It uses the Bottom Up approach, allowing smaller things to build up to a bigger product.

The first one. It will help you to better understand the functionality you are currently implementing as you have to think about what you are trying to test. If you do all at once it will be too much cognitive overhead!

One at a time for TDD.

Write a test - write code to pass that test, rinse and repeat until all tests pass. Once you run out of tests then refactor your code.

Personally, I have found that writing a single test at a time helps me to stay focussed on the feature I am working on, rather than trying to bang out a batch of tests at once. I like the rhythm/flow of single tests better, and it helps to aviod any feature creep that may be introduced when writing a number of tests at one.

In a perfect development team (rephrase: in my perfect development team), I would be pair programming with one person being the tester and one being the production code developer. The tester would write a simple test for the production code logic and the production code developer would write the simplest thing to satisfy the test. Once that is done, both parties would check in their code. Then refactor both test and production code. Then check in. Then continue with the test.

Test -> Production Code -> Check In -> Refactor -> Check In -> Repeat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!