Testing with NUnit
NUnit is one of the most popular .NET test frameworks. It is part of the xUnit family of test frameworks. Boa Constrictor works seamlessly with NUnit. In fact, the tutorial uses NUnit as its core test framework! Boa Constrictor’s fluent-like syntax makes NUnit test classes easy to read and understand.
This user guide shows how to integrate Boa Constrictor with NUnit.
The example code matches the ScreenplayWebUiTest
class in the Boa.Constrictor.Example
project
(which is also part of the tutorial).
NUnit Test Projects
Boa Constrictor can integrate with any NUnit test project. Full instructions for project setup are given by Part 1 of the tutorial. In brief:
- Create a new NUnit test project in Visual Studio.
- Install the Boa.Constrictor NuGet packages into the project.
.NET Core projects are typically recommended over .NET Framework projects. The project will need the NUnit and NUnit3TestAdapter NuGet packages, but Visual Studio should automatically add them when creating an NUnit project. You may need to add other NuGet packages like FluentAssertions as well.
NUnit Test Classes
NUnit test classes should have all the appropriate using
statements for the namespaces they need.
They should also have an instance variable for the Screenplay Actor object.
Below is a class stub for the example ScreenplayWebUiTest
class:
using Boa.Constrictor.Screenplay;
using Boa.Constrictor.Selenium;
using FluentAssertions;
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
namespace Boa.Constrictor.Example
{
public class ScreenplayWebUiTest
{
private IActor Actor { get; set; }
// ...
}
}
Screenplay SetUp Methods
In NUnit test classes, methods with the [SetUp]
attribute run before each test case to “set up” the test.
Each test should have its own Actor with its own set of Abilities to preserve test case independence.
Below is the [SetUp]
method for the example ScreenplayWebUiTest
class.
It constructs an Actor and gives it the Ability to browse the web with ChromeDriver:
[SetUp]
public void InitializeScreenplay()
{
Actor = new Actor(name: "Andy", logger: new ConsoleLogger());
Actor.Can(BrowseTheWeb.With(new ChromeDriver()));
}
Screenplay Test Methods
In NUnit test classes, methods with the [Test]
or [TestCase]
attributes are individual test cases.
With Boa Constrictor, most test cases should be a series of Screenplay interactions, like
Actor.AttemptsTo(...)
, Actor.AsksFor(...)
, and Actor.WaitsUntil(...)
.
Below is a test case method for the example ScreenplayWebUiTest
class:
[Test]
public void TestWikipediaSearch()
{
Actor.AttemptsTo(Navigate.ToUrl(MainPage.Url));
Actor.AskingFor(ValueAttribute.Of(MainPage.SearchInput)).Should().BeEmpty();
Actor.AttemptsTo(SearchWikipedia.For("Giant panda"));
Actor.WaitsUntil(Text.Of(ArticlePage.Title), IsEqualTo.Value("Giant panda"));
}
Screenplay TearDown Methods
In NUnit test classes, methods with the [TearDown]
attribute run after each test case to “tear down” (or “clean up”) the test.
Cleanup procedures are not required for all types of tests, but they are required for WebDriver-based tests.
Below is the [TearDown]
method for the example ScreenplayWebUiTest
class.
It safely quits the browser for Web UI test cleanup:
[TearDown]
public void QuitBrowser()
{
Actor.AttemptsTo(QuitWebDriver.ForBrowser());
}
A Complete NUnit Test Class
The complete code for ScreenplayWebUiTest
is below:
using Boa.Constrictor.Screenplay;
using Boa.Constrictor.Selenium;
using FluentAssertions;
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
namespace Boa.Constrictor.Example
{
public class ScreenplayWebUiTest
{
private IActor Actor;
[SetUp]
public void InitializeScreenplay()
{
Actor = new Actor(name: "Andy", logger: new ConsoleLogger());
Actor.Can(BrowseTheWeb.With(new ChromeDriver()));
}
[TearDown]
public void QuitBrowser()
{
Actor.AttemptsTo(QuitWebDriver.ForBrowser());
}
[Test]
public void TestWikipediaSearch()
{
Actor.AttemptsTo(Navigate.ToUrl(MainPage.Url));
Actor.AskingFor(ValueAttribute.Of(MainPage.SearchInput)).Should().BeEmpty();
Actor.AttemptsTo(SearchWikipedia.For("Giant panda"));
Actor.WaitsUntil(Text.Of(ArticlePage.Title), IsEqualTo.Value("Giant panda"));
}
}
}
The example project contains this class and two other NUnit test classes under
Boa.Constrictor.Example/Tests
.