NUnit is an open source testing framework for .NET languages. It´s based on the XUnit testing design which contains powerful functionality for test execution. In this article I want to show how to make your Ranorex Tests fit for NUnit.

Requirements:

NUnit –  http://www.nunit.org/index.php?p=download
(preferred version >= 2.5)

1.) Add the NUnit reference to your project
Right-click on your project and choose ‘Add Reference’
Select ‘nunit.framework’ from the ‘GAC’ tab

2.) Create a new NUnit testcase class
Right-click on your project and choose ‘Add->New Item’
Select ‘new empty file’ and click create

3.) Add the following lines of code:

using NUnit.Framework;

[TestFixture, RequiresSTA]
public class TestCase1
{
	[TestFixtureSetUp]
	public void Init()
	{
	}

	[Test]
	public void Test1()
	{
	}

	[TestFixtureTearDown]
	public void Dispose()
	{
	}
}

4.) Insert the recorded or manual test calls
Write your automation code into the Test1 method
Use the ‘TestFixtureSetUp’ and ‘TearDown’ methods to initialize and set you Software Under Test (SUT) back to its original state

5.) Open your dll/exe with the ‘NUnit GUI Runner’ and start your tests
Navigate to a specific test in the test tree
Right-click on the file node and click ‘Run’ or ‘Run all’

NUnit GUI Test Runner

Or:

If you want to call your existing recordings directly, declare them as NUnit tests:

1.) Open the ‘Recording.UserCode’ file
Navigate to the recording node in Project Browser and open the ‘.usercode’ file

2.) Declare your Recording class as ‘[TestFixture]’
Set the ‘TestFixture’ attribute at the top of the class (see code below)

3.) Create a new NUnit test method
Insert a new method ‘StartRecording’ into the usercode (“Recording1.UserCode.cs” file, see code below)

e.g.

[TestFixture, RequiresSTA]
public partial class Recording1
{
         /*
        ...
        */

        [Test]
        public void StartRecording()
        {
             Recording1.Start();
        }

        /*
        ....
        */
}

Now we are able to call every single test method from the NUnit Test Runner and we don´t have to worry about the build and cleanup of our testing environment. We are also free to choose which test methods we want to execute for each test run.

You might also like these articles