OpenSourced: JustBehave

Open Source and JUST EAT

Like most of the technology world, we use a lot of open source software at JUST EAT.  Open source is important to us and reflects how we like to work. It’s collaborative, it’s open, it’s pragmatic – all qualities that are essential if you want to build great software and services.

As we grow and expand (we’re hiring, get in touch!) we’ve made an internal commitment to engaging more with the open source products communities that have helped us become successful.

We’re committing to contributing back our modifications to open source projects, we’re going to be sending more pull requests to try and make the world a little bit better. But we want to do more – we’re going to start releasing some of our own proprietary libraries (initially C# and Ruby code) as open source under the permissive Apache license, and start developing them in the open. We’ll happily take pull requests, and help you use our code – we believe that as a software community, we’re better working together.

We try and use the best of both open and closed source software to give deliver the best experience we possibly can to our users and partners, and we’re thrilled to be able to share some of our work with you.

Announcing JustBehave – our BDD extensions for .NET testing

On the back of our commitment to open source, we’d like to announce the publication of our first open source project – JustBehave.  We use C# to build a lot of our internal services at JUST EAT and meaningful test suites are vital to shipping reliable code.

Some of our teams are proponents of a BDD approach to testing – organising our test suites around the concepts of contexts and scenarios. This helps us understand the functionality of our application, and lets us organise our test suites into meaningful, feature-focused namespaces, and helps us extend our tests with minimal friction.

What we didn’t necessarily agree with when looking at the frameworks available, was the focus on the Gherkin language popularised by Cucumber, which was supported in the leading BDD framework for .NET SpecFlow.

Gherkin’s focus on acting as a communication format between “the business” and programmers – didn’t fit our needs (in a unit testing context). We weren’t looking for business people to write our behaviour tests and preferred to have tests written in an internal rather than an external DSL. We use Gherkin for our acceptance test suite, but we wanted something more language native for our C# tests.

We found a sweet spot in the middle – a way of organising tests into contexts and scenarios without straying too far from the path of nUnit and xUnit in the way that frameworks like xBehave do, and developed a convention of test classes with well defined “Given”, “When” and “Then” steps. This evolved into JustBehave, a small C# package that enforces these conventions.

A typical JustBehave test will look like this:

public class TestClassNamedAfterABddContext
: BehaviourTest<TheClassThatIWantToTest>
{
protected override void Given(){ /*…*/ }
protected override void When(){ /*…*/ }
[Then] public void Assertion1(){ /*…*/ }
[Then] public void Assertion2(){ /*…*/ }
}

and will happily execute using nUnit, xUnit, TestDriven.NET, Resharper and NCrunch.

We evolved a pattern of using inheritance and giving our test classes meaningful names to build scenarios out of test contexts. Using file structures like:

/Tests/Payments/WhenIMakeAPayment.cs
/Tests/Payments/AndMyPaymentIsRejected.cs (inherits from WhenIMakeAPayment.cs)
/Tests/Payments/AndMyPaymentIsAccepted.cs (inherits from WhenIMakeAPayment.cs)

to represent the behaviour of our applications. These simple patterns, along with enforcing the convention of “Given, When, Then” in our test classes, helped us build a meaningful and useful set of behavior driven tests that render well in test runners.

 

JustBehave has evolved a little over the last couple of years – it now supports AutoMocking, an opinionated way to test for exceptions and works with both nUnit and xUnit and it helps us get our job done.

We’re releasing it on GitHub and NuGet under the Apache license – check it out.

https://github.com/justeat/JustBehave

http://www.nuget.org/packages/JustBehave/

Keep an eye out for the imminent release of our extension packages giving you immediate NSubstitue, Rhino and MOQ driven tests too…