Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing - Extending the Visual Studio Unit Test Type - Not working

We're asked to move from NUnit to MSTest and now have to convert all the existing tests to the new platform. Most of it converted fine but we have an issue with parameterised tests. We found the following web site showing how to write an extension and downloaded the code for it.

http://blogs.msdn.com/b/vstsqualitytools/archive/2009/09/04/extending-the-visual-studio-unit-test-type-part-2.aspx

However, although it compiles fine, the RowTestClass test all fail to run with the following error message:

"Failed to initialize the unit test extension 'urn:RunAsTestClassAttribute': A unit test extension is not registered for the following attribute: Microsoft.VisualStudio.Test.Sample.UnitTestTypeExtension.RunAsSample.RunAsTestClassAttribute."

We're using VS2010 (10.0.40219.1 SP1Rel) (Microsoft .NET Framework Version 4.0.30319 SP1Rel)

I wonder if it has something to do with the following libraries:

Microsoft.VisualStudio.QualityTools.Common.dll
Runtime Version: v2.0.50727

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.VisualStudio.QualityTools.Common.dll

Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
Runtime Version: v4.0.30319

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll

Microsoft.VisualStudio.QualityTools.Vsip.dll
v4.0.30319

C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.Vsip\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.QualityTools.Vsip.dll

Any help to get this working greatly appreciated.

like image 245
ArchiFactor Avatar asked Nov 26 '12 14:11

ArchiFactor


People also ask

How do I run a specific unit test in Visual Studio?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.

Is NUnit better than MSTest?

The main difference is the ability of MsTest to execute in parallel at the method level. Also, the tight integration of MsTest with Visual Studio provides advantages in both speed and robustness when compared to NUnit. As a result, I recommend MsTest.

How do you unit test a class that extends a base class?

If you really need to extend the base class to override something, extract the functionality to a third class and make the extended class a proxy. The extended class does nothing else than call methods on the third class. E.g. This way, you can test the real implementation, without instantiating the base class.

How do I create a unit test in Visual Studio?

From the code editor window, right-click and choose Create Unit Tests from the right-click menu. The Create Unit Tests menu command is only available for C# code. To use this method with . NET Core or .


2 Answers

Have you registered the new types (in the Windows Registry)? There's a section on how to do it in the tutorial - have a look at Row Test Test Type Extension: Registering your test type extension.

If you're having trouble, try downloading the complete code + registry keys from the MSDN code gallery.

like image 130
Dror Helper Avatar answered Oct 26 '22 07:10

Dror Helper


You may want to look into using SpecFlow to make your tests more parameterized. It is traditionally a BDD tool, but it will allow you to make re-usable steps that take parameters.

For example you can create a step saying

"Given the user 'admin' exists."

and on the c# side you get a method like this:

[Given(@"the user '(.*)' exists"]
public void GivenTheUserExists(string name)
{
    //create the user
}

Going forward you can reuse the step in any "feature file" without having to rewrite the implementation of that step.

like image 29
bobby Avatar answered Oct 26 '22 08:10

bobby