Sugafree Sugafree - 2 months ago 13
C# Question

NUnit Assert method error as it is found in NUnit.Framework and Microsoft.VisualStudio.TestTools.UnitTesting namespace as well

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);

Answer

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.

Comments