With Visual Studio Express for Desktop, you can easily run and debug tests within the IDE using MSTest tool. But if there is a need to do the same with only NUnit tool, you have to do some tricks. The problem is that the Express edition does not allow installing any extension tool and does not allow debugging class library projects. But it allows running and debugging of console applications. So, you may do your class library being pretending a console application and Visual Studio will allow debugging the code.
1. Create new class inside the Tests project. Name it somehow, for example, Runner.
2. Add a reference to the nunit-console-runner.dll library in the Tests project. It is located inside NUnit folder (on my machine the path is C:\Program Files\NUnit 2.6.3\bin\lib)
3. Create a method named Main() in the Runner class. The method is the entry point of the application and the necessary tests will be executed in debug mode through this method – it will start NUnit tool and NUnit will execute a test.
4. Specify what project in the solution is the start project. Click with right mouse button on the solution’s name, and then click Properties. On the property dialog select Start project, switch type of project as Multiple startup projects and then set value Start for Tests project. All other projects stay in None because they do not directly take part in running and debugging the tests.
5. Now adjust properties of the Tests project
6. In Application tab set Output type value to Console Application. Save changes (Ctrl+S)
7. In Debug tab in the working directory field specify the path to Tests project folder. This is the folder where Tests.csproj file is located. Type the file name (Tests.csproj) and additional parameters value (/include:Debug) into Command line arguments field. Save changes.
Now you may create in the Tests project inside any test class a method marked with [Test] and [Category(“Debug”)] attributes and the method will be considered by NUnit as a test which should be run in debug mode.
When a new test is created, it’s marked with the attribute [Test]. It allows running the test with NUnit GUI tool; when you try running tests inside Visual Studio with nunit-console-runner, all the tests but one marked with [Category(“Debug”)] attribute will be ignored because you specified that only this kind of tests must be executed in debug mode.
After the test is verified and debugged you can just remove [Category(‘Debug”)] attribute and add it to another test. Hit F5 or click Start button on the instrument panel of Visual Studio to start running the test in debug mode.
Please be aware you have the only one method with [Category(‘Debug”)] attribute for avoiding errors while compiling the code.
Since the tools selected for tests execution are NUnit and MSTest, the tests should meet certain rules determined by the tools. Obviously, all the tests must be created in the Tests project.
Below is an example of a simple test with Selenium and NUnit/MSTest tools.
First of all the references to necessary libraries which classes will be used in test development must be added to the project. Some of the references have been added automatically right after the project had been created.
Some should be added explicitly in order to being able to use the required classes.
Selenium WebDriver library is located in previously downloaded archive selenium-dotnet-version_number. Unpack it and select all *.dll files. Selected libraries will appear inside the References folder.
Since I’m going to develop tests for running with NUnit, I have to add a reference to the NUnit framework as well. Usually, it is located inside the bin\framework folder of installed NUnit application.
To be able to execute tests with MSTest tool I added the reference to its library too. It is named Microsoft.VisualStudio.QualityTool.UnitTestFramework.
No additional actions are required if execute tests with Mozilla Firefox browser only. But Internet Explorer and Chrome requires the presence of their own server files in project working directory.
For now all preparations are complete and the first test may be created.
The code above shows the structure of the test. The sequence of actions that are carrying on during test run is following:
One more test.
If you compile the code (Build -> Build Solution) and run all the tests with MSTest or with NUnit tool you can notice that new instance of IWebDriver interface is created for every single test (new browser instance is launched). This means that [SetUp] / [TestInitialize] method is invoked every time new test has started running and [TearDown] / [TestCleanup] method is invoked after a test finishes its work.
There are also additional types of attributes in MSTest that are very useful. They are [AssemblyInitialize()] / [AssemblyCleanup()] marked methods. The methods are invoked only once per the entire session. [AssemblyInitialize()] method is invoked in the very beginning of tests run session before any tests are started and [AssemblyCleanup()] method is performed when all work is completed. You can put a logic which affects the entire assembly inside these methods.
NUnit tool has methods with similar behavior as well but there is an important constraint for them. The methods must be located in a separate class marked with [SetUpFixture] attribute, apart the methods are not visible for the tool and cannot be performed. The class should be in the same namespace as the tests are.
One more thing I want to mention is that a good style of programming assumes having reusable code in a base class whose children inherit it. In my case such kind of code is all methods but ones marked with [Test] / [TestMethod].
Add->Class… (or hit Shift+Alt+C)
Base class may be public or internal. All auxiliary methods and variables from Example class may be moved into it; all but those marked as [Test] / [TestMethod].
Plus class with [SetUpFixture] attribute. It may be located TestBase.cs file or in separate file. The class contains method for assembly set up compatible both with NUnit and MSTest as well.
The final thing to do is to inherit the test class from the BaseTest.
The methods of GlobalSetup class will be invoked once in the very beginning and in the end of entire test session. All the methods from BaseClass are inherited in Example class and will be invoked in accordance of tests run flow described above.