C# 为什么unittest上未处理的异常不会使用MSTest使其失败?
今天我们意识到有些测试抛出异常,但所有的测试都“通过”。事实上,异常文本显示为绿色 这有意义吗 如果异常未处理,是否有办法使测试失败(我知道大多数情况下都会发生) 截图: 测试方法:C# 为什么unittest上未处理的异常不会使用MSTest使其失败?,c#,visual-studio-2010,unit-testing,exception,mstest,C#,Visual Studio 2010,Unit Testing,Exception,Mstest,今天我们意识到有些测试抛出异常,但所有的测试都“通过”。事实上,异常文本显示为绿色 这有意义吗 如果异常未处理,是否有办法使测试失败(我知道大多数情况下都会发生) 截图: 测试方法: [TestMethod] public void CommunicationCore_CommunicationController_ConnectRequestWellProcessed() { // Arrange IUnityContainer container = new UnityCo
[TestMethod]
public void CommunicationCore_CommunicationController_ConnectRequestWellProcessed()
{
// Arrange
IUnityContainer container = new UnityContainer();
ICommonInitializer initializer = new CommonInitializer(container);
initializer.Initialize(); // register all types
DriveConnection connectionSettings = CreateFakeConnetionSettings(1);
Transaction transaction = null;
ICommunicationController controller = container.Resolve<ICommunicationController>();
object locker = new object();
// Act
controller.Connect(
connectionSettings,
result =>
{
transaction = result;
lock (locker)
{
Monitor.Pulse(locker);
}
});
// asyncronous communication wait for the process of the message
lock (locker)
{
Monitor.Wait(locker, 10000);
}
// Assert
bool connectSuccessfully = (transaction != null) && !transaction.Response.ErrorResult.ErrorDetected;
Assert.IsTrue(connectSuccessfully);
((IDisposable)controller).Dispose();
}
[TestMethod]
public void CommunicationCore_CommunicationController_ConnectRequestWellProcessed()
{
//安排
IUnityContainer容器=新的UnityContainer();
ICommonInitializer initializer=新的CommonInitializer(容器);
initializer.Initialize();//注册所有类型
DriveConnectionSettings=CreateFakeConnectionSettings(1);
事务=空;
ICommunicationController=container.Resolve();
对象锁定器=新对象();
//表演
控制器,连接(
连接设置,
结果=>
{
交易=结果;
锁(储物柜)
{
监视器。脉冲(锁定器);
}
});
//异步通信等待消息处理
锁(储物柜)
{
监视器。等待(储物柜,10000);
}
//断言
bool connectSuccessfully=(transaction!=null)&&!transaction.Response.ErrorResult.ErrorDetected;
Assert.IsTrue(连接成功);
((IDisposable)controller.Dispose();
}
从您发布的调用堆栈中,我看到异常发生在一个单独的线程中
似乎只有在测试方法的调用线程中抛出的异常才会导致测试失败。考虑这个例子:
[TestMethod]
public void ThreadExceptionTest()
{
new Thread(() => { throw new Exception("Error in thread"); }).Start();
}
还可以看到这个问题:您是否启动了新的线程和/或进程?@LieRyan:是的,我启动了。这有关系吗?@SoMoS事实上,它非常重要,因为异常是在该线程中抛出的。的可能副本