Oldies but Goodies - Red-Green-Refactor - TDD

A lot of people liked my description of test-driven development during the Microsoft TDD flap. Here it is again so you can more easily bookmark it:

  1. Think: Figure out what test will best move your code towards completion. (Take as much time as you need. This is the hardest step for beginners.)

  2. Red: Write a very small amount of test code. Only a few lines… usually no more than five. Run the tests and watch the new test fail: the test bar should turn red. (This should only take about 30 seconds.)

  3. Green: Write a very small amount of production code. Again, usually no more than five lines of code. Don’t worry about design purity or conceptual elegance. Sometimes you can just hardcode the answer. This is okay because you’ll be refactoring in a moment. Run the tests and watch them pass: the test bar will turn green. (This should only take about 30 seconds, too.)

  4. Refactor: Now that your tests are passing, you can make changes without worrying about breaking anything. Pause for a moment. Take a deep breath if you need to. Then look at the code you’ve written, and ask yourself if you can improve it. Look for duplication and other “code smells.” If you see something that doesn’t look right, but you’re not sure how to fix it, that’s okay. Take a look at it again after you’ve gone through the cycle a few more times. (Take as much time as you need on this step.) After each little refactoring, run the tests and make sure they still pass.

  5. Repeat: Do it again. You’ll repeat this cycle dozens of times in an hour. Typically, you’ll run through several cycles (three to five) very quickly, then find yourself slowing down and spending more time on refactoring. Than you’ll speed up again. 20-40 cycles in an hour is not unreasonable.

http://www.jamesshore.com/Blog/Red-Green-Refactor.html

2 Likes

Ce tare, cam în stilul ăsta lucrez şi eu, nu stau să planific la infinit, pur şi simplu mă apuc de treabă şi pofta vine mâncând, ideile se înfiripă în timp ce scriu codul.

1 Like

abordarea tdd este incurajata.
Din ce stiu ai o suita de teste si tu trebuie sa scrii programul, nu ?

Scrii teste si dupa scrii codul, dar nu esti capabil sa scrii teste inseamna ca nu ai inteles problema.

Common, puteai pune un exemplu .net, ai test explorer in visual studio, nu se compara cu java, de acolo vine red green refactor, iti apar intai testele red si pe parcurs ce lucrezi, se fac green

https://ivision.com/blog/test-driven-development-tutorial/

1 Like

Pai aia cu green, red nu cred ca este specifica VS. toate ide-urile iti permit sa faci teste.

Important este intelegerea conceptului :slight_smile:

3 Likes

da, cu un plugin, in Visual Studio ai integrat tot ce ai nevoie in IDE

nu e chiar actual articolul, ai proiect de testare in vs, si framework testare integrat.

Intr-adevar

Sunt lucruri interesante pe acest blog :slight_smile: