I have class Foo
:
class Foo
{
public string Bar { get; set; }
}
I have lots of tests which reference this class. How I can ensure that all new properties are tested when my system evolves and I extend Foo
with new property?
I.e. after some time I will add a new property to Foo
class:
class Foo
{
public string Bar { get; set; }
public string Baz { get; set; }
}
I need to review all code which tests my class and ensure that all properties are tested - which is manual process, hard to always get right.
I have class Foo
:
class Foo
{
public string Bar { get; set; }
}
I have lots of tests which reference this class. How I can ensure that all new properties are tested when my system evolves and I extend Foo
with new property?
I.e. after some time I will add a new property to Foo
class:
class Foo
{
public string Bar { get; set; }
public string Baz { get; set; }
}
I need to review all code which tests my class and ensure that all properties are tested - which is manual process, hard to always get right.
Share Improve this question edited Feb 2 at 20:57 Basheer Jarrah 3963 silver badges10 bronze badges asked Jan 29 at 23:18 ShadowShadow 2,4786 gold badges30 silver badges60 bronze badges 3- 3 You'll likely need to build a Roslyn analyzer that's invoked via a test - or use a code-coverage library. BTW, consider using immutable types with constructors instead of mutable properties: once you make the change you never look back (and then you start learning F#, OCaml and eventually Haskell, and then it's all downhill from there – Dai Commented Jan 29 at 23:20
- 1 Even if you have mutable immutable type with constructor you still can add property to this type (i.e. in service), and verify result in tests. This does not really solve the problem. – Shadow Commented Jan 30 at 0:13
- "Baz" introduces one or more new interfaces: IFooBar; IFooBaz; IFooBarBaz. Tells you what and where. – Gerry Schmitz Commented Jan 30 at 1:49
1 Answer
Reset to default 0There is not much you can do actually to guarantee this. You can try writing a Roslyn analyzer to check for assertions but arguably it would be not worth the effort and probably would not be that feasible.
As alternative you can rely on code coverage tools (integrated into the build pipeline) and setting high coverage target (90+ %).
Another option would be just moving assertions to a method (or set of methods) like:
public void AssertFoo(string expectedBar, string expectedBaz)
{
//
}
Or
public void AssertFoo(Func<string, bool> assertBar, Func<string, bool> assertBaz)
{
//
}
And substitute your assertions to call for such function. And when a new property will be added it would be easy to just modify those functions and code will automatically become incompatible.