ArticleslgStudy

computer science

Test-driven development

Test-driven development is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Test-driven development rather than just read about it. In short: Test-driven development (TDD) is a way to write source code that involves writing an automated unit-level test case that fails, then writing just enough code to make the test pass, then refactoring both the test code and the production code, then repeating with another new test case. Alternative approaches to writing automated tests is to write all of the production code before starting on the test code or to write…

Test-driven development — main illustration
Test-driven development — illustration

Key takeaways

  • Test-driven development belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Test-driven development to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Test-driven development from memory before moving on to harder problems.

Reference excerpt

Test-driven development (TDD) is a way to write source code that involves writing an automated unit-level test case that fails, then writing just enough code to make the test pass, then refactoring both the test code and the production code, then repeating with another new test case. Alternative approaches to writing automated tests is to write all of the production code before starting on the test code or to write all of the test code before starting on the production code. With TDD, both are written together, therefore shortening debugging time needs. TDD is related to the test-first programming concepts of extreme programming, begun in 1999, but more recently has created more general interest in its own right. Programmers also apply the concept to improving and debugging legacy code developed with older methods.

History Software engineer Kent Beck, who is credited with having developed or "rediscovered" the technique, stated in 2003 that TDD encourages simple designs and inspires confidence.

The original description of TDD was in an ancient book about programming. It said you take the input tape, manually type in the output tape you expect, then program until the actual output tape matches the expected output. After I'd written the first xUnit framework in Smalltalk I remembered reading this and tried it out. That was the origin of TDD for me. When describing TDD to older programmers, I often hear, "Of course. How else could you program?" Therefore I refer to my role as "rediscovering" TDD.

Coding cycle

The TDD steps vary somewhat by author in count and description, but are generally as follows. These are based on the book Test-Driven Development by Example, and Kent Beck's Canon TDD article.

1. List scenarios for the new feature List the expected variants in the new behavior. "There's the basic case & then what-if this service times out & what-if the key isn't in the database yet &…" The developer can discover these specifications by asking about use cases and user stories. A key benefit of TDD is that it makes the developer focus on requirements before writing code. This is in contrast with the usual practice, where unit tests are only written after code. 2. Write a test for an item on the list Write an automated test that would pass if the variant in the new behavior is met. 3. Run all tests. The new test should fail – for expected reasons This shows that new code is actually needed for the desired feature. It validates that the test harness is working correctly. It rules out the possibility that the new test is flawed and will always pass. 4. Write the simplest code that passes the new test Inelegant code and hard coding is acceptable. The code will be honed in Step 6. No code should be added beyond the tested functions. 5. All tests should now pass If any fail, fix failing tests with minimal changes until all pass. 6. Refactor as needed while ensuring all tests continue to pass Code is refactored for readability and maintainability. In particular, hard-coded test data should be removed from the production code. Running the test suite after each refactor ensures that no existing function is broken. Examples of refactoring: moving code to where it most logically belongs removing duplicate code making names self-documenting splitting methods into smaller pieces re-arranging inheritance hierarchies Repeat Repeat the process, starting at step 2, with each test on the list until all tests are implemented and passing. Each tests should be small and commits made often. If new code fails some tests, the programmer can undo or revert rather than debug excessively. When using external libraries, it is important not to write tests that are so small as to effectively test that library only, unless there is reason to believe that the library is buggy or too feature-poor to serve all needs of the software under development.

Test-driven work TDD has been adopted outside of software development, in both product and service teams, as test-driven work. For testing to be successful, it needs to be practiced at the micro and macro levels. Every method in a class, every input data value, log message, and error code, amongst other data points, need to be tested. Similar to TDD, non-software teams develop quality control (QC) checks (usually manual tests rather than automated tests) for each aspect of the work prior to commencing. These QC checks are then used to inform the design and validate the associated outcomes. The six steps of the TDD sequence are applied with minor semantic changes:

"Add a check" replaces "Add a test" "Run all checks" replaces "Run all tests" "Do the work" replaces "Write some code" "Run all checks" replaces "Run tests" "Clean up the work" replaces "Refactor code" "Repeat"

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Test-driven development

Start with the simplest possible case. Write down what Test-driven development claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Test-driven development before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Test-driven development ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Test-driven development

In research
Test-driven development appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Test-driven development in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Test-driven development is common in secondary-school and first-year university syllabi. It links to neighbouring topics Extreme programming, Software development philosophies, Software development process, so understanding it makes those chapters shorter.
In everyday life
Look for Test-driven development outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Test-driven development” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Test-driven development in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Test-driven development means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Test-driven development out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Test-driven development in simple terms?

Test-driven development (TDD) is a way to write source code that involves writing an automated unit-level test case that fails, then writing just enough code to make the test pass, then refactoring both the test code and the production code, then repeating with another new test case. Alternative ap…

Why does Test-driven development matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Test-driven development?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Test-driven development.

Tags

  • Extreme programming
  • Software development philosophies
  • Software development process
  • Software testing

Keep exploring