简体   繁体   English

Visual Studio 2010中的单元测试

[英]Unit Tests in Visual Studio 2010

i am trying to create a Unit test for a WinForm in a Visual Studio 2010 project. 我试图在Visual Studio 2010项目中为WinForm创建一个单元测试。 I add a new "Coded UI Test" to my project, open up the code file, then right click and select "Generate Code for Coded UI Test" -> "Use Coded UI Test builder". 我将一个新的“编码UI测试”添加到我的项目中,打开代码文件,然后右键单击并选择“为编码UI测试生成代码”->“使用编码UI测试构建器”。 I then start my application up, select "Record" on the UI Map control. 然后,我启动我的应用程序,在UI Map控件上选择“记录”。 I run my tests (in this case simply select a textbox, type in a random value, them click a button). 我运行测试(在这种情况下,只需选择一个文本框,输入一个随机值,然后单击一个按钮即可)。 I then select "Generate Code" from the UI Map control which generates the code which the test will use. 然后,我从UI Map控件中选择“ Generate Code”(生成代码),该控件生成测试将使用的代码。 When running this test, i get the error: 运行此测试时,出现错误:

Test method HelloWorldTest.CodedUITest1.CodedUITestMethod1 threw exception: Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: The playback failed to find the control with the given search properties. 测试方法HelloWorldTest.CodedUITest1.CodedUITestMethod1引发异常:Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException:回放未能找到具有给定搜索属性的控件。 Additional Details: TechnologyName: 'MSAA' ControlType: 'Window' Name: 'Form1' ClassName: 'WindowsForms10.Window' ---> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component. 其他详细信息:TechnologyName:'MSAA'ControlType:'Window'名称:'Form1'ClassName:'WindowsForms10.Window'---> System.Runtime.InteropServices.COMException:错误HRESULT E_FAIL已从对COM组件的调用返回。

Does anyone know where i am going wrong? 有谁知道我要去哪里错了?

Thanks 谢谢

I believe your problem is with the window title. 我相信您的问题出在窗口标题上。 You see that Visual Studio will give a window title to each new window generated in your application. 您会看到Visual Studio将为应用程序中生成的每个新窗口赋予一个窗口标题。 When you recorded your test, your window's title was "Form1". 记录测试时,窗口的标题为“ Form1”。 It says so in your error message: 它在您的错误消息中这样说:

ControlType: 'Window' Name: 'Form1' ControlType:“窗口”名称:“ Form1”

So what you need to determine is if your application renames the window each time a new window is opened (most likely it does). 因此,您需要确定的是您的应用程序是否在每次打开新窗口时都重命名窗口(很可能是这样)。 For instance, if you open a second window, it might be called "Form2". 例如,如果您打开第二个窗口,则它可能被称为“ Form2”。

For the coded ui test to work properly, it needs to know where to look for certain controls to perform its actions on. 为了使编码后的ui测试正常工作,它需要知道在哪里寻找某些控件以对其执行操作。 So, it will need to know the window title to be able to "select a textbox, type in a random value, them click a button." 因此,它将需要知道窗口标题才能“选择文本框,输入随机值,然后单击按钮”。

The only way I have found to fix this problem is to set the window title manually if i know it has changed, or will change. 我发现要解决此问题的唯一方法是,如果我知道它已更改或将更改,则手动设置窗口标题。

this.UIMap.UIHighCapitalFranchiseWindow.UIHighCapitalFranchiseDocument.UICtl00ContentPlaceHolComboBox.WindowTitles.Clear();
this.UIMap.UIHighCapitalFranchiseWindow.UIHighCapitalFranchiseDocument.UICtl00ContentPlaceHolComboBox.WindowTitles.Add("Franchises for Sale");

As you can see, I am just clearing all the values in the WindowTitles property of the control, then adding the correct title, the one I know it should be. 如您所见,我只是清除控件的WindowTitles属性中的所有值,然后添加正确的标题,即我所知道的标题。 In the above example it is organized as follows: 在上面的示例中,其组织方式如下:

this.(name of your uimap file).(name of your window).(name of the document in the window).(name of the certain control (combo box, radio button, text box etc)).WindowTitles.(clear or add)(); 这。(uimap文件的名称)。(窗口的名称)。(窗口中的文档的名称)。(某些控件的名称(组合框,单选按钮,文本框等))。WindowTitles(清除)或添加)();

This code can go right in the coded ui test file, or if you want to use the partial class UIMap.cs (the one that is not auto generated, UIMap.Designer.cs is the auto generated one) to make changes to the UIMap. 此代码可以直接在已编码的ui测试文件中,或者如果您要使用部分类UIMap.cs(不是自动生成的部分,UIMap.Designer.cs是自动生成的部分)来更改UIMap 。 Just remember to remove the ".UIMap" if you choose the latter. 只要记住选择“ .UIMap”,就记得删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM