Using NUnit for test execution

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

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

Tags: , , ,

11 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

  8. Combining Ranorex 3.0 and Visual Studio 2010 for UI Unit Testing | Ranorex Blog

    [...] Using NUnit for Test Execution [...]

  9. Claudia

    Hi,
    I’m using Nunit and Ranorex since 2 Years, but since a view weeks the nunit app stops working during the run of the testcases.

    I work with NUnit 2.6.2.

    Did you hear from this problem before? Do you know what the problem could be?

    Thx Claudia

  10. Ranorex Team

    Hi Claudia,
    I have tried a test run (NUnit 2.6.2 and Ranorex 4.0.1) without problems.

    Do you get any error messages?
    What shows exception details? (Tools->Exception Details)
    Could you please provide me with information about your system i.e. OS, Ranorex Version, System Under Test?

    Try out the following:
    – Disable “Shadow Copy” (Tools->Settings->Test Loader->Advanced)
    – Default Domain Usage is set to “Use a single AppDomain for all tests”
    – Default Process Model is set to “Run tests directly in the NUnit process”

    Best regards,
    Christian
    Ranorex Team

  11. Claudia

    Hi,
    so today I had that problem with NUnit again.
    I got following information:
    Problemsignatur:
    Problemereignisname: APPCRASH
    Anwendungsname: nunit-agent-x86.exe
    Anwendungsversion: 2.6.2.12296
    Anwendungszeitstempel: 50861a22
    Fehlermodulname: clr.dll
    Fehlermodulversion: 4.0.30319.296
    Fehlermodulzeitstempel: 50484aa9
    Ausnahmecode: c0000005
    Ausnahmeoffset: 001ac7dd
    Betriebsystemversion: 6.1.7601.2.1.0.256.4
    Gebietsschema-ID: 3079
    Zusatzinformation 1: 8c71
    Zusatzinformation 2: 8c71959688718f2aab31ea9a70f1f381
    Zusatzinformation 3: d3ec
    Zusatzinformation 4: d3ec1fb42434ae93a4b4bb3c00ee4d7d
    Lesen Sie unsere Datenschutzbestimmungen online:
    http://go.microsoft.com/fwlink/?linkid=104288&clcid=0×0407
    Wenn die Onlinedatenschutzbestimmungen nicht verfügbar sind, lesen Sie unsere Datenschutzbestimmungen offline:
    C:\Windows\system32\de-DE\erofflps.txt

    Additional information:
    Windows7
    Ranorex Version 3.3.4

    I could not try that what you told me because I couldn’t find that “Shadow Copy” and the other things.
    I do not record my testcases, I code them in Visual Studio 2010.

    So today I saw that there is a new version of Ranorex available, so I will try with them.
    If I get the problem again I will tell you :)

    thank at the moment
    br Claudia

Leave a Reply