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'

We must enable TRUSTWORTHY property of the database in which we want to install tSQLt framework.

我们必须启用要在其中安装tSQLt框架的数据库的TRUSTWORTHY属性。

Note: In the following demonstrations, we will use WideWorldImporters sample database.

注意: 在以下演示中,我们将使用 WideWorldImporters 示例数据库。

USE WideWorldImporters
GO
ALTER DATABASE WideWorldImporters SET TRUSTWORTHY ON;

In the second step of the installation process, we will install tSQLt framework into WideWorldImporters database. Therefore, we need to execute tSQLt.class sql query file which is placed in the downloaded file.

在安装过程的第二步中,我们将tSQLt框架安装到WideWorldImporters数据库中。 因此,我们需要执行tSQLt.class sql查询文件,该文件位于下载的文件中。

We will open tSQLt.class file in the SQL Server Management Studio and then execute it. If your installation succeeds, you will see the version of the tSQLt and thanks message.

我们将在SQL Server Management Studio中打开tSQLt.class文件,然后执行它。 如果安装成功,您将看到tSQLt的版本并感谢消息。

In here, I want to add a notice about the tSQLt framework and Azure SQL database collaboration. The main question about this issue is how to install the tSQLt framework to Azure SQL. If we want to install and work tSQLt framework on Azure SQL database we don’t need to enable CLR and TRUSTWORTHY options, we can simply execute tSQLt.class file in the Azure SQL database so that we can use tSQLt framework on Azure SQL. The result set of the following query shows the tSQLt framework objects list which are installed to Azure SQL database also Dbversion column indicates version of the Azure SQL database.

在这里,我想添加有关tSQLt框架和Azure SQL数据库协作的通知。 有关此问题的主要问题是如何将tSQLt框架安装到Azure SQL。 如果要在Azure SQL数据库上安装和使用tSQLt框架,而无需启用CLR和TRUSTWORTHY选项,则只需在Azure SQL数据库中执行tSQLt.class文件,以便可以在Azure SQL上使用tSQLt框架。 以下查询的结果集显示了已安装到Azure SQL数据库的tSQLt框架对象列表,并且Dbversion列指示了Azure SQL数据库的版本。

SELECT @@VERSION,nameFROM sys.objects sysobj where schema_id = (
select sch.schema_id from sys.schemas sch where name='tSQLt' )
order by sysobj.name

我们通过tSQLt进行的第一个SQL单元测试 (Our first SQL unit test through tSQLt)

The tSQLt framework offers several different test methods to us. However, at first, we need to create a test class, because the test class collect the test cases under this class. The below script creates a new test class whose name is DemoUnitTestClass

tSQLt框架为我们提供了几种不同的测试方法。 但是,首先,我们需要创建一个测试类,因为该测试类会收集该类下的测试用例。 下面的脚本创建一个名为DemoUnitTestClass的新测试类。

USE WideWorldImporters
GO
EXEC tSQLt.NewTestClass 'DemoUnitTestClass';

When we create a new test class, in the back of the scene tSQLt creates a schema and adds extended property with value so that tSQLt framework can easily figure out this schema is created for test class. The following query proof this point.

当我们创建一个新的测试类时,tSQLt在幕后创建了一个架构,并添加了带有值的扩展属性,以便tSQLt框架可以轻松地找出为测试类创建的该架构。 以下查询证明了这一点。

select SCHEMA_NAME,objtype,name,value from INFORMATION_SCHEMA.SCHEMATA SC
CROSS APPLY fn_listextendedproperty (NULL, 'schema', NULL, NULL, NULL, NULL, NULL) OL
WHERE OL.objname=sc.SCHEMA_NAME COLLATE Latin1_General_CI_AI
and SCHEMA_NAME = 'DemoUnitTestClass'

And also we can find out this value in the extended event properties of the test class (schema).

我们也可以在测试类(模式)的扩展事件属性中找到该值。

  • Open the Security->Schemas node under the database folders. 打开数据库文件夹下的“安全性->模式”节点。
  • DemoUnitTestClass and navigate to DemoUnitTestClass并导航到“ Properties 属性”
  • Extended Properties page. 扩展属性”页面

In addition, if you want to drop the created test class, we can use the following stored procedure. It takes a test class name as a parameter.

另外,如果要删除创建的测试类,我们可以使用以下存储过程。 它以测试类名称作为参数。

EXEC tSQLt.DropClass  'DemoUnitTestClass'

If you run the DropClass stored procedure, it does not only drop the test class (schema), but it also removes whole test objects which are related to this class. Another consideration about recreating test class with the same name. In this case, tSQLt framework acts as similar to DropClass procedure. In the first step it removes whole test objects that are related to test class and then creates an empty test class with the same name.

如果运行DropClass存储过程,它不仅会删除测试类(架构),而且还会删除与此类相关的整个测试对象。 关于重新创建具有相同名称的测试类的另一个注意事项。 在这种情况下,tSQLt框架的作用类似于DropClass过程。 第一步,它删除与测试类相关的整个测试对象,然后创建一个具有相同名称的空测试类。

tSQLt框架的单元测试方法 (Unit test methods of the tSQLt framework)

The tSQLt framework offers a bunch of test methods for SQL unit testing operations so that we can use this method under different circumstances.

tSQLt框架为SQL单元测试操作提供了许多测试方法,因此我们可以在不同的情况下使用此方法。

  • tSQLt.AsserEqualstSQLt.AsserEquals
  • tSQLr.AssertEqualsTabletSQLr.AssertEqualsTable
  • tSQLt.AssertEmptyTabletSQLt.AssertEmptyTable
  • tSQLt.AssertEqualsStringtSQLt.AssertEqualsString
  • tSQLt.AssertEqualsTableSchematSQLt.AssertEqualsTableSchema
  • tSQLt.AssertLiketSQLt.AssertLike
  • tSQLt.AssertNotEqualstSQLt.AssertNotEquals
  • AssertObjectDoesNotExistAssertObjectDoesNotExist
  • AssertObjectExistsAssertObjectExists
  • AssertResultSetsHaveSameMetaDataAssertResultSetsHaveSameMetaData
  • Fail失败

Let’s learn some details of this method and reinforce with examples.

让我们学习一下此方法的一些细节并通过示例进行补充。

tSQLt.AsserEquals: This method allows us to compare the expected and actual values and it takes three input parameters;

tSQLt.AsserEquals:此方法使我们可以比较期望值和实际值,并且它使用三个输入参数。

@expected: This parameter specifies expected value namely the result of the test value compared with this value.

@expected:此参数指定期望值,即测试值与该值比较的结果。

@actual: This parameter specifies the result of the test.

@actual:此参数指定测试结果。

@message: If the unit test returns fail, we can customize the error message with the help of the parameter.

@message:如果单元测试返回失败,我们可以借助参数自定义错误消息。

Now, we will create a very basic unit test through the tSQLt framework. In the following unit test scenario, we will create a user-defined function which will calculate the tax value of the given parameter afterwards. We will write a unit test case according to this user function. As we have already noticed about that, the main idea of the unit testing is checking behavior of individual, programmable and smallest (atomic) part of the database object.

现在,我们将通过tSQLt框架创建一个非常基本的单元测试。 在以下单元测试方案中,我们将创建一个用户定义的函数,该函数随后将计算给定参数的税额。 我们将根据此用户功能编写一个单元测试用例。 正如我们已经注意到的那样,单元测试的主要思想是检查数据库对象的各个,可编程和最小(原子)部分的行为。

CREATE OR ALTER FUNCTION CalculateTaxAmount(@amt MONEY)
RETURNS MONEY
AS BEGIN
RETURN (@amt /100)*18
END;
GOselect dbo.CalculateTaxAmount(100) AS TaxAmount

As you can see in the above illustration when we send 100 as a parameter into CalculateTaxAmount function and it returns 18. Now, we will write a test case to check this user function behavior.

如上图所示,当我们将100作为参数发送到CalculateTaxAmount函数并返回18时,现在,我们将编写一个测试用例来检查该用户函数的行为。

EXEC tSQLt.NewTestClass 'DemoUnitTestClass';
GOCREATE OR ALTER PROC DemoUnitTestClass.[test tax amount]
AS
BEGIN
DECLARE @TestedAmount as money = 100
DECLARE @expected as money  = 18
DECLARE @actual AS money
SET @actual = dbo.CalculateTaxAmount(100)EXEC tSQLt.AssertEquals @expected , @actualEND

In order to run the test case, we can use tSQLt.Run method. It can take test class name as a parameter so that it can run all the unit tests which are related to this test class or it takes test case name and then it executes particularly.

为了运行测试用例,我们可以使用tSQLt.Run方法。 它可以将测试类名称作为参数,以便它可以运行与此测试类相关的所有单元测试,或者可以使用测试用例名称然后特别执行。

tSQLt.Run 'DemoUnitTestClass.[test tax amount]'
tSQLt.Run 'DemoUnitTestClass'

As you can see in the above images, our test is successful and acceptable. Now, we will change the expected result in the test case and then re-run the unit test.

如上图所示,我们的测试是成功的并且可以接受。 现在,我们将在测试用例中更改预期结果,然后重新运行单元测试。

CREATE OR ALTER PROC DemoUnitTestClass.[test tax amount]
AS
BEGIN
DECLARE @TestedAmount as money = 100
DECLARE @expected as money  = 20
DECLARE @actual AS money
SET @actual = dbo.CalculateTaxAmount(100)EXEC tSQLt.AssertEquals @expected , @actualEND
GOtSQLt.Run 'DemoUnitTestClass.[test tax amount]'

When we read the messages about the unit test result, it is obviously explaining everything about the unit test issue. In the first line, we can find out the main problem of the unit test. It clarifies that the expected and actual values are different.

当我们阅读有关单元测试结果的消息时,显然可以解释有关单元测试问题的所有信息。 在第一行中,我们可以找到单元测试的主要问题。 它阐明了期望值和实际值是不同的。

I want to mention about unit test case naming conventions. The test’s case names must begin with “test” and if we don’t do this tSQLt framework, we cannot find and execute it. As we noticed about the message parameter of the tSQLt.AsserEquals method. Now, we will implement this test message to our unit test.

我想提一下单元测试用例的命名约定。 测试的案例名称必须以“ test”开头,如果不执行此tSQLt框架,则无法找到并执行它。 正如我们注意到的,有关tSQLt.AsserEquals方法的message参数。 现在,我们将这个测试消息实现到我们的单元测试中。

CREATE OR ALTER PROC DemoUnitTestClass.[test tax amount]
AS
BEGIN
DECLARE @TestedAmount as money = 100
DECLARE @expected as money  = 20
DECLARE @actual AS money
DECLARE @Message AS VARCHAR(500)='Wrong tax amount'
SET @actual = dbo.CalculateTaxAmount(100)EXEC tSQLt.AssertEquals @expected , @actual ,@MessageEND
GOtSQLt.Run 'DemoUnitTestClass.[test tax amount]'

So far, we mentioned about installation and essentials of the tSQLt framework, but in the real world, unit test scenarios are much complex than these examples. For this reason, we must understand Arrange, Act and Assert pattern well according to SQL unit testing. This pattern suggests separating unit tests into three parts so that we can write more clear and readable tests.

到目前为止,我们已经提到了tSQLt框架的安装和要点,但是在现实世界中,单元测试方案比这些示例要复杂得多。 因此,我们必须根据SQL单元测试充分理解Arrange,Act和Assert模式。 这种模式建议将单元测试分为三个部分,以便我们可以编写更清晰易读的测试。

Arrange: In this part, we can declare the variables, or we can define preconditions and inputs.

安排:在这一部分中,我们可以声明变量,也可以定义前提条件和输入。

Act: In this part, we can execute the code that is under the test and then capture the result of the executed code.

行动:在这一部分中,我们可以执行测试中的代码,然后捕获执行后的代码的结果。

Assert: In this part, we compare the expected and actual value.

断言:在这一部分中,我们将期望值与实际值进行比较。

Now, we will specify this part for our previous unit test example.

现在,我们将为之前的单元测试示例指定此部分。

CREATE OR ALTER PROC DemoUnitTestClass.[test tax amount]
AS
BEGIN
----------------------Arrange-----------------------------
DECLARE @TestedAmount as money = 100                   ---
DECLARE @expected as money  = 20                       ---
DECLARE @actual AS money          ---
DECLARE @Message AS VARCHAR(500)='Wrong tax amount'    ---
----------------------------------------------------------
----------------------Act---------------------------------
SET @actual = dbo.CalculateTaxAmount(100)              ---
----------------------------------------------------------
----------------------Assert------------------------------
EXEC tSQLt.AssertEquals @expected , @actual ,@Message  ---
----------------------------------------------------------
END

结论 (Conclusion)

In this article, we mentioned about the importance of the SQL unit testing and how to act this approach through the tSQLt framework. First of all, we need to well understand to the main logic of the tSQLt framework essentials so that we can develop more complex test cases.

在本文中,我们提到了SQL单元测试的重要性以及如何通过tSQLt框架采取这种方法。 首先,我们需要充分了解tSQLt框架要点的主要逻辑,以便我们可以开发更复杂的测试用例。

目录 (Table of contents)

SQL Unit Testing with SSDT (SQL Server Data Tools)
SQL unit testing with the tSQLt framework for beginners
SQL Unit testing with the tSQLt framework and SQL Server Database Project integration
SQL unit testing best practices
How to use fake tables in SQL unit testing?
How to use fake functions with SQL unit testing?
SQL Unit Testing Mocking Stored Procedures
SQL Unit Testing: Working with exceptions
使用SSDT进行SQL单元测试(SQL Server数据工具)
使用tSQLt框架SQL单元测试面向初学者
使用tSQLt框架和SQL Server Database Project集成进行SQL单元测试
SQL单元测试最佳实践
如何在SQL单元测试中使用假表?
如何在SQL单元测试中使用伪函数?
SQL单元测试模拟存储过程
SQL单元测试:使用异常

翻译自: https://www.sqlshack.com/sql-unit-testing-with-the-tsqlt-framework-for-beginners/

sql初学者指南

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

  1. java初学者指南_企业Java中事务隔离级别的初学者指南

    java初学者指南 介绍 基于ACID事务属性的关系数据库强一致性模型. 在本文中,我们将阐明对资源本地事务和JTA事务使用不同的事务隔离级别和各种配置模式的背后原因. 隔离和一致性 在关系数据库系统 ...

  2. sql初学者指南_初学者SQL示例:SQL SELECT语句的用法

    sql初学者指南 SQLSELECT statements are used to retrieve data from the database and also, they populate th ...

  3. sql查询初学者指南_面向初学者SQL Server查询执行计划–类型和选项

    sql查询初学者指南 When a DBA is working with the SQL Server, he/she might sometimes say that the execution ...

  4. sql查询初学者指南_适用于初学者SQL Server查询优化技巧与实际示例

    sql查询初学者指南 In this article, we will continue to learn essential techniques of the SQL Server query t ...

  5. sql查询初学者指南_面向初学者SQL Server查询执行计划–聚集索引运算符

    sql查询初学者指南 We have discussed how to created estimated execution plans and actual execution plans in ...

  6. sql查询初学者指南_面向初学者SQL Server查询执行计划–非聚集索引运算符

    sql查询初学者指南 Now that we understand what Clustered Index Scan and Clustered Index Seek are, how they o ...

  7. selenium初学者指南_如何进化人工智能生活:初学者指南

    selenium初学者指南 'Evolving artificial intelligent life' might sound like a grandiose claim from an indi ...

  8. sql azure 语法_什么是Azure SQL Cosmos DB?

    sql azure 语法 介绍 (Introduction) In the Azure Portal, you will find the option to install Azure SQL Co ...

  9. sql机器学习服务_机器学习服务–在SQL Server中配置R服务

    sql机器学习服务 The R language is one of the most popular languages for data science, machine learning ser ...

最新文章

  1. ICML 2019:序列到序列自然语言生成任务超越BERT、GPT!微软提出通用预训练模型MASS | 技术头条...
  2. mysql innodb flush method_对innodb_flush_method的一点解释
  3. 【Docker系列教程之三】Docker容器是如何工作的
  4. idea中.ignore插件的使用
  5. laradock 进入 工作区
  6. videojs学习随笔记录
  7. centos下java编程工具_centos下安装JAVA开发工具(4)------Redis
  8. 下载站源码 php,thinkphp开发素材资源源码下载站整站源代码
  9. 《天才在左、疯子在右》随笔
  10. ue转换文件格式linux,关于windows与unix之间文件格式转换问题。UE编辑器中(CR/LF)问题...
  11. 一个程序员购买保险之路-1(适用于所有人)
  12. 利用代码实现山脊线、山谷线的提取(arcpy版)
  13. 用计算机弹出记事本,win7电脑开机就会弹出Desktop.ini记事本怎么办?
  14. WLGK-51单片机接口技术基础实验 —LED闪烁灯
  15. 怎么将整个英文的pdf文档翻译成中文
  16. GhostXP_SP2电脑公司特别版_5.5
  17. 情绪激动的时候最好不要做决定
  18. Unity 材质替换工具说明
  19. Centos 配置阿里yum源
  20. MacOS好用的播放速度控制扩展(插件)推荐

热门文章

  1. flex布局怎么设置子元素大小_48张小图带你领略Flex 布局之美
  2. Java的jdk1.6与jre1.8中存在的差异
  3. hadoop 3.0.0 alpha3 安装、配置
  4. PHP 中获取文件名及路径
  5. 通过rss阅读器写blog
  6. 040_Auto.js
  7. JavaScript学习(六十)—JSON
  8. 力扣 有多少小于当前数字的数字
  9. 为什么坐飞机时会耳痛?
  10. 为什么都开始流行将洗手台装在厕所外?