-1

Here is my class library code which I have C# and xUnit.

namespace XUnitSample.ClassLib
{
 public class DataAccess : IDataAccess
 {
 public void AddPersonToPeopleList(List<Person> people, Person person)
 {
 // TDD: 01 - Write a unit test which 'shouldfail'
 // If the 'shouldfail' unit test you wrote fail as expected, 
 // then your unit test is 'passed'.
 //people.Add(person);
 // TDD: 02 - Write a unit test which 'shouldpass'.
 // If the 'shouldpass' unit test you wrote fail as expected, 
 // then your unit test is 'passed'.
 people.Add(person);
 }
 }
}

And for my xUnit, I have one test class with 2 unit tests method.

public class DataAccessTest
{
 [Fact] 
 // TDD: 01 - Write a unit test which "shouldfail"
 // If the "shouldfail" unit test you wrote fail as expected, 
 // then your unit test is "passed".
 public void AddPersonToPeopleList_ShouldFail()
 {
 // Arrange
 var newPerson = new Person() { Id = 1, FullName = "John" };
 var people = new List<Person>();
 // Act
 var dataAccess = new DataAccess();
 dataAccess.AddPersonToPeopleList(people, newPerson);
 // Assert
 // If count is not 1, unit test is 'passed'
 Assert.True(people.Count != 1);
 // If people does not contain new person, unit test is 'passed'
 Assert.DoesNotContain<Person>(newPerson, people);
 }
 [Fact]
 //TDD: 02 - Write a unit test which 'shouldpass'.
 // If the 'shouldpass' unit test you wrote fail as expected, 
 // then your unit test is 'passed'.
 public void AddPersonToPeopleList_ShouldPass()
 {
 // Arrange
 var newPerson = new Person() { Id = 1, FullName = "John" };
 var people = new List<Person>();
 // Act
 var dataAccess = new DataAccess();
 dataAccess.AddPersonToPeopleList(people, newPerson);
 // Assert
 // If count == 1, unit test is 'passed'
 Assert.True(people.Count == 1); 
 // If people contain new person, unit test is 'passed'
 Assert.Contains<Person>(newPerson, people);
 }
}

Is this what it means by applying TDD principles of writing a unit test which shouldfail first and then write unit test which shouldpass?

asked Jun 15, 2020 at 2:17
2
  • 2
    This looks strange. The explanation for "should pass" and "should fail" are letter-for-letter identical. That makes no sense. Commented Jun 15, 2020 at 5:08
  • @JörgWMittag The copy paste bandit has struck again. Commented Jun 15, 2020 at 5:56

1 Answer 1

1

"Should fail" and "should pass" refer to the same test at different points in time, it doesn't mean that you write two separate tests. The TDD process starts with writing a failing unit test (failing because you haven't implemented the expected behavior yet) and then writing the code to make it pass.

In your example, the expected behavior is described by AddPersonToPeopleList_ShouldPass and this is the only test you need. You should write this before implementing the AddPersonToPeopleList method (at this point, the test will fail) and then go add the implementation (people.Add(person)) after which the test should pass.

answered Jun 15, 2020 at 2:45
5
  • 1
    Not 100% sure this was meant to be the red green cycle. I write "should fail" tests that are meant to cause exceptions. They pass when they do. We call them the "rainy day" tests. Commented Jun 15, 2020 at 6:02
  • 1
    See also the happy path Commented Jun 15, 2020 at 6:04
  • Many test frameworks have shouldFail or assert_fail_assertion / assert_raise or assertThrows for that use case. Commented Jun 15, 2020 at 6:58
  • Based on the sample code in the question, it did seem like the OP was confused about the red-green cycle. Both test cases have the same arrange and act steps, but opposite assertions. I would expect the "rainy day" tests you describe to have a different setup (such as invalid input). Commented Jun 15, 2020 at 7:32
  • Also the commented out code in the SUT (under TDD: 01) suggests that the OP was indeed trying to write a test case that "passed" before the code was written. Commented Jun 15, 2020 at 7:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.