Question

today we realized that some tests where throwing exceptions but all the tests where 'pass'. In fact the exception text was shown in green.

Does this make sense?

Is there a way to make the tests fail if an exception is unhandled (I know that most of the time that happens)?

The screenshot:

enter image description here

The tested method:

[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();
}
Était-ce utile?

La solution

From the call stack you posted, I see that the exception occurs in a seperate thread.

It seems that only exceptions thrown in the calling thread of the test method cause the test to fail. Consider this example:

[TestMethod]
public void ThreadExceptionTest()
{
    new Thread(() => { throw new Exception("Error in thread"); }).Start();
}

Also see this question: how-to-handle-exceptions-raised-in-other-threads-when-unit-testing

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top