如何在 MSVS 2010 的 CodedUITest 中实现 AssemblyInitialize/AssemblyCleanup? [英] How do I implement AssemblyInitialize/AssemblyCleanup in my CodedUITest in MSVS 2010?

查看:18
本文介绍了如何在 MSVS 2010 的 CodedUITest 中实现 AssemblyInitialize/AssemblyCleanup?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Microsoft Visual Studio 2010 中实现 AssemblyInitialize/AssemblyCleanup 属性,以达到所述的确切目的 在这里.该链接甚至描述了我实现代码所需遵循的过程.

I am trying to implement AssemblyInitialize/AssemblyCleanup attributes in my Microsoft Visual Studio 2010 for the exact purpose as stated here. That link even describes the process which I need to follow to implement the code.

该目的的快速总结是创建一个初始代码块,无论我在解决方案中运行哪个 codedUITest,它将在任何测试之前运行,然后创建一个将在最后一个 codedUITest 完成后运行的代码块.示例:我需要打开一个特定的应用程序,然后运行一系列 codedUITests,它们都从该应用程序开始并以任意顺序执行,然后在一切完成后关闭应用程序;这比为每个 codedUITest 打开/关闭应用程序更有效.

A quick summary of that purpose is to create an initial block of code which will run right before any test no matter which of the codedUITests I run in the solution and then a block of code which will run after the last codedUITest is completed. Example: I need to open up a specific application, then run a series of codedUITests which all start at that application and which are executed in any order, then close the application after everything is finished; this is more efficient than opening/closing the application for each codedUITest.

我不明白我需要将代码放置在该页面底部的位置(如下所示).我将所有代码都放在我的公共部分类 UIMap"下,并且代码运行,除了它在每个 CodedUITest 之前/之后运行OpenApplication"和CloseApplication"命令,而不是夹在整个 CodedUITest 组中.

What I don't understand is where I need to place the code laid out at the bottom of that page (also shown below). I stuck all that code right under my 'public partial class UIMap' and the code runs except it runs the 'OpenApplication' and 'CloseApplication' commands before/after each CodedUITest instead of sandwiching the entire group of CodedUITests.

如何正确实现代码?

更新:

我昨晚发现了 AssemblyI/C,我花了 3 个小时尝试找出将代码放在哪里以便它工作.如果我把AssemblyInitialize 在特定测试方法的开头然后:

I discovered AssemblyI/C last night and I spent 3 hours trying to figure out where to put the code so it works. If I put the AssemblyInitialize at the beginning of a specific test method then:

1) 它仍然无法运行 - 它给了我一些错误说UIMap.OpenWindow() 和 UIMap.CloseWindow() 方法需要是静态的我不知道如何使它们成为静态的.

1) It still wouldn't run - it was giving me some error saying that UIMap.OpenWindow() and UIMap.CloseWindow() methods need to be static and I couldn't figure out how to make them static.

2) 不是具有 AssemblyI/C 的特定 [TestMethod]需要在测试集中吗?在我的情况下,我有十几个CodedUITests 需要单独运行或以更大的方式运行组,我需要让 AssemblyI/C 打开/关闭我所在的窗口测试.

2) Wouldn't the specific [TestMethod] which has the AssemblyI/C on it need to be in the test set? In my situation I have a dozen CodedUITests which need to run either individually or in a larger group and I need to get the AssemblyI/C to Open/Close the window I am testing.

推荐答案

您将方法添加到了错误的类中.通过将 then 放入 UIMap 部分类,您是在告诉运行时在每次创建新的 UIMap 实例时运行这些方法,这听起来就像您每次都在做测试.

You've added the methods to the wrong class. By putting then into the UIMap partial class, you are telling the runtime to run those methods every time you create a new UIMap instance, which it sounds like you're doing every test.

ClassInitialize/ClassCleanup 方法的重点是将它们添加到包含您的测试方法的类中.您应该至少有一个用 TestClass 属性修饰的类,该类至少有一个用 TestMethod 属性修饰的方法.这是需要应用 ClassInitializeClassCleanup 属性的类.这些方法将为您项目中的每个单独的 TestClass 运行一次.

The point of the ClassInitialize/ClassCleanup methods is to add them to the class with your test methods in it. You should have at least one class decorated with the TestClass attribute, which has at least one method decorated with a TestMethod attribute. This is the class that needs the ClassInitialize and ClassCleanup attributes applied to it. Those methods will run one time for each separate TestClass you have in your project.

您也可以改用 AssemblyInitializeAssemblyCleanup 属性.在任何给定的程序集中只能有这些方法之一,它们将分别在任何类中的任何测试方法之前和之后首先运行和最后运行.

You could also use the AssemblyInitialize and AssemblyCleanup attributes instead. There can only be one of these methods in any given assembly, and they will run first and last, respectively, before and after any test methods in any classes.

更新:AssemblyInitialize/Cleanup 需要在具有 TestClass 属性的类中,但不管是哪一个.具有每个属性的单个方法将在程序集运行中的任何测试之前或之后运行.但是,它不能是一种测试方法;它必须是静态方法,不能算作测试".

UPDATE: AssemblyInitialize/Cleanup need to be in a class that has the TestClass attribute, but it doesn't matter which one. The single method with each attribute will get run before or after any tests in the assembly run. It can't be a test method, though; it has to be a static method and will not count as a "test".

这篇关于如何在 MSVS 2010 的 CodedUITest 中实现 AssemblyInitialize/AssemblyCleanup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆