To create a unit test for a WPF project

Source: Internet
Author: User
Tags assert

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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.