Second Law of TDD

5 minutes 5 Questions

The Second Law of TDD states that you cannot write more of a unit test than is sufficient to fail. This means you should write just enough of a test to demonstrate failure. It encourages only writing the code necessary to address the failing test, ensuring that the codebase remains concise and uncluttered. By writing tests in small increments, it reduces the chances of introducing bugs.

Guide: Second Law of Test-Driven Development (TDD)

The second law of TDD is a principle in test-driven development that states, 'You should not write more of a unit test than is sufficient to fail, or compilation failures are failures.'

This law is crucial because it enhances the cycle of writing just enough code and tests, assisting in quick detection and correction of errors.

Under this law, developers write a failing unit test before writing functional code. The written code will then make the test pass. Subsequently, any new code should fail this test.

This process ensures that every piece of code is properly tested, thus reducing the possibility of bugs or defects. It also allows for a streamlined and efficient approach to coding.

Exam Tips: Answering Questions on Second Law of TDD

Understanding the principle behind the Second Law of TDD is critical for successfully answering exam questions about it. Here are crucial tips:

  • Understand the Basics: Thoroughly understand the principles and steps involved in TDD
  • Practical Applications: Have a good understanding of how this law is applied in a real-world scenario
  • Identify Relevant Scenarios: Be able to identify which scenarios warrant the use of the Second Law of TDD
  • Interpretation Skills: You should be able to interpret different contexts and apply the law accordingly
Test mode:

PMI-ACP - Second Law of TDD Example Questions

Test your knowledge of Second Law of TDD

Question 1

You are working on a program to calculate the area of a shape. You wrote a test for the 'calculateCircleArea' method but it failed because the method is not yet implemented. You now decide to write the 'calculateSquareArea' method. According to the second law of TDD, is this correct?

Question 2

You have written a failing test for a class method in your eCommerce application. Without making the test pass, you wrote a test for another feature. According to the Second Law of TDD, what should you have done differently?

Question 3

You've written a failing test for a 'SortItems' function. You decide to write just enough code to make the test pass. You then try improving the code to optimize the item sorting. Does this contradict the Second Law of TDD?

More Second Law of TDD questions
10 questions (total)