SQL 服务器数据库单元测试和持续集成

SQL Server Database Unit Testing and Continuous Integration

我在 Visual Studio 2013 年构建了一套 SQL 服务器数据库单元测试来测试存储过程和函数。测试 运行 对我的本地数据库很好,但我也希望它们 运行 对 DEV 服务器的数据库作为 CI 过程的一部分。

为了实现这一点,我正在构建测试 DLL 并将 DLL 和配置文件复制到 DEV 服务器,然后 运行 连接命令行 vstest.console.exe 进行测试。这在我的本地机器上运行良好,但是当我尝试在 DEV 上 运行 它时,每次测试都会出现以下异常:

An error occurred while SQL Server unit testing settings were being read from the configuration file. Click the test project, open the SQL Server Test Configuration dialog box from the SQL menu, add the settings to the dialog box, and rebuild the project.

我已经尝试使用 slow cheetah 来确保在构建解决方案之前应用 App.Config 转换,我可以看到转换正在配置文件中应用,但我的测试仍然失败。

有没有人想出办法做到这一点?必须重建 DLL 才能使用配置文件中的连接字符串更改,这似乎很奇怪。

我也尝试按照此 post 中的建议进行操作,但没有成功。

我终于搞定了。似乎用于 SQL 单元测试的配置文件的结构发生了变化。在我的配置中,我有一个名为 SqlUnitTesting 的部分,其中包含 ExecutionContext 和 PrivilegedContext 的连接字符串。我注意到当我创建一个新的单元测试项目时,部分名称已更改为 SqlUnitTesting_VS2013。将我现有的配置更改为此解决了问题。我只能假设我在这两个环境中调用了稍微不同的 vstest.console.exe 版本。

总结如何从命令行SQL服务器单元测试在多个环境中工作vstest.console.exe应用程序这些是要采取的步骤:

  1. 为 Visual Studio 安装 Slow Cheetah 扩展,它根据所选的构建配置管理 App.config 文件的转换。
  2. 确保您的 App.config 文件有一个名为 SqlUnitTesting_VS2013 的部分,其中包含您的 ExecutionContext 和 PrivilegedContect 连接字符串。
  3. 根据构建配置(调试、发布等)为您使用的每个环境设置慢速 cheetah 转换。您需要阅读 Slow Cheetah doco 才能执行此操作,但基本上您是在为每个在构建时转换为 App.config 的环境设置连接字符串。
  4. 使用您的目标环境配置构建解决方案。
  5. 将 DLL 和配置从相关 bin/buildConfiguration 目录复制到您的目标环境。
  6. 运行 vstest.console.exe 并观察您的测试是否通过。

希望对大家有所帮助。

以下源代码示例基于 Greg 的回答。

app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>...</configSections>
    <SqlUnitTesting>
       <DataGeneration .. />
       <ExecutionContext .. />
       <PrivilegedContext.. />
    </SqlUnitTesting>
    <startup>...</startup>
</configuration>

app.Release.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <configSections>...</configSections>
    <SqlUnitTesting xdt:Transform="Replace">
       <DataGeneration .. />
       <ExecutionContext .. />
       <PrivilegedContext.. />
    </SqlUnitTesting>
    <startup>...</startup>
</configuration>

更多关于SlowCheetah的信息,您可以参考以下内容link:Github and Microsoft DevBlogs