简体   繁体   中英

Can Nunit be used with Visual C#? Which Exception Assert should be used?

Can Nunit be used with C#? Which Exception Asserts methods should be used?

I was trying to make use of Assert.DoesNotThrow but this is not recognized by Microsoft.VisualStudio.TestTools.UnitTesting .

Which Exception Assert has to be used to test a case that should not throw an exception?

NUnit is actually a .NET library, so it was created to be used with C#, but I'm guessing this was not actually your question.

I'm guessing you created a unit test project in Visual Studio, which would have created a project using Microsoft's own unit test framework (Microsoft.VisualStudio.TestTools.UnitTesting). You then googled "how to assert that a method throws" and found results around NUnit .

NUnit is also a unit test framework, so I would advise not using both at the same time. In order to use just NUnit, just create a normal C# project and add an NUnit reference.

In order to just add NUnit in a Visual Studio project, you should use NuGet unless you have a good reason not to. To do that, after creating your new normal-C#-project-meant-to-be-a-NUnit-test-project, right click on the project in the Solution Explorer, and click Manage NuGet packages... . Find NUnit and install it. This will automatically add a reference to it (and allow you updating it when you want).

Also, to solve your initial issue, the Microsoft Visual Studio framework does have a method to assert exceptions. See here .

That being said, you could use NUnit's assertions when writing Microsoft unit tests, all you have to do is reference NUnit in your existing Microsoft test project. Again, really not recommended, as you would be using two frameworks at the same time.

You could also look for other assertions framework that could complement Microsoft's own one ( Fluent Assertions being one for instance).

Or you could create your own assertion method, something along the lines of:

private static void AssertThrows<T>(Action action) where T: Exception
{
    try
    {
        action();
    }
    catch (T)
    {
        return;
    }
    Assert.Fail("Failed to fail");
}

I would really advise against this though (as much fun as writing your own framework is), because the less test framework your write yourself the better ( Who whatches the watchmen? ), and you could never make it as feature-rich and bug-poor as something that has been written by a dedicated community (NUnit) or company (VS test framework).

It seems you're confusing NUnit (which is a separate .NET library that needs to be installed ) and the Visual Studio Unit Testing Framework which comes with Visual Studio.

They follow the similar XUnit patterns , so you can write tests in a similar fashion, using different methods.

You can read more about the Assert statements included in Visual Studio Unit testing framework in the Unit Testing Framework Asserts documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM