SparkyTestHelpers 1.0.0

.NET Unit test helpers for asserting that exceptions were / weren't thrown, and for "scenario tests".

There is a newer version of this package available.
See the version list below for details.
Install-Package SparkyTestHelpers -Version 1.0.0
dotnet add package SparkyTestHelpers --version 1.0.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SparkyTestHelpers, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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));

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));

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.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).
SparkyTestHelpers.AppSettings
Helpers for testing code that uses ConfigurationManager.AppSettings

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.5.4 2,188 8/13/2019
1.5.3 22,629 3/18/2019
1.5.2 18,024 12/2/2018
1.5.1 2,902 6/2/2018
1.5.0 477 5/20/2018
1.4.0 837 5/10/2018
1.2.2 14,621 2/21/2018
1.2.1 545 2/20/2018
1.2.0 621 2/14/2018
1.1.0 724 2/12/2018
1.0.0 679 2/9/2018
Show less