1,对普通类(非WPF UI组件)进行测试:
这和在.Net2.0中使用NUnit进行测试时一样,不会出现任何问题,参考下面的代码:


[TestFixture] public class ClassTest { [Test] public void TestRun() { ClassLibrary1.Class1 obj = new ClassLibrary1.Class1(); double expected = 9; double result = obj.GetSomeValue(3); Assert.AreEqual(expected, result); } }
2,对WPF UI组件进行测试
使用NUnit对WPF UI组件(比如MyWindow,MyUserControl)进行测试的时候,NUnit会报如下异常:“The calling thread must be STA, because many UI components require this”。
下面是错误的测试代码:


[TestFixture] public class ClassTest { [Test] public void TestRun() { WindowsApplication1.Window1 obj = new WindowsApplication1.Window1(); double expected = 9; double result = obj.GetSomeValue(3); Assert.AreEqual(expected, result); } }

为了让调用线程为STA,我们可以编写一个辅助类CrossThreadTestRunner:
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/