Intro to Unit Testing with tSQLt

Slides:



Advertisements
Similar presentations
Entity Framework Code First Migrations
Advertisements

#sqlsatPordenone #sqlsat367 February 28, 2015 Testing your databases Alessandro
Client Deployment Strategies
Virtual techdays INDIA │ august 2010 Building ASP.NET applications using SQL Server Compact Chaitanya Solapurkar │ Partner Technical Consultant,
Continuous Integration for Databases Learn how to automate your build and test Steve Jones Red Gate Software Part II of the Continuous Delivery for Databases.
DONE-10: Adminserver Survival Tips Brian Bowman Product Manager, Data Management Group.
Sumedha Rubasinghe October,2009 Introduction to Programming Tools.
UNIT TESTING FOR SQL Prepared for SUGSA CodeLabs Alain King Paul Johnson.
De-mystifying TFS Reporting Benjamin Day Level: Intermediate.
StyleCop Breaking down the barriers to entry Gary Ewan Park Twitter: Blog:
Source Code Control CSE 3902 Matt Boggus. Source code control options for CSE 3902 Must use source code control that is integrated with Visual Studio.
Learningcomputer.com SQL Server 2008 – Administration, Maintenance and Job Automation.
Experiment Management System CSE 423 Aaron Kloc Jordan Harstad Robert Sorensen Robert Trevino Nicolas Tjioe Status Report Presentation Industry Mentor:
ADAPTING YOUR ETL SOLUTION TO USE SSIS 2012 Presentation by Devin Knight
Understanding StarTeam Enterprise Advantage Course #4124.
Isolated Database Environments Kevin Howell February 2014.
Database Projects in Visual Studio Improving Reliability & Productivity.
Visual Studio 2012: A Complete IDE (Debugging & Source Control) Kevin Howell.
SSMS SQL Server Management System. SQL Server Microsoft SQL Server is a Relational Database Management System (RDBMS) Relational Database Management System.
YOUR LOGO Phase 2 International Providing critical business software solutions at affordable prices.
Managing Database objects in Visual Studio and Team Foundation Server Presented by Sean P SQL Saturday - Phoenix 2016.
Developing SQL/Server database in Visual Studio Introducing SQL /Server Data Tools Peter Lu.Net Practices Director Principle Architect Nexient March 19.
Get testing with tSQLt Practical examples and automation Steve Jones SQLServerCentral Red Gate Software.
Upgrade on Windows 7. DownloadSoftware Download Software from link provided in Webliography: e/
Template Package  Presented by G.Nagaraju.  What is Template Package?  Why we use Template Package?  Where we use Template Package?  How we create.
Others Talk, We Listen. Managing Database Projects in Visual Studio 2013.
Confidential ASWM Installation Guide in Main Server ( for Windows Server 2008 R2 ) 1.
ClickOnce Deployment (One-click Deployment)
DBMS Programs MS SQL Server & MySQL
DevOps with ASP.NET Core and Entity Framework Core
How to be a SharePoint Developer
Data Virtualization Demoette… ODBC Clients
Explore the Integration Services Catalog
Effective T-SQL Solutions
Visual Studio Database Tools (aka SQL Server Data Tools)
SQL Server deployments
Making Your List and Checking It Twice
Introduction to unit and integration testing with tSQLt
Data Virtualization Demoette… ADO.NET Client
Beyond the BDC\BCS Model
Data Virtualization Tutorial… OAuth Example using Google Sheets
Data Virtualization Demoette… JDBC Clients
Visual Studio Tools for Office 2005
Automated and Repeatable Test Cases for SQL Server Development
Data Virtualization Community Edition
SQL Server Client Tools 2018
SQL Server Data Tools Gert Drapers
Continuous Integration For Databases
Database Code Management with VS 2017 and RedGate
Principles of report writing
Test-Driving Your Database
Prove to your boss your database is sound - Unit Testing with tSQLt
Visual Studio Database Tools (aka SQL Server Data Tools)
Bringing DevOps to the Database
DAT381 Team Development with SQL Server 2005
Visual Studio 2005 Tools For Office: Creating A Multi-tier Application
Your code is not just…your code
Presented by : Chirag Dani & Dhaval Shah
SSDT and Database Project Basics
Introduction to VSTS Database Professional
These slides are for reference only. They are not "lecture notes"
Implementing Entity Framework with MVC Jump Start
SSDT, Docker, and (Azure) DevOps
Michelle Haarhues Keeping up with SSMS.
ClickOnce Deployment (One-click Deployment)
SSDT, Docker, and (Azure) DevOps
MS Confidential : SharePoint 2010 Developer Workshop (Beta1)
SSDT, Docker, and (Azure) DevOps
Bringing source control to BI world!
Your code is not just…your code
Presentation transcript:

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

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

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

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)

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.

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”

http://tsqlt.org Source: https://github.com/tSQLt-org/tsqlt

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: https://github.com/tSQLt-org/tSQLt/issues/25

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

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]…

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’

Using SQL Test

Sample Database: Wide World Importers Source: https://github.com/Microsoft/sql-server-samples

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

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

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

Running tSQLt Unit Tests in the VS IDE

Specify test SQL database connection in a runsettings file:

Test Explorer with tSQLt unit tests found

ReadyRoll Core in VS2017 Enterprise

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

Imported list of objects:

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)

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

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

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

Results from the test task – Success!

Code Coverage with SQL Cover Source: https://github.com/GoEddie/SQLCover

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

Why use code coverage? Source: https://martinfowler.com/bliki/TestCoverage.html

tSQLt is found at: http://tsqlt.org/ Blog articles based on this talk: http://blog.bteksoftware.com/   Article from the tSQLt creators: https://www.red-gate.com/simple-talk/sql/t-sql-programming/sql-server- unit-testing-with-tsqlt/ Author of the Visual Studio tSQLt Test Adapter and SQLCover: https://the.agilesql.club/blogs/Ed-Elliott/ RedGate – introduction to SQL Cover: https://www.red-gate.com/blog/sql-cover Microsoft's Wide World Importers sample database (and others): https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world- importers-v1.0

Thank you Sponsors