SparkyTestHelpers.Scenarios.MsTest 1.2.2

.NET Standard 1.3
dotnet add package SparkyTestHelpers.Scenarios.MsTest --version 1.2.2
NuGet\Install-Package SparkyTestHelpers.Scenarios.MsTest -Version 1.2.2
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="SparkyTestHelpers.Scenarios.MsTest" Version="1.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SparkyTestHelpers.Scenarios.MsTest --version 1.2.2
#r "nuget: SparkyTestHelpers.Scenarios.MsTest, 1.2.2"
#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.
// Install SparkyTestHelpers.Scenarios.MsTest as a Cake Addin
#addin nuget:?package=SparkyTestHelpers.Scenarios.MsTest&version=1.2.2

// Install SparkyTestHelpers.Scenarios.MsTest as a Cake Tool
#tool nuget:?package=SparkyTestHelpers.Scenarios.MsTest&version=1.2.2

This package contains an implementation of SparkyTestHelpers.Scenarios for "MsTest" / VisualStudio.TestTools.

The only differences are:

  • If you use MsTest's Assert.Inconclusive() in a scenario test, your scenario "suite" will be recognized as inconclusive and not as a failure by the Visual Studio test runner.
  • The "using" statement is using SparkyTestHelpers.Scenarios.MsTest.MsTest; instead of using SparkyTestHelpers.Scenarios.MsTest;

see also:


MsTestScenarioTester<TScenario>

VisualStudio.TestTools now has "DataMemberTest" and "DataRow" attributes, but they didn't when I started using the framework, so I wrote my own "scenerio testing" tool based my experience with NUnit "TestCase" attributes. This class provides the ability to execute the same test code for multiple test cases and, after all test cases have been executed, fail the unit test if any of the test cases failed.

Even if your test framework has attribute-based scenario testing, these helpers provide an alternative syntax that you might find useful.

This class is rarely used directly. It's easier to use with IEnumerable<TScenario>.TestEach or ForTest.Scenarios (see below).

When one or more of the scenarios fails, the failure exception shows which were unsuccessful, for example, this scenario test:

ForTest.Scenarios
(
    new { DateString = "1/31/2023", IsGoodDate = true },
    new { DateString = "2/31/2023", IsGoodDate = true },
    new { DateString = "6/31/2023", IsGoodDate = false }
)
.TestEach(scenario =>
{
    DateTime dt;
    Assert.AreEqual(scenario.IsGoodDate, DateTime.TryParse(scenario.DateString, out dt));
});

...throws this exception:

Test method SparkyTestHelpers.UnitTests.DateTests threw exception:
SparkyTestHelpers.Scenarios.ScenarioTestFailureException: Scenario[1] (2 of 3) - Assert.AreEqual failed. Expected:<True>. Actual:<False>.

Scenario data - anonymousType: {"DateString":"2/31/2023","IsGoodDate":true}

Scenario[2] (3 of 3) - Assert.AreEqual failed. Expected:<True>. Actual:<False>.

Scenario data - anonymousType: {"DateString":"4/31/2023","IsGoodDate":true}

Public Methods

  • ScenarioTester BeforeEachTest(Action<TScenario>)

    Defines action to called before each scenario is tested.

Example

ForTest.Scenarios(*array*)
.BeforeEachTest(scenario => 
{
    // do something, e.g. reset mocks, log scenario details
});
.TestEach(scenario =>
{
    DateTime dt;
    Assert.AreEqual(scenario.IsGoodDate, DateTime.TryParse(scenario.DateString, out dt));
});
  • ScenarioTester AfterEachTest(Action<TScenario>)

    Defines function called after each scenario is tested. The function receives the scenario and the exception (if any) caught by the test. If the function returns true, the scenario test is "passed". If false, exception is thrown to fail the test.

Example

ForTest.Scenarios(*array*)
.AfterEachTest((scenario, ex) => 
{
    // do something, e.g. log scenario details, 
    // decide if scenario with exception should be passed
    return ex == null;
});
.TestEach(scenario =>
{
    DateTime dt;
    Assert.AreEqual(scenario.IsGoodDate, DateTime.TryParse(scenario.DateString, out dt));
});

ScenarioTesterExtension

ScenarioTester<TScenario> extension methods.

Static Methods

  • ScenarioTester<TScenario> TestEach(IEnumerable<TScenario> enumerable, Action<TScenario> test)

Example

using SparkyTestHelpers.Scenarios.MsTest;
. . .
new []
{
    new { DateString = "1/31/2023", IsGoodDate = true },  
    new { DateString = "2/31/2023", IsGoodDate = false } 
}
.TestEach(scenario =>
{
    DateTime dt;
    Assert.AreEqual(scenario.IsGoodDate, DateTime.TryParse(scenario.DateString, out dt));  
});  

ForTest

"Syntactic sugar" methods for working with ScenarioTester<TScenario>

Static Methods

  • ForTest.Scenarios(TScenario[] scenarios) - creates array of scenarios that can be "dotted" to the TestEach extension method:

Example

using SparkyTestHelpers.Scenarios.MsTest;
. . .
ForTest.Scenarios
(
    new { DateString = "1/31/2023", IsGoodDate = true },  
    new { DateString = "2/31/2023", IsGoodDate = false }
)
.TestEach(scenario =>
{
    DateTime dt;
    Assert.AreEqual(scenario.IsGoodDate, DateTime.TryParse(scenario.DateString, out dt));  
});  
  • IEnumerable<TEnum> EnumValues() - tests each value in an enum.

Example

using SparkyTestHelpers.Scenarios.MsTest;
. . .
ForTest.EnumValues<OrderStatus>()
    .TestEach(orderStatus => foo.Bar(orderStatus));
  • IEnumerable<TEnum> ExceptFor(IEnumerable<TEnum> values, TEnum[] valuesToExclude) - Exclude enum values from test scenarios.

Example

using SparkyTestHelpers.Scenarios.MsTest;
. . .
ForTest.EnumValues<OrderStatus>()
    .ExceptFor(OrderStatus.Cancelled)
    .TestEach(orderStatus => foo.Bar(orderStatus));
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net46 net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Universal Windows Platform uap uap10.0
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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
1.2.2 653 7/27/2019
1.2.1 794 2/21/2018
1.2.0 761 2/14/2018
1.1.0 825 2/12/2018
1.0.0 779 2/9/2018