docs icon indicating copy to clipboard operation
docs copied to clipboard

Incorrect text

Open Gooseman opened this issue 3 years ago • 7 comments

The text has the following: "A popular approach in test driven development (TDD) is to write a test before implementing the target code"

This is obviously incorrect. Writing a failing test before implementing the behaviour is the first step in TDD. One might argue that it is very, very slightly correct to claim that writing a test first is not a popular approach in TDD because writing a test is always the first step in TDD. But to claim that it's popular, is obviously false. And it is specifically writing a failing test that is the first step, not just writing a test.


Document Details

⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Gooseman avatar Feb 08 '23 11:02 Gooseman

Writing a test before implementing the target code is "popular" in TDD because it's step one. Not every test is written this way but the first one usually is or should be. And obviously it will fail - the code it's testing hasn't even been written yet.

ardalis avatar Feb 08 '23 14:02 ardalis

Incorrect. In TDD, a test for a behaviour is always written first. It is not even close to true that only the first test is always written before writing any code. Any claim to it being popular is simply not true.

It's also not always true that any test written before the behaviour it's testing will fail. It is entirely possible to write a test that is actually incorrect and ends up passing even though the behaviour it's supposed to test is not yet written. That is why TDD specifically states that a failing test is written before the behaviour to be tested. It is not stated that a test is written before the behaviour to be tested, but a failing test.

On Wed, 8 Feb 2023 at 14:12, Steve Smith @.***> wrote:

Writing a test before implementing the target code is "popular" in TDD because it's step one. Not every test is written this way but the first one usually is or should be. And obviously it will fail - the code it's testing hasn't even been written yet.

— Reply to this email directly, view it on GitHub https://github.com/dotnet/docs/issues/33896#issuecomment-1422660929, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6QMOF6EEUD2AH4ZZ5ZCPDWWOSUHANCNFSM6AAAAAAUVEYRQQ . You are receiving this because you authored the thread.Message ID: @.***>

Gooseman avatar Feb 08 '23 14:02 Gooseman

We may be talking past one another. When I say "before implementing the target code" I mean literally, before writing any code.

So, my test:

public class GreetingServiceGreet
{
  [Fact]
  public void ReturnsHelloWorld()
  {
    var greetingService = new GreetingService(); // doesn't yet compile

    string result = greetingService.Greet();

    Assert.Equal("Hello World", result);
  } 
}

Obviously this fails, since it doesn't even compile. This is literally the first test.

Ok, so I go hit ctrl-. and tell my IDE to implement GreetingService.Greet() for me (from the test code). It generates something like this:

public class GreetingService
{
  public string Greet()
  {
    throw new NotImplementedException();
  }
}

Now it compiles, but it will still always fail. Why? Because I haven't yet implemented it. This is what I am saying. Am I not communicating that effectively?

ardalis avatar Feb 08 '23 15:02 ardalis

Also, having now written the first test before writing (or in this case, generating) the code for the service, it is also true from this point forward that only the first test was written before "writing any code." At this point, I've written some SUT code, so any tests that occur after this will obviously not have been written before I wrote any SUT code.

ardalis avatar Feb 08 '23 15:02 ardalis

I do concede that it probably adds some clarity to state that it should be a "failing" test since there are certainly cases where the structure of the SUT code is present but new behavior hasn't been added to it. That's clearly not the example being show in the referenced doc (where the SUT method IsPrime hasn't yet been defined/implemented), but it would make the statement more true in the general case.

ardalis avatar Feb 08 '23 15:02 ardalis

It is simply false to claim that wiring a test first is popular in TDD. If you are describing an approach where writing a test first is simply popular then you are not describing TDD.

In TDD, no system under test code is written before the test for the behaviour to be implemented. If that were not the case then it could not be claimed to be test-driven. If the test is not specifically a failing test then it is not TDD. If there is an application that has just been created, as is the case in the example, and someone writes an empty test case, that test does not fail. No behaviour to test has been written, but the test does not fail.

Regardless of whether there is or is not code, it is false to claim that writing a test first is just popular in TDD when it is always the first step, not just a popular first step.

On Wed, 8 Feb 2023 at 15:44, Steve Smith @.***> wrote:

I do concede that it probably adds some clarity to state that it should be a "failing" test since there are certainly cases where the structure of the SUT code is present but new behavior hasn't been added to it. That's clearly not the example being show in the referenced doc (where the SUT method IsPrime hasn't yet been defined/implemented), but it would make the statement more true in the general case.

— Reply to this email directly, view it on GitHub https://github.com/dotnet/docs/issues/33896#issuecomment-1422827167, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6QMOGHJI5CD66ZPT2IBZTWWO5NBANCNFSM6AAAAAAUVEYRQQ . You are receiving this because you authored the thread.Message ID: @.***>

Gooseman avatar Feb 08 '23 15:02 Gooseman

https://github.com/dotnet/docs/pull/33922 does not fix the issue I've raised. The text contains a clearly incorrect reference to TDD. Claiming that 33922 addresses the problem is the same as saying that the dissemination of false claims about TDD is deliberate.

Gooseman avatar Feb 09 '23 14:02 Gooseman