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’

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.
Tags: Code Samples, Integration, Library, Unit Test

Subscribe
February 18th, 2010 at 12:12 am
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
February 22nd, 2010 at 9:34 am
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
March 2nd, 2010 at 3:25 pm
Thanks, It works.
March 17th, 2010 at 9:02 pm
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
March 22nd, 2010 at 11:10 am
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
April 15th, 2010 at 12:30 pm
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
April 16th, 2010 at 9:42 am
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
March 28th, 2011 at 2:03 pm
[...] Using NUnit for Test Execution [...]
January 17th, 2013 at 1:29 pm
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
January 18th, 2013 at 12:59 pm
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
February 6th, 2013 at 8:05 am
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