How do I sense if my unit test is a member of an o

2019-07-17 13:04发布

Environment:

I have a program - named CSIS - which I need to run a lot of automated tests on in Visual Studio 2010 using C#. I have a series of functions which need to be run in many different orders but which all start and end at the same 'home screen' of CSIS. The tests will either be run on their own as a single CodedUITest (.cs filetype) or as an ordered test (.orderedtest filetype).

Goal:

The goal is to open to the CSIS homepage once no matter which of the unit tests is run first and then, after all CodedUITests are finished, no matter which unit test is last, the automated test will close CSIS. I don't want to create a separate unit test to open CSIS to the homepage and another to close CSIS as this is very inconvenient for testers to use.

Current Solution Development:

UPDATE: The new big question is how do I get '[ClassInitialize]' to work?

Additional Thoughts:

UPDATE: I now just need ClassInitialize to execute code at the beginning and ClassCleanUp to execute code at the end of a test set.

If you would like the actual code let me know.

Research Update:

Because of Izcd's answer I was able to more accurately research the answer to my own question. I've found an answer online to my problem.

Unfortunately, I don't understand how to implement it in my code. I pasted the code as shown below in the 'Code' section of this question and the test runs fine except that it executes the OpenWindow() and CloseWindow() functions after each test instead of around the whole test set. So ultimately the code does nothing new. How do I fix this?

static private UIMap sharedTest = new UIMap();

[ClassInitialize] 
static public void ClassInit(TestContext context) 
{ 
    Playback.Initialize(); 
    try 
    { 
        sharedTest.OpenCustomerKeeper(); 

    } 
    finally 
    { 
        Playback.Cleanup(); 
    } 

}

=====================================================================================

Code

namespace CSIS_TEST { //a ton of 'using' statements are here

public partial class UIMap
{
    #region Class Initializization and Cleanup
    static private UIMap sharedTest = new UIMap();

    [ClassInitialize]
    static public void ClassInit(TestContext context)
    {
        Playback.Initialize();
        try
        {
            sharedTest.OpenWindow();
        }
        finally
        {
            Playback.Cleanup();
        }
    }

    [ClassCleanup]
    static public void ClassCleanup()
    {
        Playback.Initialize();
        try
        {

            sharedTest.CloseWindow();
        }
        finally
        {
            Playback.Cleanup();
        }
    }
    #endregion

2条回答
啃猪蹄的小仙女
2楼-- · 2019-07-17 13:27

Microsoft's unit testing framework includes ClassInitialise and ClassCleanUp attributes which can be used to indicate methods that execute functionality before and after a test run. ( http://msdn.microsoft.com/en-us/library/ms182517.aspx )

Rather than try and make the unit tests aware of their position, I would suggest it might be better to embed the opening and closing logic of the home screen within the aforementioned ClassInitialise and ClassCleanUp marked methods.

查看更多
倾城 Initia
3楼-- · 2019-07-17 13:29

I figured out the answer after a very long process of asking questions on StackOverflow, Googling, and just screwing around with the code.

The answer is to use AssemblyInitialize and AssemblyCleanup and to write the code for them inside the DatabaseSetup.cs file which should be auto-generated in your project. You should find that there already is a AssemblyInitialize function in here but it is very basic and there is no AssemblyCleanup after it. All you need to do is create a static copy of your UIMap and use it inside the AssemblyInitialize to run your OpenWindow() code.

Copy the format of the AssemblyInitialize function to create an AssemblyCleanup function and add your CloseWindow() function.

Make sure your Open/CloseWindow functions only contains basic code (such as Process.Start/Kill) as any complex variables such as forms have been cleaned up already and won't work.

Here is the code in my DatabaseSetup.cs:

using System.Data;
using System.Data.Common;
using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Data.Schema.UnitTesting;
using System.Windows.Input;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse;
using MouseButtons = System.Windows.Forms.MouseButtons;

namespace CSIS_TEST
{
    [TestClass()]
    public class DatabaseSetup
    {
        static private UIMap uIMap = new UIMap();
        static int count = 0;

        [AssemblyInitialize()]
        public static void InitializeAssembly(TestContext ctx)
        {
            DatabaseTestClass.TestService.DeployDatabaseProject();
            DatabaseTestClass.TestService.GenerateData();

            if(count < 1)
                uIMap.OpenWindow();
            count++;
        }

        [AssemblyCleanup()]
        public static void InitializeAssembly()
        {            
            uIMap.CloseWindow();
        }
    }
}
查看更多
登录 后发表回答