SparkyTestHelpers 1.5.3

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

Install-Package SparkyTestHelpers -Version 1.5.3
dotnet add package SparkyTestHelpers --version 1.5.3
<PackageReference Include="SparkyTestHelpers" Version="1.5.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SparkyTestHelpers --version 1.5.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
  • SparkyTestHelpers.Exceptions: for testing exception expectations
  • SparkyTestHelpers.Scenarios: for testing a method with a variety of different input scenarios

see also:


AssertExceptionNotThrown

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

Static Methods

  • void WhenExecuting(Action action)

Example

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

AssertExceptionThrown

Used to assert than an expected exception is thrown when a test action is executed.

Why use this instead of something like the VisualStudio TestTools ExpectedExceptionAttribute?

  • It lets you to check the exception message.
  • It lets you assert the exception is thrown for a specific statement, not just anywhere in the code under test.

There is no public constructor for this class. It's 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 matche.
  • AssertExceptionThrown WithMessageStartingWith (String expected)
    Set up to test that the action under test throws an exception where the message starts with expected string.
  • AssertExceptionThrown WithMessageContaining (String expected)
    Set up to test that the action under test throws an exception where the message contains substring.
  • AssertExceptionThrown WithMessageMatching (String regExPattern)
    Set up to test that the action under test throws an exception where the message matches RegEx pattern.
  • Exception WhenExecuting (Action action)
    Call 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>

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;
. . .
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;
. . .
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;
. . .
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));
  • SparkyTestHelpers.Exceptions: for testing exception expectations
  • SparkyTestHelpers.Scenarios: for testing a method with a variety of different input scenarios

see also:


AssertExceptionNotThrown

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

Static Methods

  • void WhenExecuting(Action action)

Example

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

AssertExceptionThrown

Used to assert than an expected exception is thrown when a test action is executed.

Why use this instead of something like the VisualStudio TestTools ExpectedExceptionAttribute?

  • It lets you to check the exception message.
  • It lets you assert the exception is thrown for a specific statement, not just anywhere in the code under test.

There is no public constructor for this class. It's 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 matche.
  • AssertExceptionThrown WithMessageStartingWith (String expected)
    Set up to test that the action under test throws an exception where the message starts with expected string.
  • AssertExceptionThrown WithMessageContaining (String expected)
    Set up to test that the action under test throws an exception where the message contains substring.
  • AssertExceptionThrown WithMessageMatching (String regExPattern)
    Set up to test that the action under test throws an exception where the message matches RegEx pattern.
  • Exception WhenExecuting (Action action)
    Call 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>

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

Version History

Version Downloads Last updated
1.5.3 164 3/18/2019
1.5.2 3,637 12/2/2018
1.5.1 2,063 6/2/2018
1.5.0 144 5/20/2018
1.4.0 183 5/10/2018
1.2.2 383 2/21/2018
1.2.1 200 2/20/2018
1.2.0 175 2/14/2018
1.1.0 199 2/12/2018
1.0.0 216 2/9/2018