【问题标题】:VS2013 - SQL Server Unit Test - Query TimeoutVS2013 - SQL Server 单元测试 - 查询超时
【发布时间】:2015-03-07 23:27:29
【问题描述】:

我正在 Visual Studio 2013 中创建我的第一个 SQL Server 单元测试。我有一个在 SQL Server Management Studio 窗口中成功运行的脚本。我有一个重置数据库状态的脚本,它也成功运行。 (我已将其设置为我的 PreTest 脚本。)我的单元测试运行大约需要 2 分钟。 (它在幕后生成大量数据。)但 SQL Server 单元测试似乎配置为在 30 秒后超时。我一直在尝试找出如何更改超时设置,但似乎找不到。 (请注意,我指的不是连接超时,而是查询执行超时。)我想以 3 分钟的超时设置运行这个测试。

谢谢,提前。

【问题讨论】:

    标签: sql-server visual-studio unit-testing visual-studio-2013


    【解决方案1】:

    数字,我在发布问题之前花了一个小时搜索,然后在 10 分钟后找到答案。这是我的测试代码,其中包含 commandtimeout 的行:

        [TestMethod()]
        public void CreateScenario_FromProject_Q123314_FromVersion_AA()
        {
            SqlDatabaseTestActions testActions = this.SqlTest1Data;
            // Execute the pre-test script
            // 
            System.Diagnostics.Trace.WriteLineIf((testActions.PretestAction != null), "Executing pre-test script...");
            SqlExecutionResult[] pretestResults = TestService.Execute(this.PrivilegedContext, this.PrivilegedContext, testActions.PretestAction);
            // Execute the test script
            // 
            System.Diagnostics.Trace.WriteLineIf((testActions.TestAction != null), "Executing test script...");
            this.ExecutionContext.CommandTimeout = 180;  // HERE IS THE COMMANDTIMEOUT
            SqlExecutionResult[] testResults = TestService.Execute(this.ExecutionContext, this.PrivilegedContext, testActions.TestAction);
            // Execute the post-test script
            // 
            System.Diagnostics.Trace.WriteLineIf((testActions.PosttestAction != null), "Executing post-test script...");
            SqlExecutionResult[] posttestResults = TestService.Execute(this.PrivilegedContext, this.PrivilegedContext, testActions.PosttestAction);
        }
    

    【讨论】:

    • 将此标记为您的答案,将来可能会对某人有所帮助。
    • 我会的。但我必须等待 2 天的限制才能完成。
    【解决方案2】:

    这对我很有帮助。我发现并愿意分享一种替代方法,使其更具全局性,而不仅仅是适用于单个测试用例。

    您可以通过更新app.config 中连接字符串的CommandTimeout 来全局应用超时设置(如下所示)。这适用于 SSDT 2016 和 Visual Studio 2017。

    <ExecutionContext Provider="System.Data.SqlClient" ConnectionString="" CommandTimeout="60" />
    <PrivilegedContext Provider="System.Data.SqlClient" ConnectionString="" CommandTimeout="60" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 2019-02-26
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多