Dav*_*vid 4 c# wpf nunit unit-testing
我有一个用C#(.NET 4.0)编写的复杂WPF项目,我为(NUnit)编写了几个测试.这些测试存在于不同的类中,只要我为每个类单独运行测试,一切都很好.但是,一旦我尝试同时运行所有类的所有测试,第一个类的测试就会成功,但是一旦testrunner(Resharper或nunit-console)开始测试剩余的类,所有这些类都会失败并显示以下堆栈跟踪.
System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it
at System.Windows.Threading.Dispatcher.VerifyAccess()
at System.Windows.Media.Imaging.BitmapDecoder.ToString()
at System.Windows.Media.Imaging.BitmapFrameDecode.ConvertToString(String format, IFormatProvider provider)
at System.Windows.Media.ImageSource.ToString()
at MUSTANG.ShowCase.ResourceLibrary.ResourceDictionaryManager.GetUriString(String pKey) in c:\Daten\Jenkins-ci\jobs\MUSTANG-Showcase-Release-VS2010\workspace\MUSTANG-Showcase\MUSTANG.ShowCase.ResourceLibrary\ResourceDictionaryManager.cs:Zeile 49.
相应的代码如下:
public object GetValue(string pKey)
{
    if (mDictionary.Contains(pKey))
    {
        return mDictionary[pKey];
    }
    return null;
}
public String GetUriString(string pKey)
{
    object result = GetValue(pKey);
    if (null == result)
    {
        Log.Warn(string.Format(@"Ressource '{0}' nicht gefunden!", pKey));
        return "";
    }
    return result.ToString();
}
当资源是图像时,GetUriString中的最后一行发生异常.Nunit似乎使用不同的线程来运行不同的测试类 - 它们仍按顺序运行.有没有办法解决这个问题,例如通过告诉NUnit或testrunners使用单个线程,在每个测试类运行后完全退出或类似?
编辑1:到目前为止我尝试过的内容:
[RequiresSTA]属性装饰测试问题似乎是NUnit为包含测试方法的每个类使用不同的线程,所以我要么找到一种方法
要么
new Application();从堆栈跟踪,你发布它似乎你有一个'线程关联'问题 - 即你试图更新一个不同于它创建的线程上的UI元素.BitmapDecoder派生自DispatcherObject,即它需要在单个线程上运行.在您的测试运行中,它似乎是在一个线程上创建的,然后方法调用(ToString)来自不同的线程.
MUSTANG.ShowCase.ResourceLibrary跨测试实例?如何为每个测试创建一个新实例,即隔离测试?更新:我想我已经把它钉了下来.
.
[assembly: RequiresThread(ApartmentState.STA)]