DRN.Framework.Testing 0.1.0-preview008

Prefix Reserved
This is a prerelease version of DRN.Framework.Testing.
There is a newer version of this package available.
See the version list below for details.
dotnet add package DRN.Framework.Testing --version 0.1.0-preview008
                    
NuGet\Install-Package DRN.Framework.Testing -Version 0.1.0-preview008
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DRN.Framework.Testing" Version="0.1.0-preview008" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DRN.Framework.Testing" Version="0.1.0-preview008" />
                    
Directory.Packages.props
<PackageReference Include="DRN.Framework.Testing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DRN.Framework.Testing --version 0.1.0-preview008
                    
#r "nuget: DRN.Framework.Testing, 0.1.0-preview008"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DRN.Framework.Testing@0.1.0-preview008
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DRN.Framework.Testing&version=0.1.0-preview008&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=DRN.Framework.Testing&version=0.1.0-preview008&prerelease
                    
Install as a Cake Tool

DRN.Framework.Testing

DRN.Framework.Testing package provides practical, effective helpers such as resourceful data attributes and test context.

This package enables a new encouraging testing technique called as DTT(Duran's Testing Technique). With DTT, any developer can write clean and hassle-free unit and integration tests without complexity.

Following packages are encapsulated with this package and no longer needed to be referenced in your test project:

  • AutoFixture.AutoNSubstitute
  • AutoFixture.Xunit2
  • DRN.Framework.Utils
  • FluentAssertions
  • NSubstitute
  • xunit

Quickstart

Here's a basic test demonstration to get you started:

public class DataInlineContextAttributeTests
{
    /// <param name="context"> Provided by DataInlineContext even if it is not a compile time constant</param>
    /// <param name="autoInlinedDependency">DataInlineContext will provide implementation mocked by NSubstitute</param>
    [Theory]
    [DataInlineContext]
    public void DataInlineContextDemonstration(TestContext context, IMockable autoInlinedDependency)
    {
        autoInlinedDependency.Max.Returns(int.MaxValue); //dependency mocked by NSubstitute

        context.ServiceCollection.AddApplicationServices(); //you can add services, modules defined in hosted app, application, infrastructure layer etc..
        var serviceProvider = context.BuildServiceProvider(); //appsettings.json added by convention. Context and service provider will be disposed by xunit
        var dependentService = serviceProvider.GetRequiredService<DependentService>(); //context replaces actual dependencies with auto inlined dependencies

        dependentService.Max.Should().Be(int.MaxValue);
    }
} 

Testing models used in demonstration:


public static class ApplicationModule //Can be defined in Application Layer or in Hosted App
{
    public static void AddApplicationServices(this IServiceCollection serviceCollection)
    {
        serviceCollection.AddTransient<IMockable, ToBeRemovedService>(); //will be removed by test context because test method requested mocked interface
        serviceCollection.AddTransient<DependentService>(); //dependent service uses IMockable and Max property returns dependency's Max value
    }
}

public interface IMockable
{
    public int Max { get; }
}

public class ToBeRemovedService : IMockable
{
    public int Max { get; set; }
}

public class DependentService : IMockable
{
    private readonly IMockable _mockable;

    public DependentService(IMockable mockable)
    {
        _mockable = mockable;
    }

    public int Max => _mockable.Max;
}

Advanced Data Inline

  • DataInlineContext will provide TestContext as first parameter.
  • Then it will provide inlined values.
  • Then it will provide auto inline missing values with AutoFixture.
  • AutoFixture will mock any interface requested with NSubstitute.
/// <param name="context"> Provided by DataInlineContext even if it is not a compile time constant</param>
/// <param name="inlineData">Provided by DataInlineContext</param>
/// <param name="autoInlinedData">DataInlineContext will provide missing data with the help of AutoFixture</param>
/// <param name="autoInlinedMockable">DataInlineContext will provide implementation mocked by NSubstitute</param>
[Theory]
[DataInlineContext(99)]
public void TextContext_Should_Be_Created_From_TestContextData(TestContext context, int inlineData, Guid autoInlinedData, IMockable autoInlinedMockable)
{
    inlineData.Should().Be(99);
    autoInlinedData.Should().NotBeEmpty(); //guid generated by AutoFixture
    autoInlinedMockable.Max.Returns(int.MaxValue); //dependency mocked by NSubstitute

    context.ServiceCollection.AddApplicationServices(); //you can add services, modules defined in hosted app, application, infrastructure layer etc..
    var serviceProvider = context.BuildServiceProvider(); //appsettings.json added by convention. Context and service provider will be disposed by xunit
    serviceProvider.GetService<ToBeRemovedService>().Should().BeNull(); //Service provider behaviour demonstration

    var dependentService = serviceProvider.GetRequiredService<DependentService>();
    dependentService.Max.Should().Be(int.MaxValue);
}

TestContext

TestContext has following properties:

  • captures values provided to running test method and its method info.
  • provides ServiceCollection so that to be tested services and dependencies can be added before building ServiceProvider.
  • provides lightweight ServiceProvider that contains default logging without any provider
    • ServiceProvider can provide services that depends like ILogger<DefaultService>
    • logged data will not be leaked to anywhere since it has no logging provider.
  • ServiceProvider provides IConfiguration and IAppSettings with SettingsProvider.
    • SettingsProvider reads json settings files that can be found in the settings folder of the test project
    • Make sure file is copied to output directory
    • If no settings file is specified while calling BuildServiceProvider appsettings.json file be searched by convention.
  • ServiceProvider provides utils provided with DRN.Framework.Utils' UtilsModule
  • BuildServiceProvider replaces dependencies that can be replaced with inlined interfaces.
  • ServiceProvider and TestContext will be disposed by xunit when test finishes

Data Attributes

DRN.Framework.Testing provides following data attributes that can provide data to tests:

  • DataInlineAutoAttribute
  • DataInlineContextAttribute
  • DataMemberAutoAttribute
  • DataMemberContextAttribute
  • DataSelfAutoAttribute
  • DataSelfContextAttribute

Following design principle is used for these attributes

  • All attributes has data prefix to benefit from autocomplete
  • Inline attributes works like xunit InlineData except they try to provide missing values with AutoFixture and NSubstitute
  • Member attributes works like xunit MemberData except they try to provide missing values with AutoFixture and NSubstitute
  • Self attributes needs to be inherited by another class and should call AddRow method in constructor to provide data
  • Context attributes provide TestContext as first parameter

Member Attributes

Example usages for member attributes

[Theory]
[DataMemberAuto(nameof(DataMemberAutoData))]
public void AutoMember_Should_Inline_And_Auto_Generate_Missing_Test_Data(int inline, ComplexInline complexInline, Guid autoGenerate, IMockable mock)
{
    inline.Should().BeGreaterThan(10);
    complexInline.Count.Should().BeLessThan(10);
    autoGenerate.Should().NotBeEmpty();
    mock.Max.Returns(75);
    mock.Max.Should().Be(75);
}

public static IEnumerable<object[]> DataMemberAutoData => new List<object[]>
{
    new object[] { 11, new ComplexInline(8) },
    new object[] { int.MaxValue, new ComplexInline(-1) }
};
[Theory]
[DataMemberContext(nameof(TestContextInlineMemberData))]
public void TestContextMember_Should_Inline_And_Auto_Generate_Missing_Test_Data(TestContext testContext,
    int inline, ComplexInline complexInline, Guid autoGenerate, IMockable mock)
{
    testContext.Should().NotBeNull();
    testContext.TestMethod.Name.Should().Be(nameof(TestContextMember_Should_Inline_And_Auto_Generate_Missing_Test_Data));
    inline.Should().BeGreaterThan(10);
    complexInline.Count.Should().BeLessThan(10);
    autoGenerate.Should().NotBeEmpty();
    mock.Max.Returns(75);
    mock.Max.Should().Be(75);
}

public static IEnumerable<object[]> TestContextInlineMemberData => new List<object[]>
{
    new object[] { 11, new ComplexInline(8) },
    new object[] { int.MaxValue, new ComplexInline(-1) }
};

Self Attributes

Example usages for self attributes

public class DataSelfAutoAttributeTests
{
    [Theory]
    [DataSelfAutoTestData]
    public void AutoClass_Should_Inline_And_Auto_Generate_Missing_Test_Data(int inline, ComplexInline complexInline, Guid autoGenerate, IMockable mock)
    {
        inline.Should().BeGreaterThan(10);
        complexInline.Count.Should().BeLessThan(10);
        autoGenerate.Should().NotBeEmpty();
        mock.Max.Returns(75);
        mock.Max.Should().Be(75);
    }
}

public class DataSelfAutoTestData : DataSelfAutoAttribute
{
    public DataSelfAutoTestData()
    {
        AddRow(200, new ComplexInline(9));
        AddRow(300, new ComplexInline(int.MinValue));
    }
}
public class DataSelfContextAttributeTests
{
    [Theory]
    [DataSelfContextTestData]
    public void TestContextClassData_Should_Inline_And_Auto_Generate_Missing_Test_Data(TestContext testContext,
        int inline, ComplexInline complexInline, Guid autoGenerate, IMockable mock)
    {
        testContext.Should().NotBeNull();
        testContext.TestMethod.Name.Should().Be(nameof(TestContextClassData_Should_Inline_And_Auto_Generate_Missing_Test_Data));
        inline.Should().BeGreaterThan(98);
        complexInline.Count.Should().BeLessThan(1001);
        autoGenerate.Should().NotBeEmpty();
        mock.Max.Returns(44);
        mock.Max.Should().Be(44);
    }
}

public class DataSelfContextTestData : DataSelfContextAttribute
{
    public DataSelfContextTestData1()
    {
        AddRow(99,new ComplexInline(100));
        AddRow(199,new ComplexInline(1000));
    }
}

Inline Attributes

Example usages for inline attributes

[Theory]
[DataInlineAuto(10)]
public void AutoInline_Should_Inline_And_Auto_Generate_Missing_Test_Data(int inline, Guid autoGenerate, IMockable mock)
{
    inline.Should().Be(10);
    autoGenerate.Should().NotBeEmpty();
    mock.Max.Returns(65);
    mock.Max.Should().Be(65);
}
[Theory]
[DataInlineContext(99)]
public void TextContext_Should_Be_Created_From_TestContextData(TestContext context, int inlineData, Guid autoInlinedData, IMockable autoInlinedMockable)
{
    inlineData.Should().Be(99);
    autoInlinedData.Should().NotBeEmpty(); //guid generated by AutoFixture
    autoInlinedMockable.Max.Returns(int.MaxValue); //dependency mocked by NSubstitute

    context.ServiceCollection.AddApplicationServices(); //you can add services, modules defined in hosted app, application, infrastructure layer etc..
    var serviceProvider = context.BuildServiceProvider(); //appsettings.json added by convention. Context and service provider will be disposed by xunit
    serviceProvider.GetService<ToBeRemovedService>().Should().BeNull(); //Service provider behaviour demonstration

    var dependentService = serviceProvider.GetRequiredService<DependentService>();
    dependentService.Max.Should().Be(int.MaxValue);
}

Global Usings

following global usings can be used in a Usings file in test projects to reduce line of code in test files

global using Xunit;
global using AutoFixture;
global using AutoFixture.AutoNSubstitute;
global using AutoFixture.Xunit2;
global using DRN.Framework.Testing;
global using DRN.Framework.Utils.Extensions;
global using DRN.Framework.Utils.Settings;
global using FluentAssertions;
global using Microsoft.Extensions.DependencyInjection;
global using NSubstitute;
global using System.Reflection;
global using System.IO;
global using System.Linq;
global using DRN.Framework.Testing.DataAttributes;

Commit Info

Author: Duran Serkan KILIÇ
Date: 2023-10-28 00:10:40 +0300
Hash: c86d77f1b42c8f428ad4e7723ef7e4e1ee18826b

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.7.0-preview037 80 9/28/2025
0.7.0-preview036 172 9/22/2025
0.7.0-preview035 162 8/31/2025
0.7.0-preview034 167 8/31/2025
0.7.0-preview033 203 8/28/2025
0.7.0-preview032 196 8/27/2025
0.7.0-preview031 167 8/10/2025
0.7.0-preview030 54 8/1/2025
0.7.0-preview029 77 8/1/2025
0.7.0-preview028 81 8/1/2025
0.7.0-preview027 115 7/31/2025
0.7.0-preview026 96 7/29/2025
0.7.0-preview025 120 7/27/2025
0.7.0-preview024 89 7/11/2025
0.7.0-preview023 103 7/11/2025
0.7.0-preview022 159 6/29/2025
0.7.0-preview021 165 6/23/2025
0.7.0-preview020 115 5/31/2025
0.7.0-preview019 173 3/23/2025
0.7.0-preview018 114 3/2/2025
0.7.0-preview017 123 2/23/2025
0.7.0-preview016 144 2/22/2025
0.7.0-preview015 113 2/21/2025
0.7.0-preview014 137 2/20/2025
0.7.0-preview013 131 2/9/2025
0.7.0-preview012 123 2/8/2025
0.7.0-preview011 111 2/2/2025
0.7.0-preview010 117 1/20/2025
0.7.0-preview009 96 1/19/2025
0.7.0-preview008 88 1/16/2025
0.7.0-preview007 109 12/29/2024
0.7.0-preview006 110 12/23/2024
0.7.0-preview005 124 11/27/2024
0.7.0-preview004 107 11/23/2024
0.7.0-preview003 106 11/20/2024
0.7.0-preview002 94 11/17/2024
0.7.0-preview001 120 11/14/2024
0.6.0 167 11/10/2024
0.6.0-preview002 100 11/10/2024
0.6.0-preview001 120 11/10/2024
0.5.1-preview002 116 9/30/2024
0.5.1-preview001 125 9/22/2024
0.5.0 151 8/30/2024
0.5.0-preview011 120 8/30/2024
0.5.0-preview010 148 8/25/2024
0.5.0-preview009 149 8/8/2024
0.5.0-preview008 118 8/7/2024
0.5.0-preview007 102 8/2/2024
0.5.0-preview006 99 7/30/2024
0.5.0-preview005 121 7/27/2024
0.5.0-preview004 117 7/15/2024
0.5.0-preview003 130 6/6/2024
0.5.0-preview002 129 6/5/2024
0.5.0-preview001 137 6/4/2024
0.4.0 133 5/19/2024
0.4.0-preview006 112 5/19/2024
0.4.0-preview005 115 5/12/2024
0.4.0-preview004 135 5/12/2024
0.4.0-preview003 139 5/11/2024
0.4.0-preview002 148 5/8/2024
0.4.0-preview001 161 5/5/2024
0.3.1-preview001 125 4/26/2024
0.3.0 166 4/23/2024
0.3.0-preview002 108 4/23/2024
0.3.0-preview001 122 4/23/2024
0.2.2-preview010 149 4/11/2024
0.2.2-preview009 142 3/18/2024
0.2.2-preview008 146 3/18/2024
0.2.2-preview007 157 3/16/2024
0.2.2-preview006 120 3/11/2024
0.2.2-preview005 121 3/10/2024
0.2.2-preview004 118 3/10/2024
0.2.2-preview003 174 1/22/2024
0.2.2-preview002 140 1/18/2024
0.2.2-preview001 151 1/14/2024
0.2.1 233 1/7/2024
0.2.0 191 12/31/2023
0.2.0-preview009 157 12/31/2023
0.2.0-preview008 158 12/30/2023
0.2.0-preview007 162 12/28/2023
0.2.0-preview006 134 12/27/2023
0.2.0-preview005 146 12/25/2023
0.2.0-preview004 147 12/23/2023
0.2.0-preview003 165 12/20/2023
0.2.0-preview002 162 12/19/2023
0.2.0-preview001 158 12/18/2023
0.1.0 227 11/26/2023
0.1.0-preview013 156 11/26/2023
0.1.0-preview012 136 11/20/2023
0.1.0-preview011 150 11/19/2023
0.1.0-preview010 191 10/30/2023
0.1.0-preview009 138 10/29/2023
0.1.0-preview008 150 10/27/2023
0.1.0-preview007 149 10/11/2023
0.1.0-preview006 148 10/9/2023
0.1.0-preview005 160 10/8/2023
0.1.0-preview004 158 10/8/2023
0.1.0-preview003 156 10/3/2023
0.1.0-preview002 172 10/3/2023
0.1.0-preview001 156 10/2/2023

Not every version includes changes, features or bug fixes. This project can increment version to keep consistency with other DRN.Framework projects.

## Version 0.1.0

### Breaking Changes

### New Features
Following data attributes added:
* DataInlineAutoAttribute
* DataInlineContextAttribute
* DataMemberAutoAttribute
* DataMemberContextAttribute
* DataSelfAutoAttribute
* DataSelfContextAttribute

### Bug Fixes  
 
### Commit Info  
Author: Duran Serkan KILIÇ  
Date: 2023-10-28 00:10:40 +0300  
Hash: c86d77f1b42c8f428ad4e7723ef7e4e1ee18826b