Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given -When-Then example with NUnit

Can anybody point me to some resources for Give-When-Then style of testing with NUnit?

like image 567
epitka Avatar asked Oct 29 '09 13:10

epitka


People also ask

How do you write a TestCase using NUnit?

To mark a method as a test case, we need to add a Test attribute. It tells the NUnit framework that it should run this method. It's a good approach to run our test methods with different arguments, without copy-pasting our test methods. We can define test method parameters by using the [TestCase] attribute.

What is NUnit used for?

NUnit provides a console runner (nunit3-console.exe), which is used for batch execution of tests. The console runner works through the NUnit Test Engine, which provides it with the ability to load, explore and execute tests.

How do I use NUnit in .NET framework?

So for using Nunit we need to go for Nuget Package Manager and download it. Now after installing the Nunit we need one more dll that needs to be installed in the project. The NUnit Test Adapter allows you to run NUnit tests inside Visual Studio.


2 Answers

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/

like image 169
Joseph Avatar answered Sep 19 '22 17:09

Joseph


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.

like image 40
Pete Avatar answered Sep 19 '22 17:09

Pete