How do you do this at all, using c#
and Sonar
?
Given the code:
if (root?.hop1?.hop2.hop3?.value == "blah") // this condition is not covered as far as Sonar is concerned
{
do(); // this line is covered as far as Sonar is concerned
}
we can create an object to test do()
, but how do you cover the actual condition? Supplying just a “happy” path leaves the condition uncovered, should we supply all the permutations constituting unhappy paths (which is a nightmare as this code is in the middle of another test) or how do you do it otherwise?
19
What you can do is to define parameter for test method and provide it ClassData
attribute with type of data generator, which would produce all combinations.
Considering such setup:
public class Innermost
{
public string Value { get; set; }
}
public class Inner
{
public Innermost Innermost { get; set; }
}
public class Outer
{
public Inner Inner { get; set; }
}
public class Outermost
{
public Outer Outer { get; set; }
}
public static class ClassUnderTest
{
public static string? GetString(Outermost o)
{
if(o?.Outer?.Inner?.Innermost?.Value == "value")
{
return "value";
}
return "";
}
}
You could define data generator in separate class:
public class TestDataGenerator : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
var outerMost = new Outermost();
yield return [outerMost];
outerMost = new Outermost();
outerMost.Outer = new Outer();
yield return [outerMost];
outerMost = new Outermost();
outerMost.Outer = new Outer();
outerMost.Outer.Inner = new Inner();
yield return [outerMost];
outerMost = new Outermost();
outerMost.Outer = new Outer();
outerMost.Outer.Inner = new Inner();
outerMost.Outer.Inner.Innermost = new Innermost();
yield return [outerMost];
outerMost = new Outermost();
outerMost.Outer = new Outer();
outerMost.Outer.Inner = new Inner();
outerMost.Outer.Inner.Innermost = new Innermost();
outerMost.Outer.Inner.Innermost.Value = "random string";
yield return [outerMost];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
And then use it inside test:
public class UnitTest1
{
[Theory]
[ClassData(typeof(TestDataGenerator))]
public void Test1(Outermost outerMost)
{
var result = ClassUnderTest.GetString(outerMost);
Assert.Equal("", result);
}
}
This way all data generation will be separated in different class. There you could also leverage AutoFixture package to further simplify object creation in unit tests.
3
My answer is – in a situation when an expression containing “?” is evaluated to a value or checked with a standalone if
branch it’s completely unnecessary to demand for the values representing “early exits”, it adds nothing but the hustle.
I’d suggest to refactor SUT code to use something like target.Evaluate<T>("hop1.hop2.hop3")
.