Академический Документы
Профессиональный Документы
Культура Документы
by Rgis Baccaro
This is the second part on the serie about SSDT and database development
Part I: Continuous integration with SQL Server Data Tools and Team Foundation Server
Introduction
In December 2012, a great addition was made to SSDT: The ability to do database unit
testing.
However, this feature is not available on the free edition of SSDT. For doing unit test
with SSDT you will at least need a Visual Studio Professional edition or above.
As you and the team members make changes to the database schema you can verify
that these changes are working as expected or if they have broken existing
functionality.
Please bear over with me making so many screen dumps but I was so fortunate to get a
license of SnagIt from TechSmith so I am also trying the possibilities of the tool in this
blog post.
Proceeding
Usually you will want to baseline your database and then run some unit tests on the
changes that you have made. Personally, I have made a habit out of taking a snapshot
of the database I am working with at different stages and at least once a week. This way
I can always go back to the state it was before I made the subsequent changes without
having to roll back changes with TFS.
Because unit testing of your database is just a Unit Test Project, you can create and
run database unit test without a database project. However, the only way to autogenerate tests for specific database objects is to use the database project.
When creating a test project from a database project Visual Studio will automatically
generate some of classes for you. Most of the plumbing will be done that way. The most
important class in that regard is:
Microsoft.Data.Tools.Schema.Sql.UnitTesting;
When checking for updates you also have the possibility to let Visual Studio do it
automatically for you by selecting automatically check for updates to SQL Server
Data Tools.
Each time you hit F5, SSDT will deploy your database to your LocalDB. By the way, it is
possible to change this behavior by going to the Debug tab of the projects property
page and change the connection string there so your database gets deployed
somewhere else than in this local instance.
In the window that shows next, you can chose if you want to create a VB.Net or a C#
test project as well as a list of all the database object that support unit testing and the
class name for the test file being created.
I chose C# and after project creation, you are presented with a SQL Server Test
Configuration dialog where you can specify which database to run the test on even a
secondary connection to validate those tests as well as the option to deploy the
database prior to you run your tests.
Remark : If you must test views or stored procedures that have restricted permissions,
you would typically specify that connection in this step. You would then specify the
secondary connection, with broader permissions, to validate the test. If you have a
secondary connection, you should add that user to the database project, and create a
login for that user in the pre-deployment script.
In my case I want to run my tests on the database I deployed earlier This is how my
setup looks like:
If you look at the solution explorer you will see that following was created:
A test project
A SqlDatabaseSetupFile
A SQLUnitTest class
The app.config of the project contains the settings for deployment and database
connection
6
7
FROM dbo.Customer AS c
Group BY c.CustomerId
1
1
OPEN curCustomer
2
1
WHILE @@FETCH_STATUS = 0
BEGIN
6
1
UPDATE Customer
7
1
8
1
9
2
0
2
1
2
2
2
SET RankingId = @RankingId
3
WHERE CustomerId = @CustomerId
2
4
FETCH NEXT FROM curCustomer INTO @CustomerId, @OrderTotal
2
END
5
2
CLOSE curCustomer
6
DEALLOCATE curCustomer
2
7
2
8
2
9
3
0
3
1
3
2
Test: For my existing customers I want to update the ranking using the Ranking table.
1
2
-- database unit test for dbo.uspRankCustomers
3
DECLARE @RC AS INT;
4
5
SELECT @RC = 0;
6
7
-- execute the stored procedure
8
EXECUTE @RC = [dbo].[uspRankCustomers] ;
9
1
-- select the customer ids without any ranking (should be 0)
0
SELECT @RC = CustomerID from Customer where RankingID IS NULL
1
1
SELECT @RC AS RC;
1
2
In Test conditions delete the automatically created condition and create a new one.
Change its type to Scalar Value and in the property window make sure the expected
value is 0.
If you remember the previous post in this series, I made it possible to do continuous
deployment with MSBuild and Team Foundation Server. I showed how you are able to
build and deploy your database changes for each check-in. Well, it is also possible to
have this procedure run the test for you. This is useful for running automated tests and
analyze the impact of code changes on your tests as part of your build process.
But for now well disable it by removing it in the Build Definition file. On the left pane
choose Process and Click the ellipsis on the right side of Automated Test and
click Remove in the dialog that shows !!
Running test as a part of your Build Process will be the subject of upcoming blog posts,
so stay tuned for more SSDT and unit testing fun!!
Test
Condition
Data
Checksum
Fails if the checksum of the result set returned from the Transact-SQL script does not match the e
Note
This test condition is not recommended if you are returning data that will vary between test runs
the checksum will be different on each run.
Empty
ResultSet
Fails if the result set returned from the Transact-SQL script is not empty.
Execution
Time
Fails if the Transact-SQL test script takes longer than expected to execute. The default execution
The execution time applies to the test script test only, not to the pre-test script or the post-test scri
Expected
Schema
Fails if the columns and data types of the result set do not match those specified for the test condi
see Specifying an Expected Schema.
Inconclusi
ve
Always produces a test with a result of Inconclusive. This is the default condition added to every
condition from your test after you have added other test conditions.
Not Empty
ResultSet
Fails if the result set is empty. You can use this test condition or the EmptyResultSet with the Tran
example, you can save pre-update values, run the update, compare post-update values, and raise a
Row Count
Fails if the result set does not contain the expected number of rows.
Scalar
Value
Fails if a particular value in the result set does not equal the specified value. The default Expected
http://www.sqlshack.com/sql-server-business-intelligence-features-creating-reportsbased-olap-cubes/