使用tSQLt框架和SQL Server Database Project集成进行SQL单元测试

In the previous article SQL unit testing with the tSQLt framework for beginners in this series, we covered tSQLt framework essentials and installation. we also demonstrated basic SQL unit testing through the tSQLt framework. In this article, we will continue to explore SQL unit testing and we will also discuss the benefits of SQL unit testing. Additionally, we will learn how we can use the tSQLt framework in a SQL Server Database Project.

在本系列的初学者使用tSQLt框架SQL单元测试文章中,我们介绍了tSQLt框架的要点和安装。 我们还通过tSQLt框架演示了基本SQL单元测试。 在本文中,我们将继续探索SQL单元测试,还将讨论SQL单元测试的好处。 此外,我们还将学习如何在SQL Server数据库项目中使用tSQLt框架。

According to my own experience, if you ask the developers that why they care about writing unit tests, the general answer will be something along the lines of “We don’t have enough time to write unit tests.”

根据我自己的经验,如果您问开发人员为什么他们关心编写单元测试,那么通常的回答将是“我们没有足够的时间编写单元测试”。

Unfortunately, this type of excuse can’t be acceptable because if we experience an error or bug in the production releases, we must spend more time to fix it. No doubt, error fixing time is more expensive and painful than writing unit tests. For this reason, if we want to eradicate this type of problem, we must provide our database designs with unit tests so that we can be confident with our database code. Certainly, unit test helps us to minimize errors and handle them in the early stages of the development process so that the production errors or bugs will be decrease. In the below list, you can find some suggestions about writing SQL unit testing so that you can improve our unit test quality.

不幸的是,这种借口是无法接受的,因为如果我们在生产版本中遇到错误或错误,我们必须花费更多的时间来解决它。 毫无疑问,错误修复时间比编写单元测试更加昂贵和痛苦。 因此,如果要消除此类问题,必须为数据库设计提供单元测试,以便对数据库代码充满信心。 当然,单元测试可以帮助我们最大程度地减少错误,并在开发过程的早期阶段进行处理,从而减少生产错误或错误。 在下面的列表中,您可以找到有关编写SQL单元测试的一些建议,以便提高我们的单元测试质量。

有关SQL单元测试的一些建议: ( Some suggestions about SQL unit testing: )

  • Make sure each SQL unit test is checking only one indusial functional part

    确保每个SQL单元测试仅检查一个工业功能部分
  • Don’t use more than one assert expression in the SQL unit test

    在SQL单元测试中不要使用多个断言表达式
  • To isolate the behavior of the SQL unit test from dependencies

    将SQL单元测试的行为与依赖关系隔离
  • Give proper and understandable names to the SQL unit tests

    为SQL单元测试提供正确易懂的名称
  • Create documentation about SQL unit test cases and this documentation must include all possible input and output parameters

    创建有关SQL单元测试用例的文档,并且该文档必须包含所有可能的输入和输出参数

SQL Server数据库项目 ( SQL Server Database Project )

If we ever decided to design a new database project in Visual Studio, our first template choice will be a SQL Server Database Project because it allows us to create a new database design very easily with the help of the visual designer tools and we can also import database schema from existing database or data-tier application (.dacpac) files.

如果我们决定在Visual Studio中设计一个新的数据库项目,那么我们的第一个模板选择将是SQL Server数据库项目,因为它允许我们借助视觉设计器工具非常轻松地创建新的数据库设计,并且我们还可以导入现有数据库或数据层应用程序(.dacpac)文件中的数据库架构。

Note: Dacpac is a zipped file which includes the database objects definitions in it so that we can import, export and upgrade the databases.

注意: Dacpac是一个压缩文件,其中包含数据库对象定义,因此我们可以导入,导出和升级数据库。

In the database project, we can find and add various database objects to our database design and we can also publish it to an on-premise SQL Server Instance or Azure SQL.

在数据库项目中,我们可以找到各种数据库对象并将其添加到数据库设计中,还可以将其发布到本地SQL Server 实例Azure SQL中

Now we will jump into the demonstration …

现在我们将跳入示范……

创建一个新SQL Server数据库项目 (Create a new SQL Server Database Project)

In this part, we will configure the required settings in order to integrate tSQLt framework and database project and then create a SQL Server Database Project. However, first of all, we need to install the tSQLt Test Adapter for Visual Studio 2017. This adapter allows us to run tSQLt framework unit tests in database projects, so that we can evaluate the results of the unit test behaviors and results in the Test Explorer tab. We will download tSQLt Test Adapter for Visual Studio 2017 and then we will install it. In the downloaded tSQLtTestAdapter file we will click Open.

在这一部分中,我们将配置必需的设置,以集成tSQLt框架和数据库项目,然后创建一个SQL Server数据库项目。 但是,首先,我们需要为Visual Studio 2017安装tSQLt测试适配器 。 该适配器使我们可以在数据库项目中运行tSQLt框架单元测试,以便我们可以在“ 测试资源管理器”选项卡中评估单元测试行为的结果和结果。 我们将下载适用于Visual Studio 2017的tSQLt测试适配器,然后将其安装。 在下载的tSQLtTestAdapter文件中,我们将单击Open

SQL Unit testing - tSQLt Test Adapter for Visual Studio 2017 installation

And then, VSIX Installer will ask you which tools you want to install in tSQLtTestAdapter. We will check Microsoft SQL Server Data Tools for Visual Studio 2017 (SSDT) and Visual Studio Enterprise 2017 (2) and then click the Install button. It will add a new extension to Visual Studio.

然后 VSIX安装程序将询问您要在tSQLtTestAdapter中安装哪些工具。 我们将检查适用于Visual Studio 2017(SSDT)Visual Studio Enterprise 2017(2)的 Microsoft SQL Server数据工具 ,然后单击“ 安装”按钮。 它将向Visual Studio添加新的扩展。

SQL Unit testing - tSQLt Test Adapter for Visual Studio VSIX Installer

If our installation is successful, we will see the following image:

如果我们的安装成功,我们将看到以下图像:

SQL Unit testing - tSQLt Test Adapter for Visual Studio success message.

Now, we will launch Visual Studio 2017 (SSDT) and then click the Tools menu and select Extensions and Updates.

现在,我们将启动Visual Studio 2017(SSDT),然后单击“ 工具”菜单,然后选择“ 扩展 更新”

SQL Unit testing - Check up the extensions in Visual Studio

In this screen, we will check out the installation of the tSQLtTestAdapter for Visual Studio.

在此屏幕中,我们将检出Visual Studio的tSQLtTestAdapter的安装。

SQL Unit testing - Extensions and Updates list in Visual Studio

Now, we will add a new SQL Server Database project so that we can design a new database. This database includes only one scalar-valued function and then we will test it in the main screen of the Visual Studio. Click the File menu and then select Project.

现在,我们将添加一个新SQL Server数据库项目,以便我们可以设计一个新的数据库。 该数据库仅包含一个标量值函数,然后我们将在Visual Studio的主屏幕中对其进行测试。 单击文件菜单,然后选择项目

SQL Unit testing - How to add SQL Server Database Project?

We will select SQL Server Database Project and specify a location for the project. We will give TestDbProject name to it.

我们将选择“ SQL Server数据库项目”并指定项目的位置。 我们将为其命名TestDbProject

SQL Unit testing - How to find  SQL Server Database Project?

Right-click in the TestDbProject and select New Item… and after selecting we will add scalar-valued function whose name is CalculateTaxAmount.

右键单击TestDbProject并选择New Item… ,然后选择,我们将添加名称为CalculateTaxAmount的标量值函数

SQL Unit testing - Add Scalar-valued function to database project

We will change the function script with the following codes.

我们将使用以下代码更改功能脚本。

CREATE FUNCTION [dbo].[CalculateTaxAmount](@amt MONEY)
RETURNS MONEY
AS BEGIN
RETURN (@amt /100)*18 
END;

SQL Unit testing - Create Scalar-valued function in database project

Right-click in the database project and select Publish.

右键单击数据库项目,然后选择发布

SQL Unit testing - Publish SQL Server Database Project to SQL Server instance

In the publish screen, we will configure the target database connections and then we will publish the project.

在发布屏幕中,我们将配置目标数据库连接,然后发布项目。

SQL developer unit testing - Database project publish settings.

We can track the status of the published project in the Data Tools Operations tab

我们可以在“ 数据工具操作”选项卡中跟踪已发布项目的状态

SQL developer unit testing - Data Tools Operations publish success message.

When we connect to the target SQL Server, we can find out our database which we published as a TestDbProject name.

当我们连接到目标SQL Server时,我们可以找到我们以TestDbProject名称发布的数据库。

SQL developer unit testing - List of SQL Server databases

为tSQLt框架配置数据库项目 (Configure database project for tSQLt framework)

In the following steps, we will create a new database and we will use this database in order to create and run a unit test case. A question might arise in your mind that “Why we separate the projects and use separated databases?” Actually, if we place unit test and database design into the same project, we have to put in extra effort to separate unit test cases during production of the publishing operations, so, for this reason we will separate database project and test project. Now, we will connect to target server which we published in the TestDbProject and then create a new database. After that, we will install tSQLt framework.

在以下步骤中,我们将创建一个新数据库,并将使用该数据库来创建和运行单元测试用例。 您可能会想到一个问题:“为什么我们分开项目并使用分开的数据库?” 实际上,如果将单元测试和数据库设计放在同一个项目中,则必须在发布操作的生产过程中付出更多的努力来分离单元测试用例,因此,出于这个原因,我们将数据库项目和测试项目分开。 现在,我们将连接到我们在TestDbProject中发布的目标服务器,然后创建一个新数据库。 之后,我们将安装tSQLt框架。

  • USE  master
    GO
    EXEC sp_configure 'clr enabled', 1;
    RECONFIGURE;
     
    GO
    DROP DATABASE IF EXISTS TestDbProject_UnitTest
    GO
    CREATE DATABASE  TestDbProject_UnitTest
    GO
    ALTER DATABASE TestDbProject_UnitTest SET TRUSTWORTHY ON;
    GO
    

  • tSQLt framework and execute the tSQLt框架并执行tSQLt.tSQLtclass.上课 。 最后在sql query file in the TestDbProject _ TestDbProject_UnitTest数据库中的UnitTest database finally you will see the thanks message and sql查询文件中,您将看到感谢消息和tSQLt framework version information tSQLt框架版本信息
  • EXECUTE tsqlt.NewTestClass 'TestUClass'
    

    Note: For detailed information about the tSQLt framework installation, see SQL unit testing with the tSQLt framework for beginners

    注意: 有关tSQLt框架安装的详细信息,请参阅 初学者使用tSQLt框架进行SQL单元测试

Now, we will return into Visual Studio and add a new database project. This project will include our unit test cases. Right click in the solution and then select New Project…

现在,我们将返回Visual Studio并添加一个新的数据库项目。 该项目将包括我们的单元测试用例。 右键单击解决方案,然后选择“新建项目”。

SQL developer unit testing - Add new database project.

SQL developer unit testing - Add SQL Server Database Project

We will import TestDbProject_UnitTest database to this project. Right click into TestDbProject.UnitTest project and select Import and then Database…

我们将把TestDbProject_UnitTest数据库导入到这个项目。 右键单击进入TestDbProject.UnitTest项目,然后选择导入 ,然后选择数据库…

SQL developer unit testing - Import database from SQL Server into database project

In the Import Database screen, we will make a connection to TestDbProject_UnitTest

在“导入数据库”屏幕中,我们将连接到TestDbProject_UnitTest。

database and click the Start button.

数据库,然后单击开始按钮。

SQL developer unit testing - Import database settings

And then Import Database screen will appear.

然后将出现“ 导入 数据库”屏幕。

SQL developer unit testing - Import database summary

Our solution will look like the following illustration:

我们的解决方案如下图所示:

SQL developer unit testing - Solution view of the database design and tSQLt framework.

If you try to build the database project, you will face the following error:

如果您尝试构建数据库项目,则将遇到以下错误:

SQL developer unit testing - Error list of the database project

We will click the References and then select the Add Database Reference

我们将单击“ 引用” ,然后选择“ 添加数据库引用”。

SQL developer unit testing - Add database reference

In the Add Database Reference screen, we will master database as a reference because some of the tSQLt objects are related to master database tables or views.

在“ 添加 数据库 引用”屏幕中,由于某些tSQLt对象与主数据库表或视图相关,因此我们将主数据库作为参考。

SQL developer unit testing - Add master database as a reference

We will create test setting file which includes only the following settings and then will save as

我们将创建仅包含以下设置的测试设置文件,然后另存为

the testlocal.runsettings name. Afterward, we will locate this file into the project folder.

testlocal.runsettings名称。 之后,我们将把该文件放在项目文件夹中。

<?xml version=”1.0″ encoding=”utf-8″?>
<RunSettings>
<TestRunParameters>
<Parameter name=”TestDatabaseConnectionString” value=”server=localhost;initial catalog= TestDbProject_UnitTest;integrated security=sspi” />
</TestRunParameters>
</RunSettings>

<?xml版本=“ 1.0”编码=“ utf-8”?>
<RunSettings>
<TestRunParameters>
<参数名称=“ TestDatabaseConnectionString”值=“服务器=本地主机;初始目录= TestDbProject_UnitTest;集成安全性= sspi” />
</ TestRunParameters>
</ RunSettings>

SQL developer unit testing - Unit test config file

In this step, we will change the test setting file and set this new file which we created in the previous step. Click the Test menu and then click to Select Test Setting File and then select the testlocal.runsettings file.

在此步骤中,我们将更改测试设置文件并设置我们在上一步中创建的新文件。 单击“ 测试”菜单,然后单击以选择“测试设置文件” ,然后选择testlocal.runsettings文件。

SQL developer unit testing - How to select test file in Visual Studio.

在数据库项目中运行tSQLt单元测试: (Run tSQLt unit tests in database project:)

In this part, we will add a new unit test to TestDbProject.UnitTest and it automatically found by tSQLt extension. We will add a new stored procedure which includes the unit test case.

在这一部分,我们将向TestDbProject添加一个新的单元测试。 并通过tSQLt扩展自动找到UnitTest 。 我们将添加一个新的存储过程,其中包括单元测试用例。

SQL developer unit testing - Add a new stored procedure to database project

We will change the stored procedure with the following query. The significant point about this step is that stored procedure name and query file name must be same.

我们将通过以下查询更改存储过程。 关于此步骤的重要意义是存储过程名称和查询文件名称必须相同。

CREATE PROC  TestUClass.[test tax amount]
AS
BEGIN
DECLARE @TestedAmount as money = 100
DECLARE @expected as money  = 18
DECLARE @actual AS money 
SET @actual = TestDbProject.dbo.CalculateTaxAmount(100)
 
EXEC tSQLt.AssertEquals @expected , @actual
 
END

SQL developer unit testing - Create a new tSQLt unit test case in database project

And the finally test tax amount unit test appears in the test explorer so that we can run all tSQLt unit test.

最终测试税额单元测试将出现在测试浏览器中,以便我们可以运行所有tSQLt单元测试。

SQL developer unit testing - Unit test and naming rules

In this step, we will add a Post-Deployment Script file because we want to enable TRUSTWORTHY option after the publishing process so that we don’t experience any error when we run unit test cases.

在此步骤中,我们将添加一个部署后脚本文件,因为我们要在发布过程之后启用TRUSTWORTHY选项,以便在运行单元测试用例时不会遇到任何错误。

SQL developer unit testing - Add a new Post-Deployment  Script to database project

We will add the following query to PreDeployment Script file.

我们将以下查询添加到“ 部署 脚本”文件中。

ALTER DATABASE TestDbProject_UnitTest SET TRUSTWORTHY ON;

SQL developer unit testing - Add a new setting to Post-Deployment  Script

Finally, we will publish our unit test project to the target server and then we enable to run the unit tests. Right-click into TestDbProject.UnitTest and then select Publish. Configure to target server and database and then click the Publish button.

最后,我们将单元测试项目发布到目标服务器,然后启用单元测试。 右键单击TestDbProject.UnitTest ,然后选择Publish 。 配置为目标服务器和数据库,然后单击“ 发布”按钮。

SQL developer unit testing - tSQLt test publish settings.

SQL developer unit testing - Publish result of the database project.

In the test explorer, we can find out the unit test so that we can run the test. Click the Run All in order to run unit tests.

在测试浏览器中,我们可以找到单元测试,以便我们可以运行测试。 单击运行 一切为了运行单元测试。

SQL developer unit testing - Run All unit tests in Test Explorer

结论 ( Conclusion )

In this article we discussed benefits of the SQL unit testing and we also integrated the tSQLt framework to SQL Server Database Project with the help of the tSQLt Test Adapter for Visual Studio 2017. I want to add a notice about tSQLt test adapter, this adapter is an open source project so that we can access the project code in GitHub.

在本文中,我们讨论了SQL单元测试的好处,并在Visual Studio 2017的tSQLt测试适配器的帮助下,还将tSQLt框架集成到SQL Server数据库项目中。我想添加有关tSQLt测试适配器的通知,一个开源项目,以便我们可以访问GitHub中的项目代码。

目录 (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/tsqlt-framework-and-sql-server-database-project-integration/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值