使用Visual Studio测试项目进行回归测试,自动启动/调试应用程序

时间:2015-12-12 14:41:11

标签: .net visual-studio debugging testing automated-tests

我希望在清理以前的测试,设置特定的应用程序配置和数据库配置/对象后,使用Visual Studio 2010或2013运行回归测试,启动每个测试的应用程序。

它们单元测试,没有链接到测试中的应用程序代码/对象。他们只创建应用程序配置+输入并检查结果。应用程序类型是一个可执行文件,带有VS的控制台窗口,数据库后端,在控制台中输入密钥,并作为Windows服务安装。

/* pseudo code */

[TestMethod]
public void ProcessCustomerOrderTest()
{
    bool isDebugMode = FindOutIfIsDebugModeSomehow();

    var orderData = CreateOrderDataSomehow(); /* real code here */
    SetupNewCustomerOrderInDatabase(orderData);

    /* Running/debugging the application VS project is preferred 
    over a new Process() for an executable! */
    using(var applProc = new Process()) 
    {
        /* setup and start applProc */
        /* or VS project debugging */
        if (isDebugMode)
        {
            AttachVsDebuggerToProcess(applProc); 
            /* any real code for this? */
        }

        while(!CheckIsOrderDataProcessed(orderData) 
            && !applProc.HasExited)
        { 
            Thread.Sleep(5000); 
        }

        if (!applProc.HasExited)
        {
            SignalApplStop(applProc); /* let it finish, don't kill */
            applProc.WaitForExit();
        }
    }
    CheckProcessOrderResultsInDatabase(); /* real check code here */
}

我知道如何手动完成,在测试中放置中断(调试断点或对话框窗口“立即启动应用程序!”),然后手动启动应用程序。但是对于20到30次测试,每次测试需要几分钟,这很乏味且浪费时间。

一个目的是从VS自动运行测试套件,因此我可以在午休或会议期间运行它,而无需启动/停止任何事情(这可能没有调试中断)。

还可以从VS进行调试,同时调试测试和应用程序,无需手动启动/停止或附加到进程,仅在出现问题时才会中断。所以我可以在此期间做其他事情。

我认为有一个解决方案和教程,我只是找不到合适的(不是GUI测试,不是单元测试,不是VS 2015 Enterprise,TFS和其他新的测试框架 - 我会可能在另一次使用后者,但现在需要一个简单的解决方案。)

1 个答案:

答案 0 :(得分:0)

要破解并附加代码而不是手动,请查看System.Diagnostics.Debugger.Break()

https://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx

查看#if以确定是否调试。

 #define DEBUG
// ...
#if DEBUG
   Console.WriteLine("Debug version");
#endif

https://msdn.microsoft.com/en-us/library/4y6tbswk.aspx