sql初学者指南_使用tSQLt框架SQL单元测试面向初学者

sql初学者指南

tSQLt is a powerful, open source framework for SQL Server unit testing. In this article, we will mainly focus on how to create and run SQL unit testing with help of the tSQLt framework. Before we begin to learn tSQLt framework details, let’s discuss essentials and importance of the database unit testing approach, in general. SQL unit testing is a vital and inseparable part of the modern database development approach and it makes possible to prevent errors before producing the release deployment process. Some database developers are still discussing the needs of the SQL unit testing concept; however, database unit testing is very significant to control and check the behavior of the individual parts of the database. For this reason, we cannot ignore the need to write unit test cases.

tSQLt是用于SQL Server单元测试的功能强大的开源框架。 在本文中,我们将主要关注如何在tSQLt框架的帮助下创建和运行SQL单元测试。 通常,在开始学习tSQLt框架细节之前,让我们讨论数据库单元测试方法的本质和重要性。 SQL单元测试是现代数据库开发方法中至关重要且不可分割的一部分,它可以在产生发行版部署过程之前防止错误。 一些数据库开发人员仍在讨论SQL单元测试概念的需求。 但是,数据库单元测试对于控制和检查数据库各个部分的行为非常重要。 因此,我们不能忽略编写单元测试用例的需要。

Essentially, unit tests provide code coverage over database objects (stored procedures, triggers, functions etc.) behavior so that we can automatically regression test changes. In terms of SQL unit testing, the tSQLt framework offers several advantages. Some of those are described in the following bullets with detailed descriptions.

本质上,单元测试提供了覆盖数据库对象(存储过程,触发器,函数等)行为的代码,因此我们可以自动回归测试更改。 在SQL单元测试方面,tSQLt框架具有多个优点。 在以下项目符号中对其中一些进行了详细描述。

tSQLt框架的优点: (Benefits of the tSQLt framework:)

  • Enables using T-SQL codes in unit tests. This is the most important advantage of tSQLt because we don’t need to learn new programing language or platform to create and run SQL unit test, such as if you want to write a new test for particular user-defined function; we just need a tSQLt installed database and SQL Server Management Studio or any query editor

    在单元测试中启用使用T-SQL代码。 这是tSQLt的最重要优势,因为我们不需要学习新的编程语言或平台来创建和运行SQL单元测试,例如,如果您想为特定的用户定义函数编写新的测试; 我们只需要安装一个tSQLt数据库和SQL Server Management Studio或任何查询编辑器
  • Unit tests are automatically running in the transaction log. We don’t need any data cleanup work after the unit tests because every data manipulation process rolls back after the unit test

    单元测试会自动在事务日志中运行。 单元测试后,我们不需要任何数据清理工作,因为在单元测试后,每个数据处理过程都会回滚
  • Allows using mocked (fake) objects. A mocked object simulates the real object’s behavior so the tested objects do not affect other dependencies and we can also create isolated SQL unit tests. For example, our aim is to write unit test for a particular stored procedure, but this stored procedure includes a user-defined function, however, we have to isolate stored procedure unit test from this user-defined function. We can achieve this idea by writing a mock function which is related to the tested stored procedure

    允许使用模拟(伪)对象。 模拟对象模拟了真实对象的行为,因此被测试的对象不会影响其他依赖关系,我们还可以创建隔离SQL单元测试。 例如,我们的目标是为特定的存储过程编写单元测试,但是该存储过程包括一个用户定义的函数,但是,我们必须将该存储过程的单元测试与该用户定义的函数隔离。 我们可以通过编写与测试的存储过程相关的模拟函数来实现此想法
  • Completely free and open source. tSQLt is a free and open source project, so, we can use it without any charge

    完全免费和开源。 tSQLt是一个免费的开源项目,因此,我们可以免费使用它
  • rd party software. If you want to integrate tSQLt framework to Visual Studio you can find it on 第三方软件。 如果要将tSQLt框架集成到Visual Studio,可以在tSQLt Test Adapter for Visual Studio 2017. Or you can use a 3适用于Visual Studio 2017的tSQLt测试适配器上找到它。 或者你可以使用一个rd party product like 第三方产品如 ApexSQL Unit Test ApexSQL单元测试

如何安装tSQLt (How to install tSQLt)

tSQLt installation is very easy. At first, we will download the zipped file from tsqlt.org in the download section. Then we need to enable CLR (SQL Common Language Runtime) in the SQL server instance because tSQLt requires this option. Run the following script to enable CLR functionally of the SQL Server afterwards we ensure to running vales (run_value) of this option.

tSQLt的安装非常简单。 首先,我们将从下载部分的tsqlt.org下载压缩文件。 然后,我们需要在SQL Server实例中启用CLR(SQL公共语言运行时),因为tSQLt需要此选项。 然后,运行以下脚本以启用SQL Server的CLR功能,然后确保运行此选项的值(run_value)。

EXEC sp_configure 'clr enabled', 1;
RECONFIGURE;
GO
EXEC sp_configure 'clr enabled'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值