visual-studio-2013 - Visual Studio: Load SQL file from SQL Studio Unit Test
问题描述
I am trying to set up a Microsoft Visual Studio Unit Testing environment for some of my SQL queries. My scenario is somewhat backwards to the typical software development scenario. For any given work product that I work on, I am trying to exercise ONE query through different scenarios, by passing in different parameters and comparing results.
My intention is to load my SQL query from a .sql file in each of my unit tests.
How do I open my SQL file for reading from within my SqlServerUnitTest1.cs
file?
解决方案
解决这个问题很复杂,所以我将描述我所做的大致轮廓。其中一些可能是不必要的,但至少我的说明将帮助您理解所有重要步骤:
- 创建一个新的解决方案,并在其中创建一个新的单元测试项目。(如果您像我一样使用 Reporting Services,请在其旁边创建一个 Reporting 项目)
- 将查询导入您的测试项目,并将属性设置为“Copy if Newer”或“Always Copy”。
- 在测试中加载 SQL 文件。我写了一个形式的函数:
.
public static string LoadSqlFile(string name)
{
string path = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
, @"sql\" + name
);
return File.ReadAllText(path);
}
它通过文件名加载 SQL 文件。这是我的具体问题的答案。
- 编写测试(使用 UnitTest 模板,而不是 SqlServerUnitTest 模板)。我编写了一些代码来从 JSON 生成测试,这使得我团队中的非 C# 用户更容易做到这一点。在我的用例中,每个测试都有一个未声明的参数,该参数被传递给测试。JSON 还包含一个预期结果列表,我们将其与结果进行比较。
推荐阅读
- javascript - 在反应钩子中获取 xml api 返回一个未定义的数组
- mysql - 是否存在在.Net 中同步 MySQL 和 SQLite 的任何方式?
- php - 如何在 PHP 中为通过 AMP 电子邮件发出的请求获取客户端的 IP 地址
- spring-boot - 在 WsConfigurerAdapter 中自动装配 AuthenticationManager 时没有 ServletContext 设置错误
- c# - 在 asp.net 的每个视图中显示消息
- android - 如何为房间数据库android中新添加的表编写迁移
- r - 在 R 中比较具有不同采样率(日期)的时间序列
- python - 按平均值将多维空间划分为超立方体(混合特征类型),但要考虑空白空间,用 Python 实现
- go - golang:为什么这段代码没有死锁?
- angular - 使用服务调用对 ngOnInit 进行角度测试