MSTest exceptions

by on under programming
1 minute read

MS test for exception thrown

If you switch between the two frameworks often and use MS test you may find this useful.

Old MS test

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

In old dotnet framework test library you don’t have the Assert.Throws.

So use below try catch return.

public void GetStuff_ThrowsException()
    string foobarString = "testing stuff";
    decimal foobarDecimal = 21;

        var result = _myController.GetStuff(foobarString, foobarDecimal);
    catch (Exception ex)
    Assert.Fail("Should have thrown an exception.");

New MS test


In new dotnet core test library you can use the following methods.



mstest, UnitTestFramework, dotnet, core, framework
comments powered by Disqus