Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Unit Testing with tSQLt

Similar presentations


Presentation on theme: "Intro to Unit Testing with tSQLt"— Presentation transcript:

1 Intro to Unit Testing with tSQLt
Sandra Walters BTEK Software, Inc. Intro to Unit Testing with tSQLt

2 Agenda Why should I write unit tests? …SQL unit tests?! How?
Using tSQLt in SQL Management Studio The tSQLt unit test adapter for Visual Studio SSDT projects with tSQLt ReadyRoll projects with tSQLt CI with tSQLt tests and VSTS Code Coverage with SQL Cover

3 Bugs found by unit tests are the cheapest to fix
Source: . How Google Tests Software. Westford, MA: Addison-Wesley, 2012.

4 Why write unit tests? Helps team agree on “working as intended”
Instant feedback to the developer Does refactored code behave well? Defines expectations (when used with TDD)

5 We’ve all seen this meme, right. We’ve all worked with this guy
We’ve all seen this meme, right? We’ve all worked with this guy. Don’t be this guy.

6 What is tSQLt? Database unit testing framework
MS SQL 2005 SP2 and up, all editions Unit tests, and asserts, written in T-SQL Tests are run within transactions Tests are grouped within schemas Fake tables and views Stored procedure “spies”

7 Source:

8 New in SQL 2017 Server-level option: CLR strict security
(on by default) How to enable the tSQLt CLR to be installed: EXEC sp_configure ‘show advanced options’, 1 RECONFIGURE; EXEC sp_configure ‘clr strict security’, 0 NOT FOR SECURE / PRODUCTION ENVIRONMENTS… but then, you really shouldn’t be unit testing there anyway. Further reading:

9 Assert methods available in the tSQLt schema
AssertEmptyTable AssertEquals AssertEqualsString AssertEqualsTable AssertEqualsTableSchema AssertLike AssertNotEquals AssertObjectDoesNotExist AssertObjectExists AssertResultSetsHaveSameMetaData Fail

10 How to define tSQLt unit tests
Create a new test class (schema) tSQLt.NewTestClass ‘class name’ Create unit tests (stored procedures) in the test class; must start with the word ‘test’ CREATE PROC [ClassName].[Test something]…. Create a unit test setup (stored procedure) if necessary; must be named ‘Setup’ CREATE PROC [ClassName].[SetUp]…

11 How to run tSQLt unit tests
Run all tests tSQLt.RunAll Run a single unit test tSQLt.Run ‘TestName’ Run all tests in a test class tSQLt.Run ‘ClassName’

12 Using SQL Test

13 Sample Database: Wide World Importers
Source:

14 Demo tSQLt added to an existing database
Running tSQLt unit tests in SQL Management Studio Creating new tests

15 SSDT Projects with tSQLt
“Composite” Projects: Main database project Subordinate projects (such as tSQLt unit tests)

16 Unresolved references!
Subordinate projects: set the referred project’s location as “Same Database”

17 Running tSQLt Unit Tests in the VS IDE

18 Specify test SQL database connection in a runsettings file:

19 Test Explorer with tSQLt unit tests found

20 ReadyRoll Core in VS2017 Enterprise

21 Creating a ReadyRoll Core Project in VS2017
‘Wizard’-like walkthrough to create project from existing database:

22 Imported list of objects:

23 Continuous Integration with tSQLt
tSQLt Test Adapter must be added to the solution (3 possible ways) Place binaries directly on the build server in a folder accessible by the build agent Add the binaries to a solution folder within the Visual Studio solution Add the NuGet package for the test adapter to any project within the solution that can accept NuGet packages (no SSDT or ReadyRoll projects)

24 At a minimum, the build must:
Push the revised unit tests to a SQL database Run the unit tests on the database

25 Pushing unit tests to a SQL database via a
Command Line task with SqlPackage

26 Running the unit tests against a SQL server during the build
At a minimum, set these items: Test assemblies Settings file Path to custom test adapters

27 Results from the test task – Success!

28 Code Coverage with SQL Cover
Source:

29 SQLCover uses the SQL Server API to determine what is “coverable”

30 Why use code coverage? Source:

31

32 tSQLt is found at: http://tsqlt.org/
Blog articles based on this talk: Article from the tSQLt creators: unit-testing-with-tsqlt/ Author of the Visual Studio tSQLt Test Adapter and SQLCover: RedGate – introduction to SQL Cover: Microsoft's Wide World Importers sample database (and others): importers-v1.0

33 Thank you Sponsors


Download ppt "Intro to Unit Testing with tSQLt"

Similar presentations


Ads by Google