How to run a unit Test in Visual Studio 2010 using Watin - visual-studio-2010

I have created a class library prject, and created a class as described in the Video Tutorial at Watin
and using the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using WatiN.Core;
namespace ClassLibrary2
{
[TestFixture]
[RequiresSTA]
public class Class1
{
[Test]
public void SearchForWatiNOnGoogle()
{
using (var browser = new IE("http://www.google.com"))
{
browser.TextField(Find.ByName("q")).TypeText("WatiN");
browser.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(browser.ContainsText("WatiN"));
}
}
}
}
now what is the next step, How to run the test? Do I add the class library to a project and run calls the test method from code? or do I create a Unit Test Project (how?) ?
Thanks

To execute your test you need to compile your code, then run the test through a test runner.
The first way I ever did this was using the NUnit GUI
Compile your code to a class library. The result being a .dll file; something like MyTests.dll
Open the NUnit GUI. Start -> Programs -> etc etc etc.
In the NUnit GUI, open the .dll file that contain your tests.
In the NUnit GUI, select the test(s) you wish to run and click the Run button.
There are plenty of other ways to run your tests, but I believe the NUnit GUI to be the most basic for NUnit tests. Other options include: In Visual Studio via ReSharper, Gallio, in VS via External Tools, and many others.

Related

Can I filter NUnit tests based on Property in Visual Studio test explorer

Is it possible to construct a Visual Studio Test Explorer search expression that will actually filter NUnit tests based on the name AND value of a Property attribute? For example,
something like Trait:TestSize=Large. I can't seem to figure out how to get this to work and after hours of searching it's unclear if I should be looking for documentation from Visual Studio, NUnit, or the NUnit Test Adapter.
[TestFixture]
public class SampleTestFixture {
[Test]
[Property("TestSize", "Large")]
public void Test1() { ... }
}
More details:
Currently using VS 2015, but I want this to work in VS 2017 and 2019
Using NUnit 3.12
Using NUnit3TestAdapter 3.8
Im not sure Im understood corectly, but If you want filtering tests for run what you want based on TestCaseName you should use TestEngineAPI.
For more info - https://github.com/nunit/docs/wiki/Test-Engine-API
ITestEngine engine = TestEngineActivator.CreateInstance();
TestPackage package = new TestPackage("my.test.assembly.dll");
ITestRunner runner = engine.GetRunner(package);
// Run all the tests in the assembly
XmlNode testResult = runner.Run(this, TestFilter.Empty);
where TestFilter.Empty can be something like this:
new TestFilter($"<filter><or><name>Test1</name></or></filter>");

How can I run NUnit tests in Visual Studio 2017?

I've just installed Visual Studio 2017. I have a project using NUnit for the test cases. Ctrl + R - T no longer runs the tests, and the Test Explorer no longer finds any test cases marked with the TestCase attribute.
Is there a way to get NUnit running yet, or an update I could find?
I reinstalled NUnit from the NuGet Package Manager to the latest version with no improvement.
Add the NUnit test adapter NuGet package to your test projects
2.* (https://www.nuget.org/packages/NUnitTestAdapter/)
3.* (https://www.nuget.org/packages/NUnit3TestAdapter/)
Or install the Test Adapter Visual Studio extension. There is one for
2.* (https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnitTestAdapter)
3.* (https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnit3TestAdapter).
I prefer the NuGet package, because it will be in sync with the NUnit version used by your project and will thus automatically match the version used in any build server.
You need to install NUnitTestAdapter. The latest version of NUnit is 3.x.y (3.6.1) and you should install NUnit3TestAdapter along with NUnit 3.x.y
To install NUnit3TestAdapter in Visual Studio 2017, follow the steps below:
Right click on menu Project → click "Manage NuGet Packages..." from the context menu
Go to the Browse tab and search for NUnit
Select NUnit3TestAdapter → click Install at the right side → click OK from the Preview pop up
This one helped me:
Getting started with .NET unit testing using NUnit
Basically:
Add the NUnit 3 library in NuGet.
Create the class you want to test.
Create a separate testing class. This should have [TestFixture] above it.
Create a function in the testing class. This should have [Test] above it.
Then go into TEST/WINDOW/TEST EXPLORER (across the top).
Click run to the left-hand side. It will tell you what has passed and what has failed.
My example code is here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace NUnitTesting
{
class Program
{
static void Main(string[] args)
{
}
}
public class Maths
{
public int Add(int a, int b)
{
int x = a + b;
return x;
}
}
[TestFixture]
public class TestLogging
{
[Test]
public void Add()
{
Maths add = new Maths();
int expectedResult = add.Add(1, 2);
Assert.That(expectedResult, Is.EqualTo(3));
}
}
}
This will return true, and if you change the parameter in Is.EqualTo it will fail, etc.
You need to install three NuGet packages:
NUnit
NUnit3TestAdapter
Microsoft.NET.Test.Sdk
You have to choose the processor architecture of unit tests in Visual Studio:
menu Test → Test Settings → Default processor architecture
Test Adapter has to be open to see the tests: (Visual Studio e.g.:
menu Test → Windows → Test Explorer
Additional information what's going on, you can consider at the Visual Studio 'Output-Window' and choose the dropdown 'Show output from' and set 'Tests'.
For anyone having issues with Visual Studio 2019:
I had to first open menu Test → Windows → Test Explorer, and run the tests from there, before the option to Run / Debug tests would show up on the right click menu.
Using the CLI, to create a functioning NUnit project is really easy. The template does everything for you.
dotnet new -i NUnit3.DotNetNew.Template
dotnet new nunit
On .NET Core, this is definitely my preferred way to go.
Install the NUnit and NunitTestAdapter package to your test projects from Manage Nunit packages. to perform the same:
1 Right-click on menu Project → click "Manage NuGet Packages".
2 Go to the "Browse" tab -> Search for the Nunit (or any other package which you want to install)
3 Click on the Package -> A side screen will open "Select the project and click on the install.
Perform your tasks (Add code)
If your project is a Console application then a play/run button is displayed on the top click on that any your application will run and If your application is a class library Go to the Test Explorer and click on "Run All" option.
To run or debug tests in Visual Studio 2017, we need to install "NUnit3TestAdapter". We can install it in any version of Visual Studio, but it is working properly in the Visual Studio "community" version.
To install this, you can add it through the NuGet package.

Visual Studio 2013 - Edit Project Template

I've created a new template by "File -> Export template" command. I want source file name to be such as project name, so, I've edited ".vstemplate" file. But now - how do I use this updated template? Seems like VS keeps copies of templates somewhere else, because after restarting VS - nothing changed.
By the way, in VS2012 by developer command prompt was possible to install modified templates by the command "devenv /installvstemplates". Now, this command is gone, it is not a command or a file.
So, how can I use this updated template file?
The Community/Express (free) versions do not support the /installvstemplates options.
If you are using another version, the steps for customising an existing template are as follows (taken from my blog here: http://blog.hitechmagic.com/?p=527):
1. Find the files
The files have not moved around much so are currently in C:\<Program Files>\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033
(in VS2012 they are in C:\<Program Files>\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033)
The file you want is in an appropriately named folder (no more ZIP files!). If you open the Class folder you will find the following 2 files:
Class.cs
Class.vstemplate
2. Backup the original files!
Very important step as things do go wrong. Backup the original files e.g. by copying it to Class.cs. Orig so that it is not recognised by Visual Studio.
3. Change the Class.cs template file
This is where you get to be creative. You will notice the original looks something like this:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
{
class $safeitemrootname$
{
}
}
You might want something more like this:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
{
public class $safeitemrootname$
{
#region Constants
#endregion Constants
#region Fields
#endregion Fields
#region Properties
#endregion Properties
#region Constructors
#endregion Constructors
#region Public methods
#endregion Public methods
#region Class override methods
#endregion Class override methods
#region Class extensions - virtual methods
#endregion Class extensions - virtual methods
#region Private helper methods
#endregion Private helper methods
}
}
You will notice a number of Macros $ commands in the template. The complete list is here: http://msdn.microsoft.com/en-us/library/eehb4faa.aspx.
4. Save your changes
Simply save your edited files (again, there is no longer a ZIP file to re-compress with VS 2012/2013).
5. Tell Visual Studio about the changes
Your new changes will not be loaded unless you explicitly tell Visual Studio to reload all templates.
Close Visual Studio (or the change swill not show until next time you run it)
Open a command prompt (you should run this as Administrator if you are not an admin of the machine).
Change to the IDE folder a few levels above the template folder (e.g. to C:\<Program Files>\Microsoft Visual Studio 10.0\Common7\IDE)
Run the following command: devenv.exe /installvstemplates
Viola! There you have your own custom template for new classes.
6. Automate repetitive/fiddly tasks
If you are going to do this a lot I would suggest creating a batch file, e.g. in your template folder, to run the update step for you. For example create a text file called UpdateTemplates.bat containing the following 3 lines:
cd "C:\Prograsm Files (x86)\Microsoft Visual Studio 12.0\Common\IDE\"
devenv.exe /installvstemplates
pause

Visual Studio 2013 - no longer able to run xunit tests side-by-side with MSTest

Q: Is it possible to run xunit tests side-by-side with MSTest in VS 2013? If so, what am I doing wrong?
Background:
I've migrated a .NET Solution from VS 2012 format to VS 2013.
I was unable to get the xUnit tests to work. After much troubleshooting (experimenting with project types, MS Tools versions, creating new projects with just xUnit, and experimenting with xUnit versions and dependencies), I was able to narrow the problem down to having MSTests in the same project as xUnit tests. This worked before in VS 2012.
As soon as I include just one test method marked with the [TestMethod] attribute, none of the xUnit tests will be run. They may appear in either the VS Test Explorer, or the ReSharper Unit Test Sessions panels, but they appear with either a [!] under Not Run Tests (for Test Explorer) or [?] (for Unit Test Sessions). Having a class with the [TestClass] attribute, but not a method marked with [TestMethod] still allows xUnit tests to run.
It may turn out that this is a bug in an xUnit component, but I'd like to see whether anyone else has had any experience to the contrary.
Note: nearly all unit tests are based on xUnit, the MSTest is just there as a proof-of-concept to ensure that it's supported in case MSTests are to be used later.
Code Excerpts:
MSTest:
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace YYY.XXX.Test.Unit
{
[TestClass]
public class MSTests
{
[TestMethod]
public void Test_Blah()
{
Assert.AreEqual(2, 2);
}
}
}
xUnit:
using Xunit;
namespace YYY.XXX.Test.Unit
{
public class FactTests
{
[Fact]
public void Test_Blah()
{
Assert.Equal(2, 2);
}
}
}
SW Versions:
MS Visual Studio Ultimate 2013 12.0.210051 REL
JetBrains ReSharper 8.1
xUnit.net runner for Visual Studion 2012 and 2013 v0.99.2
ReSharper extension:
xUnit.net Test Support v1.3.0
xUnit Project packages:
xunit.1.9.2
xunit.extensions.1.9.2
Resharper problem xunit/vstestrunner fix: (Took a long time to figure this one out too)
You can get resharper 8.2 or higher.
Or -
One turn off Resharper Tests
In VS click Resharper -> Options -> Unit tests - Uncheck Enable Unit tests.
and
Test -> Test Settings-> Uncheck Keep test execution engine running.
This should allow you to have both engines run. I think what is happening is that once one is started it stays and it's difficult to get the other to work
keyword: executor://xunit/VsTestRunner2

Gallio test runner plugin to Visual Studio 2008 and 2010 for MBUnit tests

If I install Gallio 3.x will it also install a test runner plugin for Visual Studio?
Or must I use an additional plug-in like TestDriven.NET or Visual Nunit to run MbUnit test classes from within VS?
Install Gallio 3.1 on the dev machine. Then in VS2008, you'll have the option to create a "MbUnit v3 Test Project". This doesn't just include all of the Gallio dlls for you, it has a magic line in the project which identifies it to VS as a Test project.
You can now just use the in-built VS2008 Test runner.
If you have any existing projects with unit tests in, rather than making new projets, edit your existing project file and add the following line on line 9 (underneath the <ProjectGuid> on line 8):
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
(If you have a VB project, it has a different second GUID: <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids> You can find the correct values by creating a new MbUnit test project from the templates installed with Gallio and then looking at the project file (.csproj or .vbproj) in a text editor.)
Now when you reload the project, VS2008 will recognise it as a test project.
A distinct advantage that I found over using Icarus was that debugging is now far more straight forward with break points being hit as expected.
Good Luck, Lee
TestDriven.Net works really well. Gallio also supports the ReSharper unit test runner and Visual Studio test tools. We will be shipping a new release of Gallio this week with support for R# 5.0 and VS 2010.
These are instruction for running MBUnit tests in Visual Studio 2012 and above using a neat NUnit trick.
Firstly, install the NUnit Test Adapter extension (yes, NUnit)
Tools > Extension and Updates > Online > search for NUnit > install
NUnit Test Adapter.
You may need to restart the Visual Studio IDE.
Then, you simply need to add a new NUnit test attribute to your test methods. See example code here (notice the using statements at the top) ...
//C# example
using MbUnit.Framework;
using NuTest = NUnit.Framework.TestAttribute;
namespace MyTests
{
[TestFixture]
public class UnitTest1
{
[Test, NuTest]
public void myTest()
{
//this will pass
}
}
}
You can run and debug the test in visual studio as NUnit and Gallio Icarus GUI Test Runner will run them as MBUnit (enabling parallel runs for example). You will need to stop Gallio from running the NUnit tests by deleting the NUnit folder in the gallio install location i.e. C:\Program Files\Gallio\bin\NUnit
Hope this helps, this is a simple working method so please vote up, many thanks.

Resources