开发者

Given -When-Then example with NUnit

Can anybody point me to some resources for 开发者_JAVA百科Give-When-Then style of testing with NUnit?


The Given When Then style correlates closely to the Arrange Act Assert style for unit testing.

Here's an example:

[Test]
public void RotateAngle_Given27Degress_Returns64Degrees()
{
   //Arrange or Given
   var someAngleClass = new Angle();

   //Act or When
   var result = someAngleClass.Rotate(27);

   //Assert or Then
   Assert.That(result, Is.EqualTo(64));
}

The great thing about this testing style is you don't need to see the underlying code to understand the intent of the behavior.

For more info here are some sites:

http://www.arrangeactassert.com/

Roy Osherove's Blog

http://www.artofunittesting.com/


I know this is an old question, but if you haven't already, you should check out SpecFlow. It allows you to write the spec in clear text in a feature file. The tool will auto-generate NUnit tests based on the feature file.


If you download, and add a reference to, StoryQ, you can use a nice BDD style (see samples by clicking the link) and at the same time use NUnit as usual (and TestDriven.Net, R#'s runner, or what have you).


You might also check a small library https://www.nuget.org/packages/Heleonix.Testing.NUnit/ Instead of writing comments like //Arrange, //Act, //Assert or naming unreadable methods like Given_UserIsLoggedIn_When_SomeButtonIsPressed_Then_BlaBla in you tests, the library provides ability to write tests in GWT and AAA styles in manner of JavaScript's Jasmine orJest. See Readme: https://github.com/Heleonix/Heleonix.Testing/blob/master/README.md

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜