What is TDD?

- The next couple of hours focuses deeplyon creating and using these automated unit tests.But this course isn't called Foundations of ProgrammingUnit Testing, it's Foundations of Programming,Test-Driven Development.And let me be very specific.Doing unit testing is not the same thingas doing test-driven development.You see, having these automated unit tests,it's great, it's amazingly helpful, very worthwhile.And we're going to do those unit tests,but to do TDD, we take that idea one step further.

So what's the difference?Well, when most developers first encounterthis idea of automated unit tests,of writing code that tests our code,they understandably make the assumptionthat this is how the process goes.First, we would write a bunch of our normalapplication logic and then we would writeour automated unit test to checkand verify the logic that we just wrote.So as an example, we might write a methodcalled calculateLoan.We'll add some functionality to that,compile that, and then, after we've written,we'd think about creating a few tests,what kind of values might I pass inand verify those results as what we expect.

And this is unit testing.We are testing and validating an individual methoddown to the individual unit in the code,but this is not test-driven development.Understand that TDD has the same two elements.A piece of application logic, a unit,and a test for that logic.But it turns the process completely around.What test-driven development asks of us as developersis to write the test first before we writethe application logic.That might sound counter-intuitive,if not indeed impossible, but Simon, I hear you say,I can't exactly test a method I haven't written yet.

Actually, you can.And in test-driven development, we do.And not just once or twice, but all the time.We will write tests that attempt to instantiate an objectwhere we haven't written a class for that object yet.We will write tests that call a method that doesn't exist.And we will run those tests.And the first time we run those tests, they should fail.Depending on the language, they may not even compile.Or they might run, and then tell us,well, I can't find this method, I can't find this class.But we want the test to fail.

Because after all, if it doesn't fail,we have some other kind of problem going on.And this is the fundamental first stepof everyday test-driven development.The core distinction between doing TDDand not doing TDD.That before we write any new code,we first write a failing test.And only after it fails, do we write the application logicto pass that test.And we write the minimum necessary codeto just pass the test.One thing it immediately removes is the common situationwhere we create a new class or a new methodand end up staring at a blank screentrying to juggle in our heads all the thingsthis might need to do today and tomorrow and in the future.

All this speculative code that we're trying to come up withthat is quicksand for every developer.No, just pass the test.This one test.It keeps us focused.This gives us clarity on exactly what we're doingat any one point.Don't do anything more than pass this small testand this is what is meant by test-driven development.The tests drive our development.So TDD is not just development that uses unit tests.Test-driven development, as a process and technique,is all about the priority and the positionthat we give to those unit tests.

Now if all you're looking for is to know moreabout automated unit testing,you will find this course very helpfulbecause that is what we're doing.But just know that I will take a TDD approachto unit testing, shifting the idea of testingas an afterthought, something that we only do laterto putting the test upfront.Figure it out first, let it drive what you write.I'm saying this multiple timesbecause for most developers,this is a very subtle change in prioritybut has a dramatic impact on the waythat you approach and write code.

So this course is focused on automated unit testing,having a systematic, repeatable processthat is totally integrated into our regular project.And with TDD, where we will use these unit teststo drive everything else that we do.

Resume Transcript Auto-Scroll

Author

Released

6/11/2013

Prove your code is working every step of the way using a formalized test-driven development (TDD) process. TDD can be done in every modern programming environment, and for desktop, mobile, or web apps. In this course, Simon Allardice teaches you exactly how to get started with TDD: what makes a good test, why we're more interested in failure than success, and how to measure and repeatedly run tests.

The course explores the jargon of TDD—test suites, test harness, mock and stub objects, and more—and covers how TDD is used in the most common programming languages and environments. Plus learn to create, run, and manage the tests and move to a test-first mindset.