Editing
Test-Driven Development
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
<div style="background-color: #4B0082; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;"> {{BloomIntro}} Test-Driven Development (TDD) is a software development process that "Flips" the traditional way of writing code. Instead of writing a feature and then "Testing" to see if it works, a TDD developer writes the **Test first**. This forces the programmer to think deeply about the "Requirement" before they ever write a line of logic. By following the "Red-Green-Refactor" cycle, TDD creates a "Safety Net" of automated tests that allows developers to change their code with 100% confidence. It is the practice of "Building the Quality In" rather than "Checking for bugs later." </div> __TOC__ <div style="background-color: #000080; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;"> == <span style="color: #FFFFFF;">Remembering</span> == * '''Test-Driven Development (TDD)''' β A process where you write an automated test before you write the code to fulfill that test. * '''Unit Test''' β A test that checks one single "Unit" of code (e.g., one function or one class) in isolation. * '''Integration Test''' β A test that checks how different parts of the system work together. * '''Red-Green-Refactor''' β The core cycle of TDD: Write a failing test (Red), make it pass (Green), and clean up the code (Refactor). * '''Mocking''' β Creating a "Fake" version of a dependency (like a database or an API) to test your code without needing the real thing. * '''Code Coverage''' β The percentage of your codebase that is "Covered" by automated tests. * '''Regression''' β A bug that appears in an old feature after you change something else. * '''Continuous Integration (CI)''' β A system that automatically runs all your tests every time you save your code. * '''Assert''' β The command used in a test to say: "I expect the result to be X." * '''Kent Beck''' β The software engineer who "Rediscovered" TDD and made it a core part of Extreme Programming (XP). </div> <div style="background-color: #006400; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;"> == <span style="color: #FFFFFF;">Understanding</span> == TDD is understood through '''The Cycle''' and '''The Safety Net'''. '''1. The Red-Green-Refactor Cycle''': * '''RED''': You write a test for a feature that doesn't exist yet (e.g., `test_addition_of_two_numbers`). When you run it, it **fails**. This proves the test is actually working. * '''GREEN''': You write the "Simplest possible code" to make the test pass (even if the code is ugly). Once the test is green, you know you have met the requirement. * '''REFACTOR''': Now that the test is safe, you "Clean up" the codeβrenaming variables, removing duplicates, and making it beautiful. Since you have the test, you know you haven't broken anything. '''2. Thinking before Coding''': The greatest benefit of TDD is not the "Tests," but the "Design." * Because you have to write a test first, you are forced to design a "Clean Interface." * If a function is "Hard to test," it means the function is "Badly Designed." TDD acts as a "Early Warning System" for bad architecture. '''3. The Living Documentation''': In a TDD project, the tests are the **Documentation**. * If a new developer wants to know how the "Login" system works, they just read the `login_tests.py` file. * Unlike a PDF manual, these tests are "Alive"βif the code changes and the manual isn't updated, the tests will fail immediately. '''The 'Test Pyramid'''': The idea that you should have thousands of fast Unit Tests, hundreds of Integration Tests, and only a few slow UI (End-to-End) Tests. </div> <div style="background-color: #8B0000; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;"> == <span style="color: #FFFFFF;">Applying</span> == '''Modeling 'The TDD Cycle' (A simple calculator):''' <syntaxhighlight lang="python"> # --- STAGE 1: THE TEST (Write this first!) --- def test_add(): # We haven't even written the 'add' function yet! assert add(2, 3) == 5 # --- STAGE 2: THE CODE (Simplest possible) --- def add(a, b): return a + b # Now the test passes (GREEN) # --- STAGE 3: THE REFACTOR (Clean up) --- def add(a: int, b: int) -> int: """Adds two integers.""" return a + b print("All tests passed!") </syntaxhighlight> ; TDD Landmarks : '''Extreme Programming (XP)''' β The 1990s movement that brought TDD to the mainstream as a way to handle rapidly changing business requirements. : '''JUnit (1997)''' β The testing framework created by Kent Beck and Erich Gamma that made TDD easy for Java developers and inspired every modern testing tool. : '''The 'London School' vs. 'Detroit School'''' β Two different philosophies of TDD: one focuses on "Behavior" and "Mocks," the other focuses on "State" and "Real objects." : '''Test-Induced Design Damage''' β A famous (and controversial) argument by DHH (the creator of Ruby on Rails) that TDD can sometimes lead to "Over-complex" code. </div> <div style="background-color: #8B4500; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;"> == <span style="color: #FFFFFF;">Analyzing</span> == {| class="wikitable" |+ Traditional vs. TDD ! Feature !! Traditional (Test After) !! TDD (Test First) |- | Focus || "Does it work?" || "How should it work?" |- | Debugging Time || High (Finding bugs later) || Low (Bugs found instantly) |- | Code Quality || Often "Spaghetti" || Usually "Modular" |- | Confidence || Low (Scared to change old code) || High (Tests catch every mistake) |} '''The Concept of "Failing Fast"''': Analyzing why failing early is good. In TDD, you know you have a bug **one second** after you write it. In traditional coding, you might find that same bug 3 months later when the app crashes in production. The "Cost of a Bug" grows exponentially the longer it stays in the code. </div> <div style="background-color: #483D8B; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;"> == <span style="color: #FFFFFF;">Evaluating</span> == Evaluating TDD: # '''Speed''': Does TDD take "Too long"? (While it takes longer to write the first line of code, it saves weeks of "Debugging" and "Maintenance" later). # '''Legacy Code''': How do you use TDD on an old app with 1 million lines of code and zero tests? (The "Testing Hole" problem). # '''UI Testing''': Is it possible to use TDD for "Art" or "UI Design" where there is no "Right Answer"? # '''Motivation''': Why is it so hard for developers to stay in the TDD "Habit"? (It requires intense discipline to write the test first when you are in a rush). </div> <div style="background-color: #2F4F4F; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;"> == <span style="color: #FFFFFF;">Creating</span> == Future Frontiers: # '''AI Test-Generators''': AI that reads your "Plain English" requirement and automatically writes the TDD tests for you. # '''Mutation Testing''': A system that "Intentionally injects bugs" into your code to see if your tests are strong enough to catch them. # '''Formal Verification''': Moving beyond "Tests" to "Mathematical Proofs" that a piece of code can **never** fail. # '''Behavior-Driven Development (BDD)''': A version of TDD where the tests are written in natural language (e.g., "Given a user is logged in, when they click X, then Y should happen"). [[Category:Computer Science]] [[Category:Software Engineering]] [[Category:Technology]] </div>
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Template:BloomIntro
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information