locked
Unobserved task exception running unit tests RRS feed

  • Question

  • We had a couple of unit test run failures with the following stack trace from "Test Run Errors and Warnings:

    Error	05/07/2012 09:27:16	One of the background threads threw exception: 
    System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.ObjectDisposedException: Cannot write to a closed TextWriter.
       at System.IO.StringWriter.Write(Char[] buffer, Int32 index, Int32 count)
       at Microsoft.VisualStudio.TestTools.TestTypes.Unit.ThreadSafeStringWriter.Write(Char[] buffer, Int32 index, Int32 count)
       at System.IO.TextWriter.WriteLine(String value)
       at System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
       at System.Threading.Tasks.Task.Execute()
       --- End of inner exception stack trace ---
       at System.Threading.Tasks.TaskExceptionHolder.Finalize()
    ---> (Inner Exception #0) System.ObjectDisposedException: Cannot write to a closed TextWriter.
       at System.IO.StringWriter.Write(Char[] buffer, Int32 index, Int32 count)
       at Microsoft.VisualStudio.TestTools.TestTypes.Unit.ThreadSafeStringWriter.Write(Char[] buffer, Int32 index, Int32 count)
       at System.IO.TextWriter.WriteLine(String value)
       at System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
       at System.Threading.Tasks.Task.Execute()<---

    Looks like a bug in unit test framework.

    Monday, May 7, 2012 3:11 PM

All replies

  • Hi Alex,

    Thank you for posting in the MSDN forum.

    Based on your description, it seems that the unit test result is failed. If so, could you right click the unit test result in the Test Results window, then select “View Test Results Details”, did you get any information? Could you share it to us?

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 9, 2012 4:46 AM
  • All tests in the run succeeded, but the run itself failed.
    Wednesday, May 9, 2012 4:50 AM
  • Hi Alex,

    Glad to receive your reply.

    All tests in the run succeeded, but the run itself failed.

    I’m not very sure about this issue, could you share us more information about the “but the run itself failed? Since if all tests’ results are passed, when and where did you get the failed information? Do you mean it generated this issue when you run your app? If so, I’m afraid that it is not the unit test issue, it is related to your app which you want to test.

    If I have misunderstood anything, please make free feel to let me know.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 9, 2012 5:18 AM
  • I do not know how else I can explain.

    1. Run the tests.

    2. In Test Results View: all tests have succeded.

    3. In Test Results View: click "Test run error" link.

    4. A test run error page appears with the content provided in original post.

    Wednesday, May 9, 2012 5:36 AM
  • Hi Alex,

    Would you mind sharing us your project?

    Please attach your Visual Studio project, you can upload it to the sky driver, and then share the download link in your post. Or you could send it to jackzhai at Hotmail dot com. I will try to run it in my computer. Please also attach a screenshot image about the Test Results in your post.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 9, 2012 5:48 AM
  • Hi Jack,

    In our project we have a similar issue. Is there already a feedback available?

    Thanks and best regards,

    Tobias

    Monday, September 3, 2012 8:41 AM
  • It seems like assertion failures in background threads are not always propagated to the test harness which means the test appears to succeed when in fact it should have be considered a failed test.  It may not help in your particular scenario but...

    Thanks to Allen Mathias in this other thread I've found that tests implemented in Visual Studio 2012 can create background threads usingTask.Run along with the async keyword and the await operator as described in Writing async methods for test execution.

    There may be limitations or restrictions that my limited testing has not revealed but the basics seem to work.  But of course you need to be able to use Visual Studio 2012 and the 4.5 .NET Framework.

    Sunday, October 7, 2012 5:43 AM
  • Hi Alex,

    Are you on VS2010? If so you need to handle background threads yourself in the tests. In VS2012 you would be able to utilize the async test method to help with this.

    If you still feel there is an issue here could you share some sample test code here and we can see if we can help further.



    Thank You
    Allen Mathias

    Tuesday, October 9, 2012 5:22 AM