---
title: "使用Windows Mobile Test Framework进行Windows Mobile程序的自动化测试 - (2) 小试牛刀"
author: "mmdev"
date: "2007-07-26T01:16:00Z"
tags: []
slug: "windows-mobile-test-framework-windows-mobile-2"
draft: false
lastmod: "2007-07-26T01:16:00Z"
id: 990547
url: "https://mmdev.iteye.com/blog/990547"
---

**前言：**

在[上一个小节](http://www.cnblogs.com/twodays/archive/2007/05/30/WMTF1.html)中给大家简单介绍了WM6 SDK中带的Windows Mobile Test Framework。各位朋友可能对Windows Mobile Test Framework有了一个大概的印象，但是对于怎么使用它还是有些疑惑。由于我的工作比较繁忙，一直没有时间更新这个系列，对此很是抱歉。今天，我们就来用这个Windows Mobile Test Framework来做一个小小的测试，看看它是怎么运行的。让大家对它有个感性的认识。

**一、准备工作**

1\. 打开Visual Studio 2005的菜单中的 工具-〉设备仿真器管理器。然后打开一个Windows Mobile 6的Professional模拟器。然后将该模拟器通过ActiveSync（如果是Vista则用WMDC）与计算机进行连接。

2\. 将Windows Mobile Test Framework.zip解开到某一个目录中。双击运行其中的**CommandWindows**快捷方式。则会弹出来一个命令提示符窗口。您在该窗口中输入“Set”然后回车，会发现“Path”参数中增加了Windows Mobile Test Framework下得Bin目录的路径。以及增加了\_WMTFROOT这个变量。

3\. 在该命令提示符窗口中输入“**SetupDevice**”然后回车。则会自动向设备上Copy和安装一些必要的文件。（SetupDevice是一个批处理文件，您也可以在Bin目录下找到）

4.在设备模拟器上，你会发现在“程序”中安装了一个快捷方式“CalView”。在设备的根目录下有一个test目录，里面是刚才copy进来的那些文件。其中包含CalView。您可以打开CalView，它是一个用来查看pOutlook中的日历信息的小程序。

5\. 通过ActiveSync或者WMDC将“**%ProgramFiles%\\Windows Mobile 6 SDK\\Tools\\Security\\SDK Development Certificates\\Certs.cab**”拷贝到模拟器中，然后安装它。

6\. 手动在模拟器中的日历中建立一个条目以备后面的步骤使用。

**二、了解测试工程**

1\. 打开**\_WMTFROOT**下的**CalViewSample**这个目录，您会发现其中有三个子目录，分别是“AbstractionLayer”，“AreaLibrary”和“Tests”。它们就是在上一小节中介绍过的“UIAL”、“AreaLibrary”和“Tests”层。至于DATK和MTK，呵呵，他们都是微软已经提供了的。您可以在**\_WMTFROOT**下的**DeviceFiles**目录中找到（分别是Microsoft.WindowsCE.DATK.dll和Microsoft.MobileDevices.MobilityToolKit.dll）。

2\. 由于Tests层的代码要引用Area Library层的代码；而Area Library层的代码要引用UIAL层的代码。所以，我们先用VS2005打开AbstractionLayer下的项目。CalView的AbstractionLayer分别针对PPC和Smartphone做了两个版本。我们先打开PocketPC目录下的解决方案。你会发现该解决方案中有两个项目，一个是“**Microsoft.MobileDevices.AbstractionLayer.CalView.PocketPC**”，一个是“**TuxNet**”。其中前者就是CalView的PPC版的程序的UIAL层代码，后者其实是一个空的项目，它的目的只是为了将它里面包含的那些文件部署到设备上去。打开UIAL层项目的属性，“**设备**”选项页。复选中“Authenticode签名”中的“用此证书对项目输出进行签名”。然后点击“选择证书...”，选中“**TEST USE ONLY - Sample Privileged Root for Windows Mobile SDK**”,这是一个测试用的特权证书。如果这里没有显示这个证书，那么点击“管理证书”，然后导入“**%ProgramFiles%\\Windows Mobile 6 SDK\\Tools\\Security\\SDK Development Certificates\\SamplePrivDeveloper.pfx**”。然后再次打开这个选择证书页面就可以看到。它的目的就是给您的UIAL层代码进行测试签名，这样便于访问一些特权函数，避免运行过程中不断的弹出那些提示框，然后再编译此解决方案。接着关闭该解决方案，同样处理以及编译Smartphone版的UIAL层解决方案和Area Library层的解决方案。

3\. 打开Tests的解决方案同样处理。然后设置Tests层项目的属性中“**调试**”选项页中，“**启动操作**”为“**启动外部程序**”，并设置其值为“\\test\\TuxNet.exe”。

**三、运行测试程序**

1.按下F5运行解决方案，注意请选择您已经启动好的那个模拟器。

2\. 您会发现模拟器中系统自动打开运行了CalView这个程序。然后调用了其中的“copy”菜单项来复制了一个日历项目。最后退出程序。

3\. 从模拟器中打开\\test目录，您会发现一个**results.log**文件，将它从模拟器中copy到PC端，用记事本打开，它就是刚才的运行记录，内容比较长，我摘录其中几个部分如下：

................................................................

&lt;TESTCASE ID="0"&gt;  
\*\** Test Name: Microsoft.MobileDevices.Tests.CalView.CalViewTests.CalViewBVT  
\*\** Test ID: 0  
BVT: BVT  
Repro: -assembly Microsoft.MobileDevices.Tests.CalView.dll -suites CalViewTests -tests CalViewBVT  
Begin Step: CalViewBVT  
\[1] LaunchApplication(CalView, finder): Attempting to launch from start menu  
\[1] LaunchFromStartMenu(CalView, WindowFinder, False): Opening start menu  
\[1] OpenStartMenu(): Clicking on start menu to open it  
\[1] ClickStartMenu(): Clicking on start menu at (2, 2)  
\[1] ClickStartMenu(): Start menu successfully opened  
\[1] LaunchFromStartMenu(CalView, WindowFinder, False): Getting list of start menu items  
\[1] LaunchFromStartMenu(CalView, WindowFinder, False): Item found - clicking on index 9  
\[1] countBefore = 1  
\[1] countAfter = 2  
\[1] Verification = Pass: Count after is one greater  
\[1] ClickOK(): Clicking on task bar at (230, 10)  
End Step : CalViewBVT  
Verification = Pass: CalViewBVT  
\*\** Result: Passed  
&lt;/TESTCASE&gt;

................................................................

\*\** SUITE SUMMARY  
\*\**  
\*\** Results  
\*\** Passed: 1  
\*\** Skipped: 0  
\*\** Failed: 0  
\*\** Aborted: 0  
\*\** -------- ---------  
\*\** Total: 1  
&lt;/TESTGROUP&gt;  
................................................................

4\. 上面Log中的我标记出来的第一部分即是我们刚才跑的一条test case的运行记录；标记的后一段是该运行的整体情况，这次运行总共1条case，结果为pass。

5\. 我们看到这条case的名称为Microsoft.MobileDevices.Tests.CalView.CalViewTests.CalViewBVT ，那么我们在tests工程的**CalViewTests.cs**中可以找到如下代码,它就是这条case：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///Addadescriptionofthestepsthistestexecutes.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///eg:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///1)OpenContactapp  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///2)CreateaContact  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///3)VerifycontactappeatsinMainlistview  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///4)CloseContactsapp  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)///&lt;returns&gt;Log.LogResult.Passonsuccess&lt;/returns&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\[TestCaseAttribute("BVT",Type=TestType.BVT)]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicLog.LogResultCalViewBVT()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//Opentheapplication  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)CalViewAreaLib.General.LaunchApp();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//Storeandlogthebeginningcount  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intcountBefore=CalViewAreaLib.MainDialog.GetItemCount();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Utils.GlobalLogger.AddComment("countBefore={0}",countBefore.ToString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//Selectandcopyoneoftheitems  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)CalViewAreaLib.MainDialog.SelectItem(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)CalViewAreaLib.MainDialog.CopySelectedItem();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//Storeandlogthecountaftercopy  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intcountAfter=CalViewAreaLib.MainDialog.GetItemCount();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Utils.GlobalLogger.AddComment("countAfter={0}",countAfter.ToString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//LogaresultbasedontheconditionalthatcountAfterisonegreaterthancountBefore  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Utils.GlobalLogResultManager.Results.Add("Countafterisonegreater",(countAfter==(countBefore+1)));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//Closetheapptocleanup  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)CalViewAreaLib.General.CloseApp();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//Resultsmanagerkepttrackoftheresultforyou,returnitssummary  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnUtils.GlobalLogResultManager.Results.Summary;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

从case的步骤我们可以看到/猜测，它先后打开CalView程序，然后选择其中第一个条目，然后复制一个。然后察看是否条目数量增加了一个。最后关闭程序再输出结果。在这里这些代码的含义您可以通过UAIL或者AreaLibrary的代码来查看具体含义，在后面我的Blog中也会陆续进一步介绍。

**四、小结**

从上面的演示我们看到我们可以很方便的通过我们的代码来模拟用户来操作我们的程序并进行验证。不错吧？![smile_teeth](http://spaces.live.com/rte/emoticons/smile_teeth.gif)

好了，就先介绍到这里吧．在下一节中，我们将会介绍一下Tests层代码的结构以及它的运作机制。