unit testing - How to apply TDD with complicated function? -


uncle bob's 3 rules of test-driven development state following:

  1. you not allowed write production code unless make failing unit test pass.

  2. you not allowed write more of unit test sufficient fail; , compilation failures failures.

  3. you not allowed write more production code sufficient pass 1 failing unit test.

in real world, example, have task complete method requires complex algorithms (some advanced math knowledge example), if apply 3 above rules, this:

  • start writing test case => run , see fails

  • write lines of code enough make test pass

  • refactor code

  • then repeat loop.

my question is: approach quite unrealistic , distracted in such situations?

why don't write first test case, focus on finding solution implement it, mean looking @ big picture, instead of writing enough code pass first test case?

i think other answer excellent, beyond that: if one method require implement several algorithms - sure such method respects single responsibility principle? rather sounds 1 method doing way many things.

so, when step , see 3 algorithms used - tells each algorithm should go own method. , our "initial" method calling these other methods computation done.

and going in other direction - there no law prevents adapt tdd needs. leads practice call "bottom tdd".

it goes this: instead of first writing test 1 huge method - think different parts need within huge method. write down tests first part; , implement that. parts. on time, parts enhanced, , maybe merge smaller parts bigger on way (and mean merge multiple tests bigger 1 well).

this technique can mean might end single testcase huge method - used tdd test small parts while building "the big solution".

in other words: instead of writing 1 big functional test public contract of 1 method - start writing tests small helper methods know need. in end helpers private methods - there no point in testing them directly. can keep parts of earlier tests make sense in context of big public method.

long story short: of these techniques meant guide finding own way. given have enough experience design on fly possible (and lot of fun) use tdd way!


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -