2

If I am using both statements at the same time

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;

I get an error for Assert method. Could someone tell me what is the best way of using it.

Should I replace each Assert method with the following?

NUnit.Framework.Assert.AreEqual(expectedResult, result);
4
  • 1
    Why do you have using directives for both frameworks? I would suggest you pick one framework and stick to it. Commented Oct 2, 2016 at 20:21
  • I was trying to use [ExpectedException(typeof(ArgumentException))] which is inside Microsoft.VisualStudio.TestTools.UnitTesting Commented Oct 2, 2016 at 20:33
  • 1
    I'd strongly advise against using that to start with. Use Asset.Throws instead, which tests that exactly the piece of code you expect to throw an exception actually throws it. Commented Oct 2, 2016 at 20:34
  • NUnit used to have ExpectedException in 2.x (like MSTest still has). If you have the opportunity to pick between MSTest and NUnit I would definitely pick NUnit. Commented Oct 3, 2016 at 14:03

2 Answers 2

4

As Jon Skeet mentions in his comment, you should only be using one test framework. They will not play nicely together. If you want to use NUnit, you should,

  1. Remove all of your Microsoft.VisualStudio.TestTools.UnitTesting using statements.
  2. Remove the reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework from your project references.

Your unit tests will only end up running under one of the test adapters, MSTest or NUnit. The asserts for each test framework throw different exceptions on assert failures and each test runner expects its own exceptions to be thrown. If you try to use an MSTest assert in your tests and your test fails, NUnit will not recognize the exception thrown as an assertion exception, so it will report it as an error as opposed to a test failure.

You likely ended up with both test frameworks because you created a Unit Test Project in Visual Studio, then added NUnit to it. NUnit test projects should just be a regular Class Library. Another option is to install the NUnit Templates Extension for Visual Studio which will add an NUnit 3 Unit Test Project template to Visual Studio.

Sign up to request clarification or add additional context in comments.

Comments

0

You can either decide to use NUnit or the built-in Visual Studio Unit Testing DLL.

NUnit
If you want to use NUnit, and it sounds like you already have the NUnit NuGet package installed, you simply comment out the line:

using Microsoft.VisualStudio.TestTools.UnitTesting;

If you want to really remove the native Visual Studio Unit Testing from your project, you could delete that line and remove your reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll. This should not be required, but it could prevent any of those "ambiguous reference" errors that could possibly occur.

Visual Studio Tools
If you wanted to use the Visual Studio native tools, however, you'd want to go to the NuGet Package Manager, find the NUnit package, and select to uninstall it and click OK on the prompt. It would proceed to remove the NUnit DLLs and dependencies. You would remove the

using NUnit.Framework;

line manually from the top of your Unit Test class. You would need to ensure you have the Visual Studio Unit Testing DLL referenced. If you don't have it already referenced, you'd need to go to References > Add References and browse to it. Its location can vary depending on the Visual Studio version you have. I answered a question on that, here:

Where to find "Microsoft.VisualStudio.TestTools.UnitTesting" missing dll?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.