-
Notifications
You must be signed in to change notification settings - Fork 0
NUnitTutorial
This tutorial will take you through writing your first Screenplay test with NUnit.
In this tutorial we are going to write some tests for a sample application which does the laundry. The tests will all be testing this simple application.
The first step is to create a new .NET project for your Screenplay tests.
Screenplay tests can coexist with non-Screenplay tests in the same test assembly but it's best to keep them separate.
Next, install the NUnit integration NuGet package into that test project.
Now, create a new class file in that test project, with code which looks something like the following example.
using CSF.Screenplay.NUnit;
using CSF.Screenplay.Integration;
[assembly: ScreenplayAssembly(typeof(ScreenplayIntegrationConfig))]
public class ScreenplayIntegrationConfig : IIntegrationConfig
{
public void Configure(IIntegrationConfigBuilder builder)
{
}
}This class is your Screenplay integration configuration and it is mandatory for any assembly which contains Screenplay tests. This tutorial will show a little of what may be accomplished with this class.
Tests which use Screenplay are normal NUnit tests, except that they must be decorated with the [Screenplay] attribute. This does not mean that they cannot also be decorated with other NUnit attributes, such as [Parallelizible].
Since we are testing a laundry application, we are going to write a test which asserts that - after using the a service to load laundry into the washing machine, that our laundry basket has been emptied.
Right now, we haven't written any tasks to go into the test, so we're just going to write a skeleton test which states our intention. We'll come back and fill in the implementation later.
using NUnit.Framework;
using CSF.Screenplay;
[TestFixture]
public class LoadLaundryTests
{
[Test, Screenplay]
public void After_Laurence_loads_laundry_into_the_machine_his_laundry_basket_should_be_empty(ICast cast)
{
var laurence = cast.Get("Laurence");
// TODO: Write some test logic
}
}The one piece of logic which we have filled in is a line which retrieves our actor, named Laurence. You'll see that this came from an ICast instance which we received as a parameter to our test.
When using NUnit you will generally want to add a cast parameter to every test method. The implementation is provided by Screenplay automatically.
Also, notice that we named an actor "Laurence" in the test. It's good practice to use actor names which are mnemonics for their roles. In this case "Laurence" has "Laundry" to do.
Let's flesh out our test with some tasks that describe what it does from a high-level perspective. For our example test (based upon its name) we have one obvious task and two which are implied:
- Begin with a basket of laundry - this is the assumption which starts the test
- Load laundry into the machine - this is the task which we are testing
- Look at what's in the laundry basket - needed in order to assert that the basket is empty
You will notice that these tasks form a natural Given, When, Then organisation (or Arrange, Act, Assert if you prefer). It's very normal for each part of a test to have a task of its own, especially when you are writing your first few tests for a feature.
Let's write these now:
using CSF.Screenplay;
public class GetABasketWithSomeDirtyLaundry : Performable
{
}
public class LoadDirtyLaundryIntoTheWashingMachine : Performable
{
}
public class GetLaundryFromTheBasket : Performable<IEnumerable<LaundryItem>>
{
}Before we go writing tasks, those tasks are going to need to be able to use the laundry application. To do that we're going to write a small Ability class and add it to our configuration.