Create a unit test for a WPF Project

Source: Internet
Author: User
Create a unit test for a WPF Project
Zhou yinhui

Maybe you have found a problem. We cannot use vs to create a unit test for the WPF Project (vs2005 is not good, vs2008 I have not tried it, but it is said that it is not good), which is depressing, here we will introduce how to use nunit to create unit tests for the WPF project and solve the problem (however, nunit is not used to create unit tests for the WPF project. as easy as net2.0, some minor problems may occur ).

1. Test common classes (non-wpf ui components:
This is the same as using nunit in. net2.0 for testing. Code : [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. Test the wpf ui component.
When using nunit to test wpf ui components (such as mywindow and myusercontrol), nunit reports the following exception: "The Calling thread must be STA, because component UI components require ".
The following is the wrong test code: [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 );
}
}

To set the call thread to STA, We can compile a helper class crossthreadtestrunner: Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Threading;
Using System. Security. permissions;
Using System. reflection;

Namespace Testunit
{
Public   Class Crossthreadtestrunner
{
Private Exception lastexception;

Public   Void Runinmta (threadstart userdelegate)
{
Run (userdelegate, apartmentstate. MTA );
}

Public   Void Runinsta (threadstart userdelegate)
{
Run (userdelegate, apartmentstate. Sta );
}

Private   Void Run (threadstart userdelegate, apartmentstate)
{
Lastexception =   Null ;

Thread thread =   New Thread (
Delegate ()
{
Try
{
Userdelegate. Invoke ();
}
Catch (Exception E)
{
Lastexception=E;
}
} );
Thread. setapartmentstate (apartmentstate );

Thread. Start ();
Thread. Join ();

If (Exceptionwasthrown ())
Throwexceptionpreservingstack (lastexception );
}

Private   Bool Predictionwasthrown ()
{
ReturnLastexception! = Null;
}

[Reflectionpermission (securityaction. Demand)]
Private   Static   Void Throwexceptionpreservingstack (exception)
{
Fieldinfo remotestacktracestring =   Typeof (Exception). getfield (
" _ Remotestacktracestring " ,
Bindingflags. Instance | Bindingflags. nonpublic );
Remotestacktracestring. setvalue (exception, exception. stacktrace + Environment. newline );
Throw Exception;
}
}
}

And write the correct test code: [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 );
} );

}
}


In addition, when using nunit, you need to add to nunit. framework. DLL reference, add the [testfixture] attribute tag to the test class, add the [test] attribute tag to the test method, and thenProgramYou can use nunit.exe to open the set. For more information about how to use nunit, see its official documentation.

Demo nunit

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.