• This repository has been archived on 07/Jul/2022
  • Stars
    star
    29
  • Rank 860,307 (Top 17 %)
  • Language
    C#
  • License
    Other
  • Created over 10 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A BDD-influenced C# testing library cooked up by Just Eat

JustBehave

Join the chat at https://gitter.im/justeat/JustBehave A BDD-influenced C# testing library cooked up by JUST EAT


Appveyor: Appveyor

  • Introduction
  • Installation
  • Getting Started
  • Documentation
  • Contributing
  • Copyright

JustBehave is a small .NET library that helps you structure your C# tests into BDD-style contexts and scenarios. It supports both NUnit and XUnit and depends on AutoFixture for automatic dependency construction using your mocking framework of choice.

JustBehave was written to ensure that our internal tests followed a consistent and sane BDD pattern, without relying on frameworks like SpecFlow and the Gherkin language. Tests written using JustBehave are structured and organised, without requiring additional tooling or plugins.

Installation

Pre-requisites: The project is built in .net v4.0.

Via NuGet:

	PM> Install-Package JustBehave

Getting Started

Once you have the package installed into your test project, along with your test framework of choice, a standard JustBehave test file will look like this using XUnit:

	//public class TestClassNamedAfterABddContext : XBehaviourTest<TheClassThatIWantToTest> // XUnit
	public class TestClassNamedAfterABddContext : BehaviourTest<TheClassThatIWantToTest> // NUnit
	{
		private string _input;
		private string _result;

		protected override void Given()
		{
			_input = "my input";
		}

		protected override void When()
		{
			_result = SystemUnderTest.CallingAMethod(_input);
		}

		[Then]
		public void ReturnsSomething()
		{
			_result.ShouldBe("something");
		}
	}

A full set of tested examples are provided in the repository in the project JustBehave.Tests in the Examples namespace.

Documentation

  • Important classes and attributes
  • Structuring Contexts and Scenarios
  • Handling and Testing Exceptions
  • Testing classes with dependencies

Important classes and attributes

The core of JustBehave is built upon a few small classes:

  • BehaviourTest - inherited for an NUnit test
  • XBehaviourTest - inherited for an XUnit test
  • ThenAttribute - used to annotate your Then conditions for the nunit testing framework
    • when using xunit, use FactAttribute as normal

This leads to test classes that follow the convention:

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

The NUnit TestFixtureAttribute is optional and implemented on the base of our NUnit BehaviourTest classes.

Structuring Contexts and Scenarios

The core of JustBehave is a base class for your tests that enforces a "Given, When, Then" pattern. Each test file represents a single context with a Given, a When, and multiple Then assertions.

Broader tests are constructed by inheriting from previous test classes, and overriding the Given or When steps, while adding additional Then conditions to construct scenarios. Expect to use namespaces to group related scenarios, while giving your test classes names that represent their context.

For example, when testing some payment infrastructure, expect to end up with a set of files like this:

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

By following this style, you'll end up with a ReSharper test runner that looks like this:

test runner following bdd conventions

Handling and Testing Exceptions

If you want to assert on thrown exceptions, you setup your Given() step like this:

	protected override void Given()
	{
	    RecordAnyExceptionsThrown();
	}

Any exceptions thrown will then be available in your Then steps for you to assert on:

	[Then]
	public void ShouldSeeException()
	{
		ThrownException.ShouldBeTypeOf<NotSupportedException>();
	}

Testing classes with dependencies

In order for JustBehave to construct your dependency graphs, you must either:

  • Manually construct your system under test
  • Configure AutoFixture and a mocking framework to auto-mock your dependencies

To override construction of the system under test, you need to override CreateSystemUnderTest() and return a valid instance:

	protected override SomethingUnderTest CreateSystemUnderTest()
	{
		return new SomethingUnderTest();
	}

Alternatively, to configure AutoFixture, you should override CustomizeAutoFixture() providing a valid AutoFixture customisation:

	protected override void CustomizeAutoFixture(Fixture fixture)
	{
		fixture.Customize(new AutoRhinoMockCustomization());
		// You can add more customisations by adding the NuGet packages for
		// Ploeh.AutoFixture.Auto**PopularMockingFramework** and hooking them up
	}

Contributing

If you find a bug, have a feature request or even want to contribute an enhancement or fix, please follow the contributing guidelines included in the repository.

Copyright

Copyright 2011-2014 Just Eat, Inc. or its affiliates. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located in the LICENSE file in this repository.

This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

ScrollingStackViewController

A view controller that uses root views of child view controllers as views in a UIStackView.
Swift
646
star
2

JustLog

JustLog brings logging on iOS to the next level. It supports console, file and remote Logstash logging via TCP socket with no effort. Support for logz.io available.
Swift
518
star
3

httpclient-interception

A .NET library for intercepting server-side HTTP requests
C#
246
star
4

JustTweak

JustTweak is a feature flagging framework for iOS apps.
Swift
203
star
5

JustPersist

JustPersist is the easiest and safest way to do persistence on iOS with Core Data support out of the box. It also allows you to migrate to any other persistence framework with minimal effort.
Swift
165
star
6

NavigationEngineDemo

A scalable and robust solution to navigation, deep linking and universal links on iOS 🚀
Swift
125
star
7

Shock

A HTTP mocking framework written in Swift.
Swift
101
star
8

JustPeek

JustPeek is an iOS Library that adds support for Force Touch-like Peek and Pop interactions on devices that do not natively support this kind of interaction.
Shell
68
star
9

Topshelf.Nancy

Nancy endpoint for the Topshelf service host providing additional support around URL reservations. — Edit
C#
65
star
10

ZendeskApiClient

C# Client for working with the Zendesk API
C#
64
star
11

JustEat.RecruitmentTest

The recruitment test to apply for an engineering role at Just Eat
Scala
62
star
12

AutomationTools

iOS UI testing framework and guidelines
Swift
52
star
13

NLog.StructuredLogging.Json

Structured logging for NLog using Json (formerly known as JsonFields)
C#
51
star
14

JustTrack

The Just Eat solution to better manage the analytics tracking and improve the relationship with your BI team.
Swift
42
star
15

JustFakeIt

HTTP server faking library
C#
31
star
16

jubako

A small API to help display rich content in a RecyclerView such as a wall of carousels
Kotlin
30
star
17

JustSaying

A light-weight message bus on top of AWS services (SNS and SQS).
C#
30
star
18

PRAssigner

Swift AWS Lambda to automatically assign engineers to pull requests with a Slack integration
Swift
29
star
19

ApplePayJSSample

A sample implementation of Apple Pay JS using ASP.NET Core
C#
23
star
20

kongverge

A desired state configuration tool for Kong
C#
23
star
21

ProcessManager

C#
21
star
22

Android.Samples.Deeplinks

Sample app demonstrating an effective and unit-testable way to handle deep links in Android
Java
20
star
23

JustEat.StatsD

Our library for publishing metrics to statsd
C#
19
star
24

iOS.ErrorUtilities

Just Eat collection of iOS error-related utilities
Swift
19
star
25

JustSupport

JavaScript
18
star
26

AwsWatchman

Because unmonitored infrastructure will bite you
C#
17
star
27

fozzie-components

JavaScript
13
star
28

kubernetes-windows-aws-ovs

Kubernetes on windows in aws using ovn
HCL
13
star
29

applepayjs-polyfill

A polyfill for the Apple Pay JS for use in non-supported browsers
JavaScript
12
star
30

fozzie

Web UI Base Library
SCSS
12
star
31

LocalDynamoDb

C#
11
star
32

JE.IdentityServer.Security

Recaptcha for OpenIdConnect
C#
7
star
33

JustEat.Recruitment.UI

UI Test for interview candidates
7
star
34

OpenRastaSwagger

Swagger / Swagger-UI implementation for OpenRasta
JavaScript
7
star
35

ts-jsonschema-builder

Fluent TypeScript JSON Schema builder.
TypeScript
6
star
36

JustEat.InfoSecRecruitmentTest

C#
4
star
37

gulp-build-fozzie

Gulp build tasks for use across Fozzie modules
JavaScript
4
star
38

JustEat.CWA.RecruitmentTest

Consumer Web Applications recruitment test for roles at JUST EAT
4
star
39

AngularJSWebAPI.Experiment

HTML
3
star
40

JE.EmbeddedChecks

Micro-framework for embedding checks inside applications, so they tell you when they're healthy and how.
C#
3
star
41

JE.TurningPages

C#
3
star
42

JE.ApiValidation

API validation helpers - A standard error response contract and FluentValidation support in .NET web frameworks
C#
3
star
43

JE.ApiExceptions

C#
2
star
44

f-footer

Common footer component for Just Eat websites
SCSS
2
star
45

JustEat.EntryLevel.RecruitmentTest

Recruitment Test for Entry Level Engineers
2
star
46

global-component-library

Global Component Library and Documentation for sharing across UK and International
CSS
2
star
47

f-templates

Locate, compile, and serve HTML from templates.
JavaScript
1
star
48

f-dom

Fozzie JS DOM queries library.
JavaScript
1
star
49

f-toggle

Fozzie vanilla JS toggle library.
JavaScript
1
star
50

PowerShellDSCUtils

Just Eat PowerShell DSC Utilities
PowerShell
1
star
51

f-header

Common header component for Just Eat websites
JavaScript
1
star
52

f-recruit-message

Adds a recruitment message to the browser console
JavaScript
1
star
53

ruby_bootcamp

Code samples for ruby bootcamp show & tells
Ruby
1
star
54

f-serviceworker

JavaScript
1
star
55

mickeydb

Android sqlite db super tool, for migrations and data access code generation
Java
1
star
56

f-copy-assets

NPM package to copy assets from node_modules to a specified directory
JavaScript
1
star
57

JustEat.PublicApi.TestApp

JavaScript
1
star
58

browserslist-config-fozzie

Just Eat's Browserslist Config used in UI packages
JavaScript
1
star
59

openrasta-hosting-owin

OWIN host for OpenRasta
C#
1
star