xUnit.BDD 4.0.0-beta.1

Xunit BDD Extensions

Extends xUnit.Net with Behavior Driven Development style fixtures.

This is a prerelease version of xUnit.BDD.
There is a newer version of this package available.
See the version list below for details.
This package has a SemVer 2.0.0 package version.
This package will only be available to download with SemVer 2.0.0 compatible NuGet clients, such as Visual Studio 2017 (version 15.3) and above or NuGet client 4.3.0 and above. Read more
Install-Package xUnit.BDD -Version 4.0.0-beta.1
dotnet add package xUnit.BDD --version 4.0.0-beta.1
<PackageReference Include="xUnit.BDD" Version="4.0.0-beta.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add xUnit.BDD --version 4.0.0-beta.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

xUnit.Net BDD Extensions

Extends xUnit.Net with Behavior Driven Development style fixtures.

Some of the design goals include:

  • Use natural C# constructs to control things such as BDD contexts and concerns. For example, the context of the specification is defined in the class constructor and the concern for the fixture is defined by its namespace.
  • Async tests are a first class citizen.

See here for a full introduction

How to Use

Install via nuget

dotnet add package xUnit.BDD

Write a Scenario

using Xunit;

public class Calculator
{
	public int Add(int x, int y) => x + y;
}

public class when_adding_two_numbers : Specification
{
	readonly Calculator calc;
	int result;

	public when_adding_two_numbers()
	{
		calc = new Calculator();
	}

	protected override Task ObserveAsync()
	{
		result = calc.Add(1, 2);
		return Task.CompletedTask;
	}

	[Observation]
	public void should_return_correct_result()
	{
		result.ShouldEqual(3);
	}
}

This is a contrived example, but the idea is to run the code you're observing in the ObserveAsync method and have one or more Observations on what you observed. This way, when a test (Observation) fails, the language of the class and method (the scenario) should be granular enough to let you know exactly what failed.

Async Scenarios

The Specification is async by default (e.g. the ObserveAsync method is expected to run async) since this is a brave new async world we live in. Since C# does not allow async constructors or async Dispose methods, if you need to do any async setup or teardown for the test, you should override InitializeAsync and/or DisposeAsync.

public class when_doing_some_async_thing : Specification
{
	string result;

	protected override async Task InitializeAsync()
	{
		await myThing.DoAThingAsync();
	}

	protected override async Task ObserveAsync()
	{
		result = await myThing.DoTheThing();
	}

	[Observation]
	public async Task should_return_correct_result()
	{
		string otherThing = await someOtherThing.DoAnotherThing();
		result.ShouldEqual(otherThing);
	}
}

You should explicitly avoid implementing Xunit's IAsyncLifetime since that is what Specification does internally to do its magic. Use the hooks on the Specification class.

Building Locally

After cloning, run:

dotnet restore
dotnet build

To run the test cases:

cd test
dotnet test

xUnit.Net BDD Extensions

Extends xUnit.Net with Behavior Driven Development style fixtures.

Some of the design goals include:

  • Use natural C# constructs to control things such as BDD contexts and concerns. For example, the context of the specification is defined in the class constructor and the concern for the fixture is defined by its namespace.
  • Async tests are a first class citizen.

See here for a full introduction

How to Use

Install via nuget

dotnet add package xUnit.BDD

Write a Scenario

using Xunit;

public class Calculator
{
	public int Add(int x, int y) => x + y;
}

public class when_adding_two_numbers : Specification
{
	readonly Calculator calc;
	int result;

	public when_adding_two_numbers()
	{
		calc = new Calculator();
	}

	protected override Task ObserveAsync()
	{
		result = calc.Add(1, 2);
		return Task.CompletedTask;
	}

	[Observation]
	public void should_return_correct_result()
	{
		result.ShouldEqual(3);
	}
}

This is a contrived example, but the idea is to run the code you're observing in the ObserveAsync method and have one or more Observations on what you observed. This way, when a test (Observation) fails, the language of the class and method (the scenario) should be granular enough to let you know exactly what failed.

Async Scenarios

The Specification is async by default (e.g. the ObserveAsync method is expected to run async) since this is a brave new async world we live in. Since C# does not allow async constructors or async Dispose methods, if you need to do any async setup or teardown for the test, you should override InitializeAsync and/or DisposeAsync.

public class when_doing_some_async_thing : Specification
{
	string result;

	protected override async Task InitializeAsync()
	{
		await myThing.DoAThingAsync();
	}

	protected override async Task ObserveAsync()
	{
		result = await myThing.DoTheThing();
	}

	[Observation]
	public async Task should_return_correct_result()
	{
		string otherThing = await someOtherThing.DoAnotherThing();
		result.ShouldEqual(otherThing);
	}
}

You should explicitly avoid implementing Xunit's IAsyncLifetime since that is what Specification does internally to do its magic. Use the hooks on the Specification class.

Building Locally

After cloning, run:

dotnet restore
dotnet build

To run the test cases:

cd test
dotnet test

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
6.0.0 1,023 10/10/2018
5.1.0 134 10/10/2018
5.0.1 2,235 4/26/2018
5.0.0 237 4/26/2018
5.0.0-slow-and-steady.1 155 4/26/2018
4.0.2 522 10/9/2017
4.0.2-reproducible-order.1 150 10/5/2017
4.0.1 223 10/2/2017
4.0.1-initialize-once.1 156 10/2/2017
4.0.0 214 10/2/2017
4.0.0-beta.3 152 9/28/2017
4.0.0-beta.2 146 9/27/2017
4.0.0-beta.1 176 9/27/2017
3.0.0 254 9/21/2017
2.2.0-beta2 302 4/22/2016
2.2.0-beta 275 2/18/2016
2.0.0 301 8/22/2017
1.9.2 1,999 2/20/2014
1.9.1.1 1,152 9/18/2012
Show less