SparkyTestHelpers 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SparkyTestHelpers --version 1.0.0
NuGet\Install-Package SparkyTestHelpers -Version 1.0.0
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" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SparkyTestHelpers --version 1.0.0
#r "nuget: SparkyTestHelpers, 1.0.0"
#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 as a Cake Addin
#addin nuget:?package=SparkyTestHelpers&version=1.0.0

// Install SparkyTestHelpers as a Cake Tool
#tool nuget:?package=SparkyTestHelpers&version=1.0.0

This package contains:

  • SparkyTestHelpers.Exceptions: helpers for testing that an expected exception is thrown, with the expected message
  • SparkyTestHelpers.Scenarios: helpers for testing a method with a variety of different input scenarios

see also:


AssertExceptionNotThrown

class SparkyTestHelpers.Exceptions.AssertExceptionNotThrown

Assert that an exception is not thrown when an action is executed. This class/method doesn't do much, but it clarifies the intent of Unit Tests that wish to show that an action works correctly.

Static Methods

  • void WhenExecuting (Action action)
    Asserts that an exception was not thrown when executing an Action.

Example

using SparkyTestHelpers.Exceptions;
AssertExceptionNotThrown.WhenExecuting(() => foo.Bar(baz));

AssertExceptionThrown

class SparkyTestHelpers.Exceptions.AssertExceptionThrown

This class is used to assert than an expected exception is thrown when a test action is executed.

Why would you want to use this class instead of something like the VisualStudio TestTools ExpectedExceptionAttribute?

  • This class allows you to check the exception message.
  • This class allows you to assert than exception is thrown for a specific statement, not just anywhere in the test method.

There is no public constructor for this class. It is constructed using the "fluent" static factory method AssertExceptionThrown.OfType<TException>().

Example

using SparkyTestHelpers.Exceptions;
AssertExceptionThrown
    .OfType<ArgumentOutOfRangeException>()
    .WithMessage("Limit cannot be greater than 10.")
    .WhenExecuting(() => { var foo = new Foo(limit: 11); });

Methods

  • AssertExceptionThrown WithMessage (String expected)
    Set up to test that the action under test throws an exception where the message exactly matches the message.

  • AssertExceptionThrown WithMessageStartingWith (String expected)
    Set up to test that the action under test throws an exception where the message starts with the message.

  • AssertExceptionThrown WithMessageContaining (String expected)
    Set up to test that the action under testthrows an exception where the message contains themessage.

  • AssertExceptionThrown WithMessageMatching (String regExPattern)
    Set up to test that the action under test throws an exception where the message matches the specified regular expression pattern.

  • Exception WhenExecuting (Action action)
    Call the action that should throw an exception, and assert that the exception was thrown.

Static Methods

  • AssertExceptionThrown.OfType<TException>*()*

    • Set up to test that the action under test throws an exception of this specific type.
  • AssertExceptionThrown.OfTypeOrSubclassOfType<TException>*()*

    • Set up to test that the action under test throws an exception of this type of a subclass of this type.

ScenarioTester<TScenario>

class SparkyTestHelpers.Scenarios.ScenarioTester<TScenario>

VisualStudio.TestTools doesn't have "RowTest" or "TestCase" attributes like NUnit or other .NET testing frameworks. (It does have a way to do data-driven tests, but it's pretty cumbersome.) This class provides the ability to execute the same test code for multiple test cases and, after all test cases have been executed, failing the unit test if any of the test cases failed.

Even if you're not testing with MSTest / VisualStudio.TestTools, these helpers provide an alternative syntax for "row testing".

This class is rarely used directly. It is more often used via the IEnumerable<TScenario>.TestEach extension method (see below).

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

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

...throws this exception:

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

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

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

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

ScenarioTesterExtension

class SparkyTestHelpers.Scenarios.ScenarioTesterExtension

ScenarioTester<TScenario> extension methods.

Static Methods

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

Example

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

ForTest

class SparkyTestHelpers.Scenarios.ForTest

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

Static Methods

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

Example

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

Example

using SparkyTestHelpers.Scenarios;
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;
ForTest.EnumValues<OrderStatus>()
    .ExceptFor(OrderStatus.Cancelled)
    .TestEach(orderStatus => foo.Bar(orderStatus));
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on SparkyTestHelpers:

Package Downloads
SparkyTestHelpers.Mapping

Unit test helpers for asserting public properties on class instances "mapped" from one type to another (via AutoMapper, another tool, or hand-written code) have the correct values.

SparkyTestHelpers.Moq

Unit test helpers providing alternate syntax for testing with Moq

SparkyTestHelpers.Scenarios.MsTest

MSTest implementation of SparkyTestHelpers.Scenarios

SparkyTestHelpers.AppSettings

Helpers for testing code that uses ConfigurationManager.AppSettings

SparkyTestHelpers.Xml

Helpers to perform .config file XML transformations, and to test the resulting transformed XML (or actually any XML, whether it's .config format or not).

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.5.4 7,440 8/13/2019
1.5.3 85,079 3/18/2019
1.5.2 23,198 12/2/2018
1.5.1 6,067 6/2/2018
1.5.0 811 5/20/2018
1.4.0 4,593 5/10/2018
1.2.2 44,941 2/21/2018
1.2.1 937 2/20/2018
1.2.0 2,343 2/14/2018
1.1.0 2,169 2/12/2018
1.0.0 1,983 2/9/2018