Skip to content
Derek Greer edited this page May 17, 2017 · 4 revisions

NUnit.Specifications

NUnit.Specifications is a context/specification library for use with the NUnit testing framework.

Background and Motivation

Unit Testing

NUnit was one of the first Unit-testing frameworks available for the .Net platform and continues to enjoy wide adoption today. NUnit itself was initially a direct port of the JUnit framework which was created for the Java development platform. Early frameworks such as JUnit were conceived to author unit tests to verify the behavior of systems. As agile testing practices evolved, developers began using unit testing frameworks not just to verify the behavior of their applications, but to drive the design of the application by writing the tests first. After some refinement, this practice came to be referred to as Test-Driven Development.

Test-Driven Development

Test-Driven Development prescribes a process whereby a developer first writes a failing test which describes how a component or system should behave, then writes just enough code to make the test pass, and finally refactors the code to eliminate duplication. Unfortunately, both the nomenclature and the frameworks used by the Test-Driven Development community were rooted in unit testing paradigms which served to hinder modeling system behavior and posed an obstacle to conveying the underlying principles of Test-Driven Development to other developers. These impediments eventually lead to further refinement of Test-Driven Development principles which came to be known as Behavior-Driven Development.

Behavior-Driven Development

Behavior-Driven Development (BDD) shifts the language used to talk about the process of driving software design through tests to focus upon the behavior of the system. This language shift along with the frameworks created to support BDD help the development team to ask, not what should we test, but how should the system behave. This creates a great deal of continuity between the language used to express Acceptance Criteria and the executable specifications written to drive the application's design.

Context/Specification

One style of Behavior-Driven Development where the behavior of the system is expressed in terms of discrete contexts with one or more observations is known as Context/Specification. Within the Context/Specification flavor of BDD, each distinct use of a software system is expressed in terms of a context within which an action is performed along with a set of assertions about how the system should behave.

There are several context/specification frameworks available for the .Net platform, the most mature of which is Machine.Specifications (a.k.a. MSpec). While MSpec is a great context/specification framework, switching to a new framework can often be problematic for teams which already have a large suite of tests written in NUnit. Additionally, 3rd-party tooling support for NUnit can be a fairly compelling reason to stay with NUnit. The NUnit.Specifications library was created for teams which are required or need to use NUnit, but want to practice BDD.

NUnit.Specifications was modeled after the Machine.Specifications API, but utilizes NUnit as its underlying test runner.

An Example

The following is an example specification:

[Subject("Authentication")]
public class When_authenticating_an_admin_user : ContextSpecification
{
    Establish context = () => {
        Subject = new SecurityService();
    };

    Because of = () => Token = Subject.Authenticate("username", "password");

    It should_indicate_the_users_role = () => Token.Role.ShouldEqual(Roles.Admin);

    It should_have_a_unique_session_id = () => Token.SessionId.ShouldNotBeNull();

    static SecurityService Subject;
    static UserToken Token;

}