Skip to content

Improve unit test coverage under linuxΒ #265

@glennawatson

Description

@glennawatson

Here are the requested materials to help increase the code coverage for reactiveui.uno.

GitHub Issue

Title: Increase Code Coverage for ReactiveUI.Uno to 60%+

Body:

Description

The current code coverage for the ReactiveUI.Uno project is below our desired threshold. This issue tracks the effort to increase the test coverage to a minimum of 60%, with an ideal target of 80% or higher. The focus is on the code paths reachable on a Linux environment running .NET 9.

Tasks

  1. Write new unit tests: Add NUnit tests for uncovered and partially covered classes and methods in the ReactiveUI.Uno project.
  2. Focus on core logic: Prioritize testing business logic, complex methods, and areas prone to regressions.
  3. Adhere to testing guidelines: Follow the testing best practices outlined below.

How to Measure Code Coverage

We use coverlet.msbuild to generate code coverage reports. You can generate a report by running the following command from the src directory on a Linux machine:

dotnet test ReactiveUI.Uno.sln \
    /p:CollectCoverage=true \
    /p:CoverletOutputFormat=opencover \
    /p:CoverletOutput=./coverage.opencover.xml

This command will execute the tests and create a file named coverage.opencover.xml in the src directory. This file contains the detailed coverage report.

How to Parse the Coverage Report

To easily check the current coverage percentage, you can use the following C# script. It parses the coverage.opencover.xml file and prints the line coverage.

CoverageParser.cs

using System;
using System.Linq;
using System.Xml.Linq;

public class CoverageParser
{
    public static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("Usage: CoverageParser <path_to_coverage.xml>");
            return;
        }

        var coverageFile = args[0];
        if (!System.IO.File.Exists(coverageFile))
        {
            Console.WriteLine($"Error: File not found at '{coverageFile}'");
            return;
        }

        try
        {
            var doc = XDocument.Load(coverageFile);
            var summary = doc.Root?.Element("Summary");

            if (summary != null)
            {
                var lineCoverage = summary.Attribute("sequenceCoverage")?.Value;
                Console.WriteLine($"Line Coverage: {lineCoverage}%");
            }
            else
            {
                Console.WriteLine("Could not find the summary element in the coverage file.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred while parsing the file: {ex.Message}");
        }
    }
}

How to compile and run the parser:

  1. Save the code above as CoverageParser.cs.
  2. Compile it: csc CoverageParser.cs
  3. Run it against your coverage file: ./CoverageParser coverage.opencover.xml

This will output the current line coverage percentage, allowing you to track your progress towards the 60% goal.

Testing Guidelines

  • Framework: Use NUnit for all new tests.
  • Assertion Style: Use the modern NUnit constraint-based assertion model.
    • DO: Assert.That(actual, Is.EqualTo(expected));
    • DO NOT: Assert.AreEqual(expected, actual);
  • Constraints: Familiarize yourself with and use the NUnit constraints: [NUnit Constraints Documentation](https://docs.nunit.org/articles/nunit/writing-tests/constraints/Constraints.html)
  • MultiScope Operator: Prefer the MultiScope operator where applicable for more complex assertions.
  • Clarity and Readability: Write clear, concise, and well-documented tests.

Prompt for a Developer/AI

Here is a prompt you can use to delegate this task.


I need your help to increase the code coverage for the reactiveui.uno library. The current coverage is around 5%, and we need to get it to a minimum of 60%, with a stretch goal of 80%. All work should be done on a Linux environment targeting .NET 9.

Your Task

Your primary task is to write NUnit unit tests for the ReactiveUI.Uno project to increase its code coverage.

How to Get Started

  1. Clone the repository: Make sure you have the latest version of the reactiveui.uno code.

  2. Generate a baseline coverage report: To see the current coverage, navigate to the src directory and run the following command. This will run all the tests and generate a coverage report.

    dotnet test ReactiveUI.Uno.sln \
        /p:CollectCoverage=true \
        /p:CoverletOutputFormat=opencover \
        /p:CoverletOutput=./coverage.opencover.xml
  3. Analyze the report: The command from the previous step creates a file named coverage.opencover.xml. You need to know the overall line coverage from this file. Here's a small C# program that you can use to parse this file and get the line coverage percentage.

    CoverageParser.cs

    using System;
    using System.Linq;
    using System.Xml.Linq;
    
    public class CoverageParser
    {
        public static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: CoverageParser <path_to_coverage.xml>");
                return;
            }
    
            var coverageFile = args[0];
            if (!System.IO.File.Exists(coverageFile))
            {
                Console.WriteLine($"Error: File not found at '{coverageFile}'");
                return;
            }
    
            try
            {
                var doc = XDocument.Load(coverageFile);
                var summary = doc.Root?.Element("Summary");
    
                if (summary != null)
                {
                    var lineCoverage = summary.Attribute("sequenceCoverage")?.Value;
                    Console.WriteLine($"Line Coverage: {lineCoverage}%");
                }
                else
                {
                    Console.WriteLine("Could not find the summary element in the coverage file.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred while parsing the file: {ex.Message}");
            }
        }
    }

    To use the parser:

    • Save the code as CoverageParser.cs.
    • Compile it with csc CoverageParser.cs.
    • Run it with ./CoverageParser coverage.opencover.xml.

    This will show you the current line coverage. Your goal is to get this number to at least 60%.

  4. Write Tests:

    • Identify classes and methods in ReactiveUI.Uno that have low or no coverage.
    • Write new NUnit tests in the ReactiveUI.Uno.Tests project to cover this code.
    • Focus on testing the logic and functionality of the methods.

Important Testing Rules

  • Use NUnit: All tests should be written using the NUnit framework.
  • Modern Assertions Only: You must use NUnit's constraint-based assertion model. Do not use the classic Assert.AreEqual, Assert.IsTrue, etc.
    • Correct: Assert.That(myValue, Is.True);
    • Incorrect: Assert.IsTrue(myValue);
  • Constraints are Key: Use the rich set of constraints available in NUnit. You can find the documentation for them here: https://docs.nunit.org/articles/nunit/writing-tests/constraints/Constraints.html
  • Prefer MultiScope: For more complex assertions, the MultiScope operator is preferred.

Definition of Done

  • The line coverage of ReactiveUI.Uno as reported by coverlet is at least 60% but aim for 80%.
  • All new tests are written using NUnit and follow the assertion guidelines mentioned above.
  • All existing tests continue to pass.

Let me know if you have any questions!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions