Han*_*sir 87 .net c# unit-testing
您认为这是测试异常的好方法吗?有什么建议?
Exception exception = null;
try{
//I m sure that an exeption will happen here
}
catch (Exception ex){
exception = ex;
}
Assert.IsNotNull(exception);
Run Code Online (Sandbox Code Playgroud)
我正在使用MS Test.
tva*_*son 129
我使用了几种不同的模式.ExpectedException
当预期出现异常时,我大部分时间都使用该属性.这在大多数情况下都是足够的,但是,在某些情况下,这还不够.异常可能无法捕获 - 因为它是由反射调用的方法引发的 - 或者我只是想检查其他条件是否成立,比如事务被回滚或者某些值仍然被设置.在这些情况下,我将它包装在一个try/catch
需要确切异常的块中,Assert.Fail
如果代码成功则执行,并捕获通用异常以确保不会抛出不同的异常.
第一种情况:
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void MethodTest()
{
var obj = new ClassRequiringNonNullParameter( null );
}
Run Code Online (Sandbox Code Playgroud)
第二种情况:
[TestMethod]
public void MethodTest()
{
try
{
var obj = new ClassRequiringNonNullParameter( null );
Assert.Fail("An exception should have been thrown");
}
catch (ArgumentNullException ae)
{
Assert.AreEqual( "Parameter cannot be null or empty.", ae.Message );
}
catch (Exception e)
{
Assert.Fail(
string.Format( "Unexpected exception of type {0} caught: {1}",
e.GetType(), e.Message )
);
}
}
Run Code Online (Sandbox Code Playgroud)
Ica*_*ato 36
现在,2017年,您可以使用新的MSTest V2 Framework轻松完成:
Assert.ThrowsException<Exception>(() => myClass.MyMethodWithError());
//async version
await Assert.ThrowsExceptionAsync<SomeException>(
() => myObject.SomeMethodAsync()
);
Run Code Online (Sandbox Code Playgroud)
小智 17
我是新来的,并没有评论或downvote的声誉,但想在安迪怀特的回复中指出这个例子中的一个缺陷:
try
{
SomethingThatCausesAnException();
Assert.Fail("Should have exceptioned above!");
}
catch (Exception ex)
{
// whatever logging code
}
Run Code Online (Sandbox Code Playgroud)
在我熟悉的所有单元测试框架中,Assert.Fail
通过抛出异常来工作,因此泛型catch实际上会掩盖测试的失败.如果SomethingThatCausesAnException()
没有抛出,Assert.Fail
意志,但永远不会冒出来给测试运行器指示失败.
如果需要捕获预期的异常(即,断言某些细节,例如异常上的消息/属性),捕获特定的预期类型而不是基本的Exception类很重要.这将允许Assert.Fail
异常冒泡(假设您没有抛出与单元测试框架相同的异常类型),但仍允许对您的SomethingThatCausesAnException()
方法抛出的异常进行验证.
Stu*_*tLC 15
从v 2.5开始,NUnit具有以下Assert
用于测试异常的方法级别:
Assert.Throws,它将测试一个确切的异常类型:
Assert.Throws<NullReferenceException>(() => someNullObject.ToString());
Run Code Online (Sandbox Code Playgroud)
并且Assert.Catch
,它将测试给定类型的异常,或者从此类型派生的异常类型:
Assert.Catch<Exception>(() => someNullObject.ToString());
Run Code Online (Sandbox Code Playgroud)
另外,在调试抛出异常的单元测试时,您可能希望阻止VS 在异常上中断.
编辑
只是举一个下面马修评论的例子,泛型的返回 Assert.Throws
和Assert.Catch
异常类型的异常,然后您可以检查以进一步检查:
// The type of ex is that of the generic type parameter (SqlException)
var ex = Assert.Throws<SqlException>(() => MethodWhichDeadlocks());
Assert.AreEqual(1205, ex.Number);
Run Code Online (Sandbox Code Playgroud)
byt*_*dev 11
不幸的是,MSTest STILL只有ExpectedException属性(只显示MS对MSTest的关注程度)IMO非常糟糕,因为它打破了Arrange/Act/Assert模式,并且它不允许您准确指定您期望异常的代码行发生在.
当我使用(/被客户端强制)使用MSTest时,我总是使用这个助手类:
public static class AssertException
{
public static void Throws<TException>(Action action) where TException : Exception
{
try
{
action();
}
catch (Exception ex)
{
Assert.IsTrue(ex.GetType() == typeof(TException), "Expected exception of type " + typeof(TException) + " but type of " + ex.GetType() + " was thrown instead.");
return;
}
Assert.Fail("Expected exception of type " + typeof(TException) + " but no exception was thrown.");
}
public static void Throws<TException>(Action action, string expectedMessage) where TException : Exception
{
try
{
action();
}
catch (Exception ex)
{
Assert.IsTrue(ex.GetType() == typeof(TException), "Expected exception of type " + typeof(TException) + " but type of " + ex.GetType() + " was thrown instead.");
Assert.AreEqual(expectedMessage, ex.Message, "Expected exception with a message of '" + expectedMessage + "' but exception with message of '" + ex.Message + "' was thrown instead.");
return;
}
Assert.Fail("Expected exception of type " + typeof(TException) + " but no exception was thrown.");
}
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
AssertException.Throws<ArgumentNullException>(() => classUnderTest.GetCustomer(null));
Run Code Online (Sandbox Code Playgroud)
作为使用ExpectedException
属性的替代方法,我有时为我的测试类定义了两个有用的方法:
AssertThrowsException()
接受一个委托并声明它会抛出预期的异常和预期的消息.
AssertDoesNotThrowException()
采用相同的委托并断言它不会抛出异常.
当您想要测试在一种情况下抛出异常而不是另一种情况时抛出异常时,此配对非常有用.
使用它们我的单元测试代码可能如下所示:
ExceptionThrower callStartOp = delegate(){ testObj.StartOperation(); };
// Check exception is thrown correctly...
AssertThrowsException(callStartOp, typeof(InvalidOperationException), "StartOperation() called when not ready.");
testObj.Ready = true;
// Check exception is now not thrown...
AssertDoesNotThrowException(callStartOp);
Run Code Online (Sandbox Code Playgroud)
好又整洁吧?
我AssertThrowsException()
和AssertDoesNotThrowException()
方法在一个公共基类上定义如下:
protected delegate void ExceptionThrower();
/// <summary>
/// Asserts that calling a method results in an exception of the stated type with the stated message.
/// </summary>
/// <param name="exceptionThrowingFunc">Delegate that calls the method to be tested.</param>
/// <param name="expectedExceptionType">The expected type of the exception, e.g. typeof(FormatException).</param>
/// <param name="expectedExceptionMessage">The expected exception message (or fragment of the whole message)</param>
protected void AssertThrowsException(ExceptionThrower exceptionThrowingFunc, Type expectedExceptionType, string expectedExceptionMessage)
{
try
{
exceptionThrowingFunc();
Assert.Fail("Call did not raise any exception, but one was expected.");
}
catch (NUnit.Framework.AssertionException)
{
// Ignore and rethrow NUnit exception
throw;
}
catch (Exception ex)
{
Assert.IsInstanceOfType(expectedExceptionType, ex, "Exception raised was not the expected type.");
Assert.IsTrue(ex.Message.Contains(expectedExceptionMessage), "Exception raised did not contain expected message. Expected=\"" + expectedExceptionMessage + "\", got \"" + ex.Message + "\"");
}
}
/// <summary>
/// Asserts that calling a method does not throw an exception.
/// </summary>
/// <remarks>
/// This is typically only used in conjunction with <see cref="AssertThrowsException"/>. (e.g. once you have tested that an ExceptionThrower
/// method throws an exception then your test may fix the cause of the exception and then call this to make sure it is now fixed).
/// </remarks>
/// <param name="exceptionThrowingFunc">Delegate that calls the method to be tested.</param>
protected void AssertDoesNotThrowException(ExceptionThrower exceptionThrowingFunc)
{
try
{
exceptionThrowingFunc();
}
catch (NUnit.Framework.AssertionException)
{
// Ignore and rethrow any NUnit exception
throw;
}
catch (Exception ex)
{
Assert.Fail("Call raised an unexpected exception: " + ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)