Checking out dbForge’s Unit Test for SQL Server
Tags: devart, sqlserver, database
Published: 2022-06-06 13:05:01

In the coding/software development space, you have likely heard, at least once, the term “unit test”. Let’s take a look at a definition of a unit test for software development: 

_Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done by software developers and sometimes QA staff during the development process.  The main objective of unit testing is to isolate written code to test and determine if it works as intended._

But, how does that translate to the database development space? Let’s see if Unit Test for SQL Server answers that for us.

**Installation**
You can get Devart’s [SQL Unit Testing](https://www.devart.com/dbforge/sql/unit-test/download.html) tool by clicking on the “Get Trial” button. Doing so will also include the whole suite of SQL Server Management Studio extensions, which adds a lot of extra value.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx0pfm9melvbnslnt3fj.png) 
 
_**Note:** Make sure you have installed SQL Server Management Studio 2012 or higher to proceed._

After you have completed the installation, head straight to SQL Server Management Studio and open it. Once opened, establish a connection to a SQL Server instance and go to any of your databases, and right-click on it.

This is what you’ll be seeing:
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3rlu9dilg0xfem092vd3.png)
 
Since it’s our very first time checking Unit Test, then let’s go straight to “Add New Test”:

You’ll immediately see this, so let me fill requested information to keep moving forward.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpga58zafs9x9pkpch28.png)
 
_**Important note:** the tool will check if tSQLt framework is installed and will warn you that the unit test will not be created if that’s not the case, so let me go ahead and install it then._

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4bbnl6edasrdkeovqyv.png)  

This is what we get after hitting “Yes”:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jzdwywnm4oq4unb19gt.png)  

So the block of code you see is for altering a stored procedure, so I guess that your unit test will be baked inside this stored procedure. If I take a look at the stored procedures inside my database, I can see a bunch with the prefix “tSQLt”, so now we know why the tool insisted on the installation then.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7i9mblqsjejvot80owt.png)  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5hhrur9hxqp3vvjs1wyc.png) 
 
If we take a look at the comments within that block of code, we can see that it has 3 sections:

**Assemble:** This section is for code that sets up the environment. It often contains calls to methods such as tSQLt.FakeTable and tSQLt.SpyProcedure, along with INSERTs of relevant data.

**Act:** Execute the code under test like a stored procedure, function, or view and capture the results in variables or tables.

**Assert:** Compare the expected and actual values, or call tSQLt.Fail in an IF statement.

If I go ahead and execute the stored procedure exactly as it is, this is what I get:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3lp669u4tg4cnwe0bh8.png)
  
Since it’s my first attempt at something like this, I have no idea how to proceed, but lucky for me, there are informative links right in the stored procedure template that I’m going to check out before I’m able to figure out what to do next.

Let me give you a tip if you simply don’t know where to start: after you install Unit Test, a new menu option will be made available right there in SQL Server Management Studio, called “Unit Test”, and this will allow you to install a sample database that has some test cases baked in for you to look around and get a feeling of how things work:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owv6zpmeg7w93im1nubh.png)  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqgffmzue8isupilvstf.png) 

Now, if instead of clicking “Install Sample Database” you click “View Test List”, then this is what you will see:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubmqd8qn8zcyvtgv1zqm.png)  

These are all the available tests within the sample database, and if you click the icon being pointed by the green arrow, then you will run all of them, so let’s check those out.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yandlirpimzgkn2zs8k.png)  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbkh0i5m4nw236gaxg4w.png) 

Ok, so this gives me the outcome of each test so that I can see which ones succeeded and which failed. But what if I want to take a look at a failed test to address whatever it is that I have to address? How can I do that? Simply right-click on the row with the failed test and click “Open Test”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/evbb2yyswahrsujijvln.png)  

With the stored procedure open that contains the failed test, I’m going to tamper with it just a bit to force a successful test case, just to confirm that’s how that works:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gcwthe66i2xomdh0xey.png)
  
And yes, that worked like a charm, cool!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhtrrxv3zqj8qujxrngm.png)  

Let me continue with the one I originally tried to make:

This is a very basic example, so I will save it in my stored procedure and try to manually execute it without using the GUI that I showed previously.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpbhil7p2lgustck6qrl.png)  

So if I try to execute my test case stored procedure directly, then I’m getting this weird error.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/viadzw6mas0811apwwe6.png)  

But guess what? To test it properly I have to run the stored procedure called tSQLt.Run and pass my stored procedure as a parameter, just like this:

As you can see, it will tell me that my test case failed because “Expected: <5> but was: <10>”. I also get a fancy table with the summary of the test case, and I assume that as you add more test cases, this table will also grow if you specify to run the entire class instead of an individual stored procedure test case.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qlw85s74i695sfxkaaq5.png)  

Now let me go to my stored procedure and change that 5 for a 10 and run it again to see if I get a different output:

Bingo, it worked! So I have shown you how to run unit tests manually and using the GUI.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eps6v13t1ny3fmpwx7ho.png)  

**Final thoughts**
●	Obviously, this was just a very brief look at the tool, but I encourage you to continue to dig deeper by checking Devart’s official [documentation center](https://docs.devart.com/unit-test/).

●	After looking at what the tool offers, I think we can safely say that the definition we saw earlier, on unit tests in the software development space, matches in the database development space. With that said, I see this as a precious tool for database developers to raise the bar in the quality department.

●	Suppose you are currently not covering unit tests within your database development workflow. In that case, it goes without saying that it is something that takes time to build for your particular environment. However, once you get a good grasp on it, it is worth it!