Skip to content

James.Testing

Todd Meinershagen edited this page Oct 17, 2017 · 13 revisions

James.Testing

James.Testing is general purpose testing library that provides action extensions to aid in testing. Source can be found here.

You can add the assembly to your projects via NuGet Package Manager using the following.

>install-package James.Testing

Action Extensions

Executing an Action with Retries

Many times in integration tests, there is a non-deterministic time period between executing some initial action and asserting your expectations for the outcome. In this case, it would be nice to have a method for automatically having the test retry your action for a number of times even though the assertion fails. This method also supports setting a wait time in between retries so that you don't overload your system.

Example:

var counter = 0;
Action action = () => counter++;
action.ExecuteWithRetries(times, waitTimeInSeconds);

Executing an Action with a Timeout Period

In other cases, you might want to execute a given action for a given time period. For instance, if you have a requirement to expect a response within 30 seconds, you can set the maximum timeout period to 30 seconds for your assertions. This method also allows a user to set a given wait time between executions of a given action.

Example:

var counter = 0;
Action action = () => counter++;
action.ExecuteWithTimeout(timeoutInSeconds, waitTimeInSeconds);

Executing an Action while Gulping Exceptions

Sometimes when you execute an action you expect an exception to occur, but you don't want the exception to cause a failure because there is something else you need to verify. This method will do just the trick.

Example:

var counter = 0;
Action failingAction = () =>
	{
		while (counter++ < 3)
		{}
	};

failingAction.GulpException();
counter.Should().Be(3);

Executing an Action with Cleanup

Sometimes when you are executing a set of actions, you need to make sure that even if your test fails, there is cleanup. This is likely going to be handled in some kind of tear down or disposal function, but in the case where you need to handle everything within one test method, this extension method comes to the rescue.

Example:

Session session = null;

Action action = () => 
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
    session = new Session(new LocalPhantomEnvironment());

    session.CurrentCulture.Name.Should().Be("fr-FR");
};

action.ExecuteWithCleanup(() => session.End());

Wait Methods

Waiting For a Time Period

Many times in multi-threaded and integration tests, you need to wait for a number of seconds or milliseconds for other events to process. James.Testing now provides a more readable syntax for these events.

Example:

Wait.For(1).Seconds();
Wait.For(250).Milliseconds();

Waiting Until Something is True

Sometimes in multi-threaded and integration tests, you need to wait until something is true or that some state has been updated before moving forward. The easiest way to deal with this is to let James.Testing wait until some predicate expression has come true.

Example:

Wait.Until(() => Test.Current.EventLogs.Count == 2);

There are cases in which a predicate may never end up being true. By default, the Wait.Until() method has a timeout of 15 seconds. You can also configure that when calling the API by passing in a timeout period.

Timeout Expressed as TimeSpan

Wait.Until(() => Test.Current.EventLogs.Count == 2, TimeSpan.FromSeconds(5));

Timeout Expressed as Integer - Seconds

Wait.Until(() => Test.Current.EventLogs.Count == 2, timeoutInSeconds);