1

I'm newbie in testing and received a notification from SonarCloud telling me that my DTO do not have any coverage, as it's just a class (a set of classes) I use for deserializing json I'm completely unaware how am I supposed to test them?

Dto definition:

using System.Text.Json.Serialization;

[JsonDerivedType(typeof(VehicleBaseDto), typeDiscriminator: "base")]
[JsonDerivedType(typeof(TrolleyDto), typeDiscriminator: "trolley")]
[JsonDerivedType(typeof(ScooterDto), typeDiscriminator: "scooter")]
[JsonDerivedType(typeof(ForkliftDto), typeDiscriminator: "forklift")]
public class VehicleBaseDto
{
    public string Name { get; set; } = string.Empty;

    public string CreationDate { get; set; }
}

public class TrolleyDto : VehicleBaseDto
{
    public TrolleyOptions? ExtendedData { get; set; }
}

public class ScooterDto : VehicleBaseDto
{
    public ScooterOptions? ExtendedData { get; set; }
}

public class ForkliftDto : VehicleBaseDto
{
    public ForkliftOptions? ExtendedData { get; set; }
}

This an example of a deserialization test I wrote using xUnit:

[Fact]
public void TrolleyDto_Deserialization_Success()
{
    var trolleyStr = @"{
       ""$type"":""trolley"",
       ""ExtendedData"":{
          ""Prop1"":""s/n"",
          ""Prop2"":""model"",
       },
       ""Name"":""test name"",
       ""CreationDate"":""some value""
    }";

    var trolleyObj = JsonSerializer.Deserialize<VehicleBaseDto>(trolleyStr);

    Assert.IsType<TrolleyDto>(trolleyObj);
}

I thought this usage of the DTO was enough for having some coverage on the DTO as the DTO don't have any methods to test, but SonarCloud says there is no coverage in any of its properties. What am I doing wrong?

4
  • 1
    DTOs typically don't contain any logic, why would you need to cover them with tests? Can't you exclude them from SonarQube? Note: A high code coverage doesn't say anything about the quality of the code or the value of the tests. Commented Jul 10, 2024 at 14:07
  • The test just asserts the type. Perhaps the code coverage tool also wants you to assert the property values therein (such that they match with the JSON string in the "arrange" step)? Accessing the properties in the test would cover them. (Though I am by no means disagreeing with the comment above about the value of this. But at the same time, sometimes the value is more political than technical. I've certainly had clients which insist on high test coverage as a line item on a report to a director somewhere.) Commented Jul 10, 2024 at 14:10
  • @David The last part you mentioned is my case, as it's supposed to be "easy" then it's required to cover also de DTO's Commented Jul 10, 2024 at 14:15
  • 1
    you can omit coverage for a type by providing the ExcludeFromCodeCoverageAttribute. Commented Jul 10, 2024 at 14:16

0

Your Answer

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.