MyTestInitialize() and MyTestCleanup()

2019-08-12 13:03发布

问题:

  [TestInitialize()]
    public void MyTestInitialize()
    {

        Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.Disabled;
        Playback.PlaybackSettings.ShouldSearchFailFast = false;
        Playback.PlaybackSettings.DelayBetweenActions = 300;
        Playback.PlaybackSettings.SearchTimeout = 30000;            
        Playback.PlaybackSettings.SearchInMinimizedWindows = false;            
    }


 [TestCleanup()]
    public void MyTestCleanup()
    {         
        Logger.CreateResultFile(ResultsLog, TestCaseInfo);
    }

Is there a way that everytime i create a new codedUI test, the MyTestInitialize() and MyTestCleanup() should be created with above lines in it instead of blank ones?

回答1:

Creating a base class and let all your other test classes inherit from it. like this:

 [CodedUITest]
public class BaseTestClass
{
    [TestInitialize()]
    public void MyTestInitialize()
    {

        Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.Disabled;
        Playback.PlaybackSettings.ShouldSearchFailFast = false;
        Playback.PlaybackSettings.DelayBetweenActions = 300;
        Playback.PlaybackSettings.SearchTimeout = 30000;
        Playback.PlaybackSettings.SearchInMinimizedWindows = false;
    }

    [TestCleanup()]
    public void MyTestCleanup()
    {
        Console.Write("Do CleanUp");

    }
}

 [CodedUITest]
public class derivedTestClass : BaseTestClass
{
    [TestMethod]
    public void Tests()
    {

        Console.Write("Test");
    }

}

when you'll invoke Tests() the init and cleanup methods will be called