C# – Is a Unit test considered brittle if it fails when the business logic changes

cdomain-driven-designunit testingxunit

Please see the code below; it tests to see if a person with Gender of female is eligible for offer1:

[Fact]
public void ReturnsFalseWhenGivenAPersonWithAGenderOfFemale()
{
    var personId = Guid.NewGuid();
    var gender = "F";
    var person = new Person(personId, gender);

    var id = Guid.NewGuid();
    var offer1 = new Offer1(id,"Offer1");
    Assert.False(offer1.IsEligible(person));
}

This unit test succeeds. However, it will fail if 'Offer1' is offered to females in future.

Is it acceptable to say – if the business logic surrounding offer 1 changes then the unit test must change. Please note that in some cases (for some offers) the business logic is changed in the database like this:

update Offers set Gender='M' where offer=1;

and in some cases in the domain model like this:

if (Gender=Gender.Male)
{
  //do something
}

Please also note that in some cases the domain logic behind offers changes regularly and in some cases it does not.

Best Answer

This is not brittle in the usual sense. A unit test is considered brittle if it breaks due to implementation changes which does not affect the behavior under test. But if the business logic itself changes, then a test of this logic is supposed to break.

That said, if the business logic indeed changes often, perhaps it is not appropriate to hardcode the expectations into the unit tests. Instead you could test if the configurations in the database affects the offers as expected.

The name of the test Returns False When Given A Person With A Gender Of Female does not describe a business rule. A business rule would be something like Offers Applicable to M should not be applied to persons of gender F.

So you could write a test that confirms that if an offer is defined as only applicable to type M persons, then a type F person will not be indicated as eligible for it. This test will ensure the logic works even if the configuration of the specific offers change.