RSCG – TUnit
| name | TUnit |
| nuget | https://www.nuget.org/packages/TUnit/ |
| link | https://github.com/thomhurst/TUnit |
| author | Tom Longhurst |
Writing unit tests
This is how you can use TUnit .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TUnit" Version="1.2.11" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
namespace TestDemo;
public class FirstTest
{
[Test]
public async Task Add_WithTwoNumbers_ReturnsSum()
{
var result = true;
// Assert
await Assert.That(result).IsTrue();
}
}
The code that is generated is
// <auto-generated/>
#pragma warning disable
[global::System.CodeDom.Compiler.GeneratedCode("TUnit", "1.0.0.0")]
file static class AssemblyLoader0fc44d9acf154c74aee2a9e062a8edcd
{
[global::System.Runtime.CompilerServices.ModuleInitializer]
public static void Initialize()
{
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("EnumerableAsyncProcessor, Version=3.8.4.0, Culture=neutral, PublicKeyToken=7a7adb9c614908c9"));
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("Microsoft.Extensions.DependencyModel, Version=6.0.0.2, Culture=neutral, PublicKeyToken=adb9793829ddae60"));
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("Mono.Cecil, Version=0.11.5.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e"));
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("Mono.Cecil.Mdb, Version=0.11.5.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e"));
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("Mono.Cecil.Pdb, Version=0.11.5.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e"));
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("Mono.Cecil.Rocks, Version=0.11.5.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e"));
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("TUnit.Assertions, Version=1.2.11.0, Culture=neutral, PublicKeyToken=b8d4030011dbd70c"));
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("TUnit.Core, Version=1.2.11.0, Culture=neutral, PublicKeyToken=b8d4030011dbd70c"));
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("TUnit, Version=1.2.11.0, Culture=neutral, PublicKeyToken=b8d4030011dbd70c"));
global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => global::System.Reflection.Assembly.Load("TUnit.Engine, Version=1.2.11.0, Culture=neutral, PublicKeyToken=b8d4030011dbd70c"));
}
}
// <auto-generated/>
#pragma warning disable
[global::System.CodeDom.Compiler.GeneratedCode("TUnit", "1.0.0.0")]
file static class DisableReflectionScanner_74bd22630f9848fb858269b3a1b085a4
{
[global::System.Runtime.CompilerServices.ModuleInitializer]
public static void Initialize()
{
global::TUnit.Core.SourceRegistrar.IsEnabled = true;
}
}
// <auto-generated/> #pragma warning disable #nullable enable // No conversion operators found
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace TUnit.Generated;
internal sealed class TestDemo_FirstTest_Add_WithTwoNumbers_ReturnsSum_TestSource : global::TUnit.Core.Interfaces.SourceGenerator.ITestSource
{
public async global::System.Collections.Generic.IAsyncEnumerable<global::TUnit.Core.TestMetadata> GetTestsAsync(string testSessionId, [global::System.Runtime.CompilerServices.EnumeratorCancellation] global::System.Threading.CancellationToken cancellationToken = default)
{
var metadata = new global::TUnit.Core.TestMetadata<global::TestDemo.FirstTest>
{
TestName = "Add_WithTwoNumbers_ReturnsSum",
TestClassType = typeof(global::TestDemo.FirstTest),
TestMethodName = "Add_WithTwoNumbers_ReturnsSum",
Dependencies = global::System.Array.Empty<global::TUnit.Core.TestDependency>(),
AttributeFactory = static () =>
[
new global::TUnit.Core.TestAttribute(),
new global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v10.0")
{FrameworkDisplayName = ".NET 10.0",},
new global::System.Reflection.AssemblyCompanyAttribute("TestDemo"),
new global::System.Reflection.AssemblyConfigurationAttribute("Debug"),
new global::System.Reflection.AssemblyFileVersionAttribute("1.0.0.0"),
new global::System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+54e5188c456ecd37fa9b7fa05bc1869fe84e7d42"),
new global::System.Reflection.AssemblyProductAttribute("TestDemo"),
new global::System.Reflection.AssemblyTitleAttribute("TestDemo"),
new global::System.Reflection.AssemblyVersionAttribute("1.0.0.0"),
new global::System.Reflection.AssemblyMetadataAttribute("Microsoft.Testing.Platform.Application", "true")
],
DataSources = global::System.Array.Empty<global::TUnit.Core.IDataSourceAttribute>(),
ClassDataSources = global::System.Array.Empty<global::TUnit.Core.IDataSourceAttribute>(),
PropertyDataSources = global::System.Array.Empty<global::TUnit.Core.PropertyDataSource>(),
PropertyInjections = global::System.Array.Empty<global::TUnit.Core.PropertyInjectionData>(),
InheritanceDepth = 0,
FilePath = @"D:\\gth\\RSCG_Examples\\v2\\rscg_examples\\TUnit\\src\\TestDemo\\TestDemo\\FirstTest.cs",
LineNumber = 6,
MethodMetadata = new global::TUnit.Core.MethodMetadata
{
Type = typeof(global::TestDemo.FirstTest),
TypeInfo = new global::TUnit.Core.ConcreteType(typeof(global::TestDemo.FirstTest)),
Name = "Add_WithTwoNumbers_ReturnsSum",
GenericTypeCount = 0,
ReturnType = typeof(global::System.Threading.Tasks.Task),
ReturnTypeInfo = new global::TUnit.Core.ConcreteType(typeof(global::System.Threading.Tasks.Task)),
Parameters = global::System.Array.Empty<global::TUnit.Core.ParameterMetadata>(),
Class = global::TUnit.Core.ClassMetadata.GetOrAdd("TestDemo:global::TestDemo.FirstTest", static () =>
{
var classMetadata = new global::TUnit.Core.ClassMetadata
{
Type = typeof(global::TestDemo.FirstTest),
TypeInfo = new global::TUnit.Core.ConcreteType(typeof(global::TestDemo.FirstTest)),
Name = "FirstTest",
Namespace = "TestDemo",
Assembly = global::TUnit.Core.AssemblyMetadata.GetOrAdd("TestDemo", static () => new global::TUnit.Core.AssemblyMetadata { Name = "TestDemo" }),
Parameters = global::System.Array.Empty<global::TUnit.Core.ParameterMetadata>(),
Properties = global::System.Array.Empty<global::TUnit.Core.PropertyMetadata>(),
Parent = null
};
foreach (var prop in classMetadata.Properties)
{
prop.ClassMetadata = classMetadata;
prop.ContainingTypeMetadata = classMetadata;
}
return classMetadata;
})
},
InstanceFactory = (typeArgs, args) => new global::TestDemo.FirstTest(),
InvokeTypedTest = static (instance, args, cancellationToken) =>
{
try
{
return new global::System.Threading.Tasks.ValueTask(instance.Add_WithTwoNumbers_ReturnsSum());
}
catch (global::System.Exception ex)
{
return new global::System.Threading.Tasks.ValueTask(global::System.Threading.Tasks.Task.FromException(ex));
}
},
};
metadata.UseRuntimeDataGeneration(testSessionId);
yield return metadata;
yield break;
}
}
internal static class TestDemo_FirstTest_Add_WithTwoNumbers_ReturnsSum_ModuleInitializer
{
[global::System.Runtime.CompilerServices.ModuleInitializer]
public static void Initialize()
{
global::TUnit.Core.SourceRegistrar.Register(typeof(global::TestDemo.FirstTest), new TestDemo_FirstTest_Add_WithTwoNumbers_ReturnsSum_TestSource());
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/TUnit