}
catch (Exception e)
{
lastException = e;
}
});
thread.SetApartmentState(apartmentState);
thread.Start();
thread.Join();
if (ExceptionWasThrown())
ThrowExceptionPreservingStack(lastException);
}
private bool ExceptionWasThrown()
{
return lastException != null;
}
[ReflectionPermission(SecurityAction.Demand)]
private static void ThrowExceptionPreservingStack(Exception exception)
{
FieldInfo remoteStackTraceString = typeof(Exception).GetField(
"_remoteStackTraceString",
BindingFlags.Instance | BindingFlags.NonPublic);
remoteStackTraceString.SetValue(exception, exception.StackTrace + Environment.NewLine);
throw exception;
}
}
}
并編寫正確的測試代碼: 軟件測試
以下是引用片段:
[TestFixture] public class ClassTest { [Test] public void TestRun() { CrossThreadTestRunner runner = new CrossThreadTestRunner(); runner.RunInSTA( delegate { Console.WriteLine(Thread.CurrentThread.GetApartmentState()); WindowsApplication1.Window1 obj = new WindowsApplication1.Window1(); double expected = 9; double result = obj.GetSomeValue(3); Assert.AreEqual(expected, result); }); } }
另外,使用NUnit時,您需要添加對nunit.framework.dll的引用,并對測試類添加[TestFixture]屬性標記以及對測試方法添加[Test]屬性標記,然后將生成的程序集用nunit.exe打開就可以了,關于NUnit的具體用法您可以參考其官方文檔。
文章來源于領測軟件測試網 http://www.kjueaiud.com/