我怎么感觉如果我的单元测试是一个有序的测试中的一员,如果它是,它是指在顺序测试持何立场? [英] How do I sense if my unit test is a member of an ordered test and, if it is, which position in that ordered test it is at?

查看:198
本文介绍了我怎么感觉如果我的单元测试是一个有序的测试中的一员,如果它是,它是指在顺序测试持何立场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:

我有一个计划 - 名为CSIS - 我需要在Visual Studio 2010中使用C#运行大量的自动化测试。我有一系列的这就需要在许多不同的命令来运行,但所有启动,并在CSIS的相同的主屏幕结束的功能。测试要么是对自己的运行作为一个单一的codedUITest(的.cs文件类型)或有序测试(的.orderedtest文件类型)。

目标:

我们的目标是开到CSIS主页一旦没有单元测试的其上运行,然后再不管,毕竟codedUITests完成,无论哪个单元测试是最后,自动化测试将关闭CSIS。我不想创建一个单独的单元测试打开CSIS到首页,另一个关闭CSIS因为这是非常不方便的测试人员使用。

当前解决方案开发:

更新:的新的大问题是如何得到[ClassInitialize]'工作

其他的思考:

更新:的我现在只需要ClassInitialize在开始和ClassCleanUp执行code。在一个测试集的末尾执行code

如果您想实际code让我知道。

研究更新:

由于Izcd的回答我能回答更准确地研究,以我自己的问题。我已经找到了答案网上我的问题。

不幸的是,我不知道如何实现它在我的code。我粘贴了code作为在这一问题的code部分如下图所示,测试运行正常,除了它每次测试之后,而不是围绕整个测试集执行了OpenWindow()和CloseWindow()函数。所以,最后的code确实不是什么新鲜事。我该如何解决这个问题?

静态专用的UIMap sharedTest =新的UIMap();

  [ClassInitialize]
静态公共无效ClassInit(的TestContext上下文)
{
    Playback.Initialize();
    尝试
    {
        sharedTest.OpenCustomerKeeper();    }
    最后
    {
        Playback.Cleanup();
    }}

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

  code

命名空间CSIS_TEST
{
    //一吨的使用语句此处

 公共部分类的UIMap
{
    #地区级Initializization及清理
    静态专用的UIMap sharedTest =新的UIMap();    [ClassInitialize]
    静态公共无效ClassInit(的TestContext上下文)
    {
        Playback.Initialize();
        尝试
        {
            sharedTest.OpenWindow();
        }
        最后
        {
            Playback.Cleanup();
        }
    }    [ClassCleanup]
    静态公共无效ClassCleanup()
    {
        Playback.Initialize();
        尝试
        {            sharedTest.CloseWindow();
        }
        最后
        {
            Playback.Cleanup();
        }
    }
    #endregion


解决方案

我很长的发问在计算器上,谷歌搜索,并刚刚与code鬼混的过程后,想出了答案。

答案就是使用 AssemblyInitialize AssemblyCleanup 并写code为他们的 DatabaseSetup.cs 的内部文件,该文件应在项目中自动生成。你会发现,已经有在这里AssemblyInitialize功能,但它是非常基本的,没有AssemblyCleanup之后。所有你需要做的就是创建您的UIMap的静态副本,并用它的AssemblyInitialize里面运行了OpenWindow()code。

复制AssemblyInitialize函数的格式创建一个AssemblyCleanup功能,并添加CloseWindow()函数。

请确保您的Open / CloseWindow功能只包含基本的code(如的Process.Start /杀死)作为任何复杂的变量,如形式已经被清理,将无法正常工作。

下面是我的DatabaseSetup.cs code:

 使用System.Data这;
使用System.Data.Common;
使用System.Configuration;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用Microsoft.Data.Schema.UnitTesting;
使用System.Windows.Input;
使用键盘= Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
使用鼠标= Microsoft.VisualStudio.TestTools.UITesting.Mouse;
使用MouseButtons = System.Windows.Forms.MouseButtons;命名空间CSIS_TEST
{
    [识别TestClass()]
    公共类DatabaseSetup
    {
        静态专用的UIMap的UIMap =新的UIMap();
        静态诠释计数= 0;        [AssemblyInitialize()]
        公共静态无效InitializeAssembly(CTX的TestContext)
        {
            DatabaseTestClass.TestService.DeployDatabaseProject();
            DatabaseTestClass.TestService.GenerateData();            如果(计数< 1)
                uIMap.OpenWindow();
            算上++;
        }        [AssemblyCleanup()]
        公共静态无效InitializeAssembly()
        {
            uIMap.CloseWindow();
        }
    }
}

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

解决方案

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();
        }
    }
}

这篇关于我怎么感觉如果我的单元测试是一个有序的测试中的一员,如果它是,它是指在顺序测试持何立场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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