Maybe you've found a problem, we can't use vs to create Unit tests for WPF projects (VS2005 not, VS2008 I haven't tried, but it's not said), which is depressing, and here's how to use NUnit to create Unit tests on WPF projects and solve the challenges (but using NUnit to create unit tests for WPF is not as easy as targeting. Net2.0, and there may be some minor problems.
1. Test the normal class (non-WPF UI components):
This and in. When you use NUnit for testing in Net2.0, there is no problem, refer to the following code:
The following is a reference fragment:
[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. Testing the WPF UI components
When you test a WPF UI component (such as Mywindow,myusercontrol) using NUnit, the NUnit reports the following exception: "The calling thread must be STA, because many UI component s require this ".
The following is the wrong test code:
The following is a reference fragment:[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);
}
}