问题
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.
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.
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.
How do I implement the code correctly?
Update:
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) 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) 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.
回答1:
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.
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.
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.
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".
来源:https://stackoverflow.com/questions/8089025/how-do-i-implement-assemblyinitialize-assemblycleanup-in-my-codeduitest-in-msvs