MSTest Test Context Exception Handling

前端 未结 3 1131
孤街浪徒
孤街浪徒 2021-01-02 15:36

Is there a way that I can get to the exception that was handled by the MSTest framework using the TestContext or some other method on a base test class?

If an unhand

3条回答
  •  鱼传尺愫
    2021-01-02 15:50

    I've been running into the same issue, there doesn't seem to be support for this. You can't even use ApplicationDomain's unhandled exception hook since if MSTEST didn't catch exceptions before they bubbled up that far, it would itself crash.

    Possible workaround:

        private delegate void TestImplDelegate();
    
        private void RunTestWithExceptionLogging(TestImplDelegate testImpl)
        {
            try
            {
                testImpl();
            }
            catch (Exception e)
            {
                string message = e.Message; // don't warn about unused variables
    
                // do logging here
            }
        }
    
        [TestMethod]
        public void test1()
        {
            RunTestWithExceptionLogging(test1Impl);
        }
        private void test1Impl()
        {
            // test code goes here
    
            throw new Exception("This should get logged by the test wrapper.");
        }
    
        [TestMethod]
        public void test2()
        {
            RunTestWithExceptionLogging(test2Impl);
        }
        private void test2Impl()
        {
            // test code goes here
    
            throw new Exception("This should get logged by the test wrapper.");
        }
    

    It's certainly not optimal, but at least this way you don't have multiple copies of the exception handler code.

    I would recommend filing a feature request for this at http://connect.microsoft.com/ (or see if someone else has already requested it, and add your vote.)

提交回复
热议问题