RSCG – TinyBDD.MSTest
| name | TinyBDD.MSTest |
| nuget | https://www.nuget.org/packages/TinyBDD.MSTest/ |
| link | https://github.com/JerrettDavis/TinyBDD |
| author | Jerrett Davis |
Optimizing Test Code with BDD Style Testing
This is how you can use TinyBDD.MSTest .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.8.1" />
<PackageReference Include="MSTest.TestAdapter" Version="4.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="4.3.2" />
<PackageReference Include="coverlet.collector" Version="10.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="TinyBDD.MSTest" Version="0.19.29" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Mock\MockData.csproj" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
using TinyBDD.MSTest;
namespace TestClock;
public class MyClock : IMyClock
{
public DateTime GetNow()
{
return DateTime.Now;
}
public DateTime GetUtcNow()
{
return DateTime.UtcNow;
}
}
[TestClass]
public partial class TestData: TinyBddMsTestBase
{
[TestMethod]
public async Task TestMyClock()
{
await Given("start", () => new MyClock())
.When("get now", x => x.GetNow())
.Then("equals 84", x => DateTime.Now>=x)
.AssertPassed();
}
}
The code that is generated is
// <auto-generated/>
#nullable enable
namespace TestClock
{
partial class TestData
{
async System.Threading.Tasks.Task TestMyClock_Optimized()
{
// Optimized implementation - no boxing!
var __ctx = TinyBDD.Ambient.Current.Value;
var __sw = System.Diagnostics.Stopwatch.StartNew();
try
{
// Given: start
__sw.Restart();
TestClock.MyClock __step0 = new MyClock();
__sw.Stop();
__ctx.AddStep(new TinyBDD.StepResult
{
Kind = "Given",
Title = "start",
Elapsed = __sw.Elapsed
});
// When: get now
__sw.Restart();
System.DateTime __step1 = __step0.GetNow();
__sw.Stop();
__ctx.AddStep(new TinyBDD.StepResult
{
Kind = "When",
Title = "get now",
Elapsed = __sw.Elapsed
});
// Then: equals 84
__sw.Restart();
bool __step2 = DateTime.Now>=__step1;
__sw.Stop();
__ctx.AddStep(new TinyBDD.StepResult
{
Kind = "Then",
Title = "equals 84",
Elapsed = __sw.Elapsed
});
if (!__step2)
{
throw new TinyBDD.BddStepException("Assertion failed: equals 84", __ctx, null);
}
}
catch (System.Exception __ex)
{
// TODO: Handle errors according to ScenarioOptions
throw;
}
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/TinyBDD.MSTest