📜 ⬆️ ⬇️

Toolkit: NBehave

Nbehave

A library for .NET that implements the concept of behavior-driven development (BDD), which is essentially a superstructure above test-driven development (TDD), with well-known NUnit at the head.

Sooner or later, all developers have to write / change unit tests. As a rule, after a month / year, the developer no longer remembers which test, what he checks and how he does it. Even worse, if you inherit hundreds of tests, even understanding what the test is testing is not always easy. And if a team of analysts is working on the project who want to test the work of logic, but they do not know the programming language?
')
And then NBehave appears on a white horse, its task is to facilitate the maintenance and support of unit tests and provide non-programmers with the opportunity to test the logic.

See an example:
[Theme]
public void Transfer_to_cash_account()
{

Account savings = null ;
Account cash = null ;

Story transferStory = new Story( "Transfer to cash account" );

transferStory
.AsA( "savings account holder" )
.IWant( "to transfer money from my savings account" )
.SoThat( "I can get cash easily from an ATM" );

transferStory
.WithScenario( "Savings account is in credit" )

.Given( "my savings account balance is $balance" , 100, accountBalance => { savings = new Account(accountBalance); })
.And( "my cash account balance is $balance" , 10, accountBalance => { cash = new Account(accountBalance); })
.When( "I transfer $amount to cash account" , 20, transferAmount => savings.TransferTo(cash, transferAmount))
.Then( "my savings account balance should be $balance" , 80, expectedBalance => savings.Balance.ShouldEqual(expectedBalance))
.And( "my cash account balance should be $balance" , 30, expectedBalance => cash.Balance.ShouldEqual(expectedBalance));

transferStory
.WithScenario( "Savings account is overdrawn" )

.Given( "my savings account balance is -20" )
.And( "my cash account balance is 10" )
.When( "I transfer 20 to cash account" )
.Then( "my savings account balance should be -20" )
.And( "my cash account balance should be 10" );
}


* This source code was highlighted with Source Code Highlighter .


As a result of the test, we get the following output:

Story: Transfer to cash account

Narrative:
As a savings account holder
I want to transfer money from my savings account
So that I can get cash easily from an ATM

Scenario 1: Savings account is in credit
Given my savings account balance is 100
And my cash account balance is 10
When I transfer 20 to cash account
Then my savings account balance should be 80
And my cash account balance should be 30

Scenario 2: Savings account is overdrawn
Given my savings account balance is -20
And my cash account balance is 10
When I transfer 20 to cash account
Then my savings account balance should be -20
And my cash account balance should be 10


* This source code was highlighted with Source Code Highlighter .


Writing Story comes down to:

Well, about the cons of the library, from personal experience:

The first time I had to meet her when she was still very raw.
It is quite difficult to get used to the syntax and it is often necessary to look at examples.
In the current version, we added an alternative-simplified approach to writing Story, although I haven’t yet tested it in practice.
Adds work to programmers.
0.4 build managed to run only from the SVN repository, 0.3 worked from the installer.
There is no normal documentation, you need to understand everything yourself = (
Reference example 0.3 - grabbagoft.blogspot.com/2007/09/authoring-stories-with-nbehave-03.html

Source: https://habr.com/ru/post/65922/


All Articles