Database unit testing with Red Gate SQL Tools



Similar documents
Continuous integration for databases using

Improving database development. Recommendations for solving development problems using Red Gate tools

Continuous integration for databases using Redgate tools

SQL DBA Bundle. Data Sheet. Data Sheet. Introduction. What does it cost. What s included in the SQL DBA Bundle. Feedback for the SQL DBA Bundle

Continuous integration for databases using Red Gate tools

WHITEPAPER. Improving database development

Best Practices for Implementing Autodesk Vault

Sage Intelligence Financial Reporting for Sage ERP X3 Version 6.5 Installation Guide

Active Directory Management. Agent Deployment Guide

Automated backup. of the LumaSoft Gas database

Essential Visual Studio Team System

Moving the TRITON Reporting Databases

Implementing Microsoft Azure Infrastructure Solutions 20533B; 5 Days, Instructor-led

Course 20533B: Implementing Microsoft Azure Infrastructure Solutions

How To Create A Replica In A Database On A Microsoft Powerbook (Ahem) On A Linux Server (A.K.A.A)

ATTACHMENT 6 SQL Server 2012 Programming Standards

How to set up SQL Source Control. The short guide for evaluators

Chancery SMS Database Split

Introduction to the Data Migration Framework (DMF) in Microsoft Dynamics WHITEPAPER

Leverage SharePoint with PSI:Capture

Backup / migration of a Coffalyser.Net database

Comparing Microsoft SQL Server 2005 Replication and DataXtend Remote Edition for Mobile and Distributed Applications

Hands-On Lab. Embracing Continuous Delivery with Release Management for Visual Studio Lab version: Last updated: 12/11/2013

Table of Contents SQL Server Option

Moving/Restoring the StarShip SQL database

A Tutorial on SQL Server CMPT 354 Fall 2007

Getting to Know the SQL Server Management Studio

Synchronizing databases

Insight Video Net. LLC. CMS 2.0. Quick Installation Guide

Learning SQL Data Compare. SQL Data Compare - 8.0

Implementing Microsoft SQL Server 2008 Exercise Guide. Database by Design

SQL SERVER FREE TOOLS

Cloud Services ADM. Agent Deployment Guide

Michael Noel. Colin Spence. SharePoint UNLEASHED. 800 East 96th Street, Indianapolis, Indiana USA

Nick Ashley TOOLS. The following table lists some additional and possibly more unusual tools used in this paper.

AWS Schema Conversion Tool. User Guide Version 1.0

We ( have extensive experience in enterprise and system architectures, system engineering, project management, and

PassTest. Bessere Qualität, bessere Dienstleistungen!

RFMS, INC. Reference Library Documentation. Version 10 Conversion Manual. Microsoft SQL

Vladimir Bakhov AT-Consulting +7 (905)

AVALANCHE MC 5.3 AND DATABASE MANAGEMENT SYSTEMS

Author: Ryan J Adams. Overview. Policy Based Management. Terminology

MIS0094 SuccessNet Upgrade Guide. Individuals Implementing SuccessNet. To assist in the upgrade of the software to version

SQL Server Training Course Content

INTEGRATING MICROSOFT DYNAMICS CRM WITH SIMEGO DS3

Getting Started with the Ed-Fi ODS and Ed-Fi ODS API

Embarcadero DB Change Manager 6.0 and DB Change Manager XE2

Agile Business Suite: a 4GL environment for.net developers DEVELOPMENT, MAINTENANCE AND DEPLOYMENT OF LARGE, COMPLEX BACK-OFFICE APPLICATIONS

Using EMC Documentum with Adobe LiveCycle ES

SonicWALL CDP 5.0 Microsoft Exchange InfoStore Backup and Restore

How To Upgrade Your Microsoft SQL Server for Accounting CS Version

SQL Server Instance-Level Benchmarks with DVDStore

About database backups

Schematron Validation and Guidance

Implementing and Maintaining Microsoft SQL Server 2008 Integration Services

Migrate Topaz databases from One Server to Another

Postgres Plus xdb Replication Server with Multi-Master User s Guide

SQL Server Replication Guide

D83167 Oracle Data Integrator 12c: Integration and Administration

MICROSTRATEGY 9.3 Supplement Files Setup Transaction Services for Dashboard and App Developers

Reinventing data migration process

Active Directory Management. Agent Deployment Guide

How to Back Up and Restore an ACT! Database Answer ID 19211

Smarter Balanced Assessment Consortium. Recommendation

ilaw Installation Procedure

RDS Migration Tool Customer FAQ Updated 7/23/2015

Migrating helpdesk to a new server

IBM Campaign Version-independent Integration with IBM Engage Version 1 Release 3 April 8, Integration Guide IBM

Course 10978A Introduction to Azure for Developers

Creating a universe on Hive with Hortonworks HDP 2.0


Data processing goes big

Moving the Web Security Log Database

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

Programa de Actualización Profesional ACTI Oracle Database 11g: SQL Tuning Workshop

DB2 Application Development and Migration Tools

Course 20533: Implementing Microsoft Azure Infrastructure Solutions

Report and Dashboard Template User Guide

SonicWALL CDP 5.0 Microsoft Exchange User Mailbox Backup and Restore

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Skynax. Mobility Management System. System Manual

MS 10978A Introduction to Azure for Developers

Table of Contents. CHAPTER 1 About This Guide CHAPTER 2 Introduction CHAPTER 3 Database Backup and Restoration... 15

Visual Studio.NET Database Projects

How to Copy A SQL Database SQL Server Express (Making a History Company)

Amman Jordan Mob: Tel:

EnterpriseLink Benefits

Migrating MSDE to Microsoft SQL 2005 Express SP4

Technical Bulletin. SQL Express Backup Utility

Lab 2: PostgreSQL Tutorial II: Command Line

POLICY PATROL MFT. Manual

IBM DB2 XML support. How to Configure the IBM DB2 Support in oxygen

OneStop Reporting 3.7 Installation Guide. Updated:

IQSweb Reference G. ROSS Migration/Registration

How to Set Up a Shared SQL Express Database with ManagePro 7 Standard version

Kony MobileFabric. Sync Windows Installation Manual - WebSphere. On-Premises. Release 6.5. Document Relevance and Accuracy

How to protect, restore and recover SQL 2005 and SQL 2008 Databases

Preparing a Windows 7 Gold Image for Unidesk

Preparing a SQL Server for EmpowerID installation

Transcription:

Database unit testing with Red Gate SQL Tools - 1 -

Contents Introduction Test data generation Example: writing a database unit test Conclusions Introduction This article examines some of the challenges of integrating databases with the development cycle, and describes how a combination of Red Gate tools and common unit testing frameworks can be used to improve development quality. This introduction addresses the following questions: What is unit testing? How are databases different? How can Red Gate tools help? What is unit testing? Unit testing aims to reduce errors in final application code by separating and evaluating the smallest meaningful units of testable software, for example an individual function. Each such unit is tested individually, giving you confidence in your code at a granular level, and making it easier to diagnose errors that arise in more complex integration testing. Additionally, unit testing facilitates continuous integration, agile development, and test driven development (where the final functionality of a piece of software is outlined as a set of test conditions, and code is then written to satisfy those tests). Unit tests are typically run locally on a developer s machine once code has been written, to ensure that it satisfies requirements before being committed to source control, and shared with the team, or passed on for QA. They may then be run again as part of a continuous integration or automated build process. For more information on continuous integration, see Continuous integration for databases using Red Gate SQL tools (.pdf) - 2 -

How are databases different? Because SQL is declarative, and much database code describes relational structures, the idea of a testable unit is not always relevant. DDL statements modify the current state of the database, receiving instant validation. So an improperly defined object will be rejected by SQL Server. Modern databases typically contain more than pure data. For performance or organizational reasons, a varying amount of business logic may be held in the database, for example as functions, stored procedures, or CLR objects. Many objects have and require metadata, and relatively simple changes can impact referential integrity. Individual units of procedural code must be tested, to ensure they function as required. Testing as part of a continuous integration process is especially hindered as databases are rarely source controlled, and so cannot be deployed alongside application code as part of the build process. More generally, database deployment is already a significant development bottleneck. It typically requires the manual creation of migration scripts. Once deployed, a database must then be populated with any data required for testing. This is also frequently a manual scripting operation, or may rely on performing a time-consuming restore from a backup. So even when a single developer is locally unit testing their own changes, the process may not be simple. - 3 -

How can Red Gate tools help? Red Gate tools work together with common unit testing frameworks, so you can automatically deploy a database, populate it with realistic data, and run unit tests. Red Gate offers the following tools for automating database development: SOURCE CONTROL Source controls database schema within SQL Server Management Studio. Compares and synchronizes database schema. Compares and synchronizes database data. A bundle of APIs for our comparison tools, accessible via C# or Visual basic. Generates realistic test data based on your database schema. The professional editions of SQL Compare and SQL Data Compare enable you to create, compare, and synchronize folders of SQL scripts representing a database s schema and data. The process is rapid, accurate, and - crucially - can be automated using the command line interface. For more flexibility, you can also automate deployments with the SQL Comparison SDK. If desired, a representation of the database can therefore be created and automatically maintained in a source control repository without manual intervention. SQL Source Control builds on this technology. It is an add-in for SQL Server Management Studio that source controls your database schema in either Subversion or Microsoft s Team Foundation Server, within the IDE. This makes database source control simple to set up and use. - 4 -

The development process with Red Gate tools, including unit testing, was demonstrated at the 6th SQL Bits conference in April 2010. You can see a repeat of that demonstration here. Test data generation Testing often requires a database to be populated with known test data. For instance, a test may verify that a stored procedure returns the expected results after a schema change. It is not always appropriate to use production data in a development environment, and manually creating test data is a monotonous, time-consuming task. Red Gate offers two solutions to the problem of test data population: migrating existing data with SQL Data Compare, and creating realistic test data with SQL Data Generator. This article gives some more detail on configuring SQL Data Generator for use with unit testing. Using SQL Data Generator SQL Data Generator has a simple graphical user interface that allows you to choose the type of data you want to generate for each table and column: - 5 -

SQL Data Generator automatically assigns a generator to each column based on its table name, column name, data type, and length. If the column has constraints, SQL Data Generator uses these to set the generator parameters for the column. You can change the generator used by a particular column later if required. Alternatively, you can create your own regular expression, or import data from an existing table. Automating data generation You can save your data generation settings as a SQL Data Generator project file (.sqlgen). The project file can be used with the SQL Data Generator command line interface: cd c:\program files (x86)\red gate\sql data generator 1 sqldatagenerator /project: c:\<location>\project.sqlgen You can then create a method in your unit test that the uses the project via the command line interface to generate the data you require. - 6 -

For example, the following C# code defines a DataGenerator.PopulateData() method which can then be used in a test by supplying the name of a.sqlgen project file: internal class DataGenerator /// <summary> /// Use SQL Data Generator to populate a table from a project file. /// </summary> /// <param name= projectfilename ></param> static internal void PopulateData(string projectfilename) //Generate the data into Person.Contact table ProcessStartInfo startinfo = new ProcessStartInfo(); //Point at the SQL Data Generator application startinfo.filename = @ C:\Program Files\Red Gate\SQL Data Generator 1\SQLDataGenerator.exe ; //Specify the SQL Data Generator project file to use startinfo.arguments = String.Format(@ /project: C:\DB Unit Testing Demo\DataGenerator\0}, projectfilename); startinfo.createnowindow = true; startinfo.useshellexecute = false; } } } //Start the process with the info we specified and wait for SQL Data //Generator to finish using (Process datageneratorprocess = Process.Start(startInfo)) datageneratorprocess.waitforexit(); } - 7 -

Example: writing a database unit test This is a brief example of constructing a database unit test in C# with the NUnit framework. It describes connecting to a database, querying a system view, and asserting the expected results. For more information, see the NUnit quick start guide. The complete code example used here is available as a downloadable zip file. Connecting to the database We encapsulate all the required connection information into one object. This makes it simple to modify the connection details. For example, here we are connecting to a local SQL Server instance using Windows authentication, but you may want to use a different server, or include a user name and password. We create an instance of SQLConnectionStringBuilder: testconn = new SqlConnectionStringBuilder IntegratedSecurity = true, DataSource = @.\USER, InitialCatalog = TestDB }; The connection can now be used to run tests. We do this with a using statement, to ensure the connection is disposed of properly after the test is complete. For example: using (SqlConnection connection = new SqlConnection(testconn.ToString())) //Test code goes here } - 8 -

Querying the database Normal T-SQL can be run within the using statement. We will query a system view, and ensure that a table in the database called Names has the expected number of columns. We use the following SQL query: SELECT count(column_name) from Information_schema.columns where table_name = Names There are several ways to run this query. Here, we use SqlCommand.ExecuteScalar() which returns the value from the first row of the first column in the results set. This is all we need for a simple test. You could also return and manipulate a whole results set. We use the following test code: SqlCommand command = new SqlCommand(query, connection); connection.open(); object reader = command.executescalar(); This returns the result to the object reader. Verifying results To check the test results is as expected, we write an assertion using NUnit. In this example, we expect the table to have three columns, and so the expected result is 3. Because the result is returned as an object, it must then be parsed into an integer. We use the following code: Assert.That(Int32.Parse(reader.ToString()), Is.EqualTo(3)); The complete code example used here is available as a downloadable zip file. - 9 -

Further tests The test in this example could be used to check changes that other developers have made. However, it is very simple and would fail often in an actual development environment. More realistic unit tests might cover: Permissions and security For example, can users in a given role access the stored procedures, tables, views, and so on that they require. These tests could attempt to query relevant objects, and check for errors using NUnit s Throws Constraint. Triggers and constraints For example, does a trigger or constraint function correctly when specific data is entered. This can be automated by running SQL Data Generator as part of the test. Consistency For example, does a stored procedure run and return data as expected. This can be tested with a small set of known data or by running SQL Data Generator as part of the test. Conclusions Red Gate SQL tools substantially simplify the path to adopting database unit testing, and so to raising code quality. The tools described in this paper work together with NUnit, XUnit, MbUnit, or any other unit testing framework. This offers flexibility, and does not require that you change the tools you currently use for unit testing. The Red gate tools discussed in this article can be accessed via their command line interfaces, and for more configurability you can access the SQL comparison APIs that comprise our SDK. NUnit, XUnit, and MbUnit are all available as free, open-source downloads. All Red Gate tools are available as a free, fully-functional 14 day trial. - 10 -

Getting more information To find out more about improving database development and unit testing, see: Using SQL Data Generator with your Unit Tests A Simple-Talk.com article by Ben Hall Testing Times Ahead: Extending NUnit Ben s follow-up article on expanding SQL unit tests SQL Source Control demonstration video A repeat of the demonstration given at SQL Bits VI, showing automated unit tests in action Improving database development with Red Gate SQL tools (.pdf) Our overview paper on database development Continuous integration for databases using Red Gate SQL tools (.pdf) Our guide to implementing continuous integration and source control - 11 -