.NET by Kurt

Testing in .NET

Automated testing in .NET is a crucial software development practice that involves using specialized tools and frameworks to create and execute tests automatically. It verifies the correctness of code, detects bugs early in the development process, and ensures that changes do not introduce new issues. .NET offers robust testing libraries like MSTest, NUnit, and xUnit, along with tools like Visual Studio Test Explorer, to simplify the creation and execution of unit, integration, and UI tests, enhancing the reliability and maintainability of .NET applications.

...see more

In the .Net Framework, the approach to unit testing internal methods or types was to add an InternalsVisibleTo attribute into the AssemblyInfo.cs file. For example, look at the following line of code below:

InternalsVisibleTo Attribute

[assembly: InternalsVisibleTo("Projects.Tests")]

This all works fine in the .net Framework. However, in .Net Core, most of the settings in the AssemblyInfo file have been moved to the project file. So, to test internal methods, you have two options.

Project File Change

Assuming we have a project called Projects and a unit test project called Projects.Tests. The Projects project file, which contains the internal method, can be modified in Visual Studio with the following:

AssemblyAttribute

<ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
        <_Parameter1>Projects.Tests</_Parameter1>
    </AssemblyAttribute>
</ItemGroup>

Starting with .NET 5, you can use the <InternalsVisibleTo> without adding any NuGet package:

<ItemGroup>
    <InternalsVisibleTo Include="Projects.Tests" />
</ItemGroup>

Source File change

The alternative way is to use an attribute in the source file containing the internal method. For example, see the code below:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Projects.Test")]

namespace Projects
{
    public class User
    {
        internal string Hello(string username)
        {
            return $"Hello {username}";
        }
    }
}

Additional Resources:

...see more

This is a unit test method written in C# using the xunit framework. It is testing the behavior of a method named GetFolderAsync from a class called FileSystem.

[Fact( DisplayName = "get folder async should throw exception for non-existent folder" )]
public async Task GetFolderAsyncShouldThrowExceptionForNonExistentFolder()
{
    var fileSystem = new FileSystem();
    var ex = await Assert.ThrowsAsync<DirectoryNotFoundException>( () => fileSystem.GetFolderAsync( @"C:\blah" ) );
}

This xUnit test method is named "GetFolderAsyncShouldThrowExceptionForNonExistentFolder." It is an asynchronous test that checks whether calling the GetFolderAsync method on a fileSystem object with the path @"C:\blah" results in a DirectoryNotFoundException being thrown. The [Fact] attribute marks it as a fact test case, and the DisplayName attribute provides a custom name for the test. This test aims to ensure that the GetFolderAsync method correctly throws an exception when trying to access a non-existent folder.

Comments