Ranorex Blog - Software Automation & Automated Testing Blog

Posted by cschmid on Friday, July 31st, 2009 at 9:12 am to Test Automation

Using NUnit for test execution

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.

Share/Save/Bookmark

7 comments

  1. julie

    Failed step 1, I don’t know why. I could not found NUnit.Framework references for Gac tab. ( NUnit installation is correct 2.5.3 )
    Thanks

  2. Ranorex Team

    It seems that the ‘Nunit.Framework.dll’ isn`t installed into the GAC with normal installation routine anymore.

    Add the ‘Nunit.Framework.dll’ from the ‘Bin’ directory of the NUnit installation instead:

    Right click on project node -> ‘Add Reference’
    Go to the ‘.NET Assembly Browser’ tab
    Select the ‘Nunit.Framework.dll’ in the ‘NUnitFolder/bin/net-2.0/framework’ folder

    Best regards,
    Christian
    Ranorex Team

  3. julie

    Thanks, It works.

  4. Philipp Dolder

    As today many who use Visual Studio have ReSharper plugin, I find it important to add that you should not use ReSharper NUnit test runner to run your ranorex tests. ReSharper does not delegate the [RequiresSTA] attribute to the NUnit framework. Therefore not all UI Elements will behave as expected and ranorex cannot find them on your UI. I ran into this problem with DevExpress components for winforms applications.

    Best Regards
    Philipp

    bbv Software Services AG
    Switzerland

  5. Ranorex Team

    Hi Philipp,
    you can set the ApartmentState property in the App.config file of your dll/exe. After telling the NUnit TestRunner to start the thread via STA you can also start it with the Jetbrains ReSharper.

    Dll.Config section:
    http://msdn.microsoft.com/en-us/magazine/dd744751.aspx

    Best regards,
    Christian
    Ranorex Team

  6. Philipp Dolder

    Hello Christian

    Thanks for your hint. I tried it out, but now ReSharper does not run the tests at all. Just giving me a green bar with the latest 4.5 Version.

    Any other ideas?

    Best Regards
    Philipp

    bbv Software Services AG
    Switzerland

  7. Ranorex Team

    Hi again!
    I have tried some NUnit tests with the ‘Unit Test Explorer’ from ReSharper 4.5 without problems.
    Which Visual Studio version do you use? There are knowing issues with Visual Studio 2008 (before SP1).
    Do you have declared your ‘TestFixture’ and ‘Test’ methods correctly?

    Best regards,
    Christian
    Ranorex Team

Leave a Reply