DRN.Framework.Testing
0.1.0-preview009
Prefix Reserved
See the version list below for details.
dotnet add package DRN.Framework.Testing --version 0.1.0-preview009
NuGet\Install-Package DRN.Framework.Testing -Version 0.1.0-preview009
<PackageReference Include="DRN.Framework.Testing" Version="0.1.0-preview009" />
<PackageVersion Include="DRN.Framework.Testing" Version="0.1.0-preview009" />
<PackageReference Include="DRN.Framework.Testing" />
paket add DRN.Framework.Testing --version 0.1.0-preview009
#r "nuget: DRN.Framework.Testing, 0.1.0-preview009"
#:package DRN.Framework.Testing@0.1.0-preview009
#addin nuget:?package=DRN.Framework.Testing&version=0.1.0-preview009&prerelease
#tool nuget:?package=DRN.Framework.Testing&version=0.1.0-preview009&prerelease
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
{
[Theory]
[DataInlineContext]
public void DataInlineContextDemonstration(TestContext context, IMockable autoInlinedDependency)
{
autoInlinedDependency.Max.Returns(int.MaxValue); //dependency is 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
DataInlineContextwill provideTestContextas first parameter.- Then it will provide inlined values.
- Then it will provide auto inline missing values with AutoFixture.
AutoFixturewill mock any interface requested withNSubstitute.
/// <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
ServiceCollectionso that to be tested services and dependencies can be added before buildingServiceProvider. - provides lightweight
ServiceProviderthat contains default logging without any providerServiceProvidercan provide services that depends likeILogger<DefaultService>- logged data will not be leaked to anywhere since it has no logging provider.
ServiceProviderprovidesIConfigurationandIAppSettingswith 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
BuildServiceProviderappsettings.jsonfile be searched by convention.
ServiceProviderprovides utils provided with DRN.Framework.Utils'UtilsModuleBuildServiceProviderreplaces dependencies that can be replaced with inlined interfaces.ServiceProviderandTestContextwill 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
InlineDataexcept they try to provide missing values with AutoFixture and NSubstitute - Member attributes works like xunit
MemberDataexcept they try to provide missing values with AutoFixture and NSubstitute - Self attributes needs to be inherited by another class and should call
AddRowmethod in constructor to provide data - Context attributes provide
TestContextas 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;
DebugOnly Tests
Following attributes can be used to run test only when the debugger is attached. These attributes does not respect debugger not debug configuration.
- FactDebuggerOnly
- TheoryDebuggerOnly
Commit Info
Author: Duran Serkan KILIÇ
Date: 2023-10-29 22:20:04 +0300
Hash: 6ed278f1c39475dd982619b8a35f89635b66d995
| Product | Versions 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. |
-
net7.0
- AutoFixture.AutoNSubstitute (>= 4.18.0)
- AutoFixture.Xunit2 (>= 4.18.0)
- DRN.Framework.Utils (>= 0.1.0-preview009)
- FluentAssertions (>= 6.12.0)
- NSubstitute (>= 5.0.0)
- xunit (>= 2.5.0)
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-preview062 | 25 | 2/11/2026 |
| 0.7.0-preview061 | 81 | 2/7/2026 |
| 0.7.0-preview060 | 82 | 1/28/2026 |
| 0.7.0-preview059 | 81 | 1/26/2026 |
| 0.7.0-preview058 | 83 | 1/25/2026 |
| 0.7.0-preview057 | 79 | 1/25/2026 |
| 0.7.0-preview056 | 90 | 1/10/2026 |
| 0.7.0-preview055 | 268 | 12/16/2025 |
| 0.7.0-preview054 | 175 | 12/13/2025 |
| 0.7.0-preview053 | 128 | 12/12/2025 |
| 0.7.0-preview052 | 438 | 12/9/2025 |
| 0.7.0-preview051 | 296 | 12/7/2025 |
| 0.7.0-preview050 | 222 | 12/7/2025 |
| 0.7.0-preview049 | 185 | 11/26/2025 |
| 0.7.0-preview048 | 199 | 11/24/2025 |
| 0.7.0-preview047 | 139 | 11/7/2025 |
| 0.7.0-preview046 | 203 | 11/6/2025 |
| 0.7.0-preview045 | 202 | 11/3/2025 |
| 0.6.0 | 209 | 11/10/2024 |
| 0.1.0-preview009 | 175 | 10/29/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-29 22:20:04 +0300
Hash: 6ed278f1c39475dd982619b8a35f89635b66d995