自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WPF單元測(cè)試方法總結(jié)

開發(fā) 開發(fā)工具
WPF單元測(cè)試的進(jìn)行主要可以分為對(duì)普通類(非WPF UI組件)進(jìn)行測(cè)試;對(duì)WPF UI組件進(jìn)行測(cè)試等。我們將針對(duì)這兩種測(cè)試做一個(gè)詳細(xì)的介紹。

WPF單元測(cè)試的創(chuàng)建需要有一定編程經(jīng)驗(yàn)的開發(fā)人員,才能熟練的操作。那么,在這篇文章中我們將針對(duì)WPF單元測(cè)試的創(chuàng)建過程做一個(gè)簡(jiǎn)單的介紹。 #t#

1,對(duì)普通類(非WPF UI組件)進(jìn)行測(cè)試:

這和在.Net2.0中使用NUnit進(jìn)行測(cè)試時(shí)一樣,不會(huì)出現(xiàn)任何問題,參考下面的代碼:

  1. [TestFixture]   
  2. public class ClassTest {   
  3. [Test] public void TestRun() {   
  4. ClassLibrary1.Class1 obj = 
    new ClassLibrary1.Class1();  
  5. double expected = 9;   
  6. double result = obj.GetSomeValue(3);   
  7. Assert.AreEqual(expected, result);   
  8. }   

2,對(duì)WPF UI組件進(jìn)行測(cè)試

使用NUnit對(duì)WPF UI組件(比如MyWindow,MyUserControl)進(jìn)行測(cè)試的時(shí)候,NUnit會(huì)報(bào)如下異常:“The calling thread must be STA, because many UI components require this”。

下面是錯(cuò)誤的WPF單元測(cè)試代碼:

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

為了讓調(diào)用線程為STA,我們可以編寫一個(gè)輔助類CrossThreadTestRunner:

 

  1. using System; using System.
    Collections.Generic;   
  2. using System.Text;   
  3. using System.Threading;   
  4. using System.Security.Permissions;   
  5. using System.Reflection;   
  6. namespace TestUnit {   
  7. public class CrossThreadTestRunner {   
  8. private Exception lastException;   
  9. public void RunInMTA(ThreadStart 
    userDelegate) {   
  10. Run(userDelegate, ApartmentState.MTA);   
  11. }   
  12. public void RunInSTA(ThreadStart 
    userDelegate) {   
  13. Run(userDelegate, ApartmentState.STA);   
  14. }   
  15. private void Run(ThreadStart 
    userDelegate, 
    ApartmentState apartmentState) {   
  16. lastException = null;   
  17. Thread thread = new Thread( delegate() {   
  18. try { userDelegate.Invoke();   
  19. }   
  20. catch (Exception e) { lastException = e;   
  21. }   
  22. });   
  23. thread.SetApartmentState(apartmentState);   
  24. thread.Start();   
  25. thread.Join();   
  26. if (ExceptionWasThrown())   
  27. ThrowExceptionPreservingStack
    (lastException);   
  28. }   
  29. private bool ExceptionWasThrown() {   
  30. return lastException != null;   
  31. }   
  32. [ReflectionPermission(Security
    Action.Demand)]   
  33. private static void ThrowException
    PreservingStack(Exception exception) {   
  34. FieldInfo remoteStackTraceString = 
    typeof(Exception).GetField( "_remoteStack
    TraceString", BindingFlags.Instance | 
    BindingFlags.NonPublic);  
  35. remoteStackTraceString.SetValue(exception,
     exception.StackTrace + Environment.NewLine);   
  36. throw exception;   
  37. }   
  38. }   

并編寫正確的WPF單元測(cè)試代碼:

  1. [TestFixture] public class ClassTest {   
  2. [Test] public void TestRun() {   
  3. CrossThreadTestRunner runner = 
    new CrossThreadTestRunner();   
  4. runner.RunInSTA( delegate {   
  5. Console.WriteLine(Thread.CurrentThread.
    GetApartmentState());   
  6. WindowsApplication1.Window1 obj = 
    new WindowsApplication1.Window1(); 
    double 
    expected = 9;  
  7. double result = obj.GetSomeValue(3);   
  8. Assert.AreEqual(expected, result);   
  9. });   
  10. }   

另外,使用NUnit時(shí),您需要添加對(duì)nunit.framework.dll的引用,并對(duì)WPF單元測(cè)試類添加[TestFixture]屬性標(biāo)記以及對(duì)測(cè)試方法添加[Test]屬性標(biāo)記,然后將生成的程序集用nunit.exe打開就可以了。

責(zé)任編輯:曹凱 來源: IT168
相關(guān)推薦

2017-01-14 23:42:49

單元測(cè)試框架軟件測(cè)試

2011-05-16 16:52:09

單元測(cè)試徹底測(cè)試

2009-09-01 10:20:06

protected方法單元測(cè)試

2023-07-26 08:58:45

Golang單元測(cè)試

2011-06-20 17:25:02

單元測(cè)試

2024-10-16 16:09:32

2017-01-14 23:26:17

單元測(cè)試JUnit測(cè)試

2017-01-16 12:12:29

單元測(cè)試JUnit

2011-11-18 15:18:41

Junit單元測(cè)試Java

2020-08-18 08:10:02

單元測(cè)試Java

2009-06-02 14:24:32

Hibernate單元測(cè)試HSQLDB

2017-03-23 16:02:10

Mock技術(shù)單元測(cè)試

2021-05-05 11:38:40

TestNGPowerMock單元測(cè)試

2022-09-15 10:02:58

測(cè)試軟件

2011-07-04 18:16:42

單元測(cè)試

2020-05-07 17:30:49

開發(fā)iOS技術(shù)

2011-04-18 13:20:40

單元測(cè)試軟件測(cè)試

2013-06-04 09:49:04

Spring單元測(cè)試軟件測(cè)試

2017-02-23 15:59:53

測(cè)試MockSetup

2009-09-25 10:33:25

Hibernate單元
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)