Top Banner
xUnit Style Database Unit Testing ACCU London – 20 th January 2011 Chris Oldwood [email protected]
21

xUnit Style Database Unit Testing

Mar 19, 2016

Download

Documents

Sef

xUnit Style Database Unit Testing. ACCU London – 20 th January 2011 Chris Oldwood [email protected]. Presentation Outline. Database Development Process The xUnit Testing Model Test First Development Continuous Integration/Toolchain Pub. Legacy Database Development. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: xUnit Style Database Unit Testing

xUnit Style Database Unit Testing

ACCU London – 20th January 2011

Chris Oldwood

[email protected]

Page 2: xUnit Style Database Unit Testing

Presentation Outline

• Database Development Process• The xUnit Testing Model• Test First Development• Continuous Integration/Toolchain• Pub

Page 3: xUnit Style Database Unit Testing

Legacy Database Development

• Shared development environment• Only integration/system/stress tests• No automated testing• Only real data not test data• Referential Integrity – all or nothing• No automated build & deployment

Page 4: xUnit Style Database Unit Testing

Ideal Development Process

• Isolation• Scaffolding• Automation

Page 5: xUnit Style Database Unit Testing

Example Testable Behaviours

• Default constraint• Trigger to cascade a delete• Refactoring to a surrogate key

Page 6: xUnit Style Database Unit Testing

NUnit Test Model[TestFixture]public class ThingTests{ [Test] public void Thing_DoesStuff_WhenAskedTo() { var input = ...; var expected = ...;

var result = ...;

Assert.That(result, Is.EqualTo(expected)); }}

Page 7: xUnit Style Database Unit Testing

NUnit Test Runner

• Tests packaged into assemblies• Uses reflection to locate tests• In-memory to minimise residual effects• Output to UI/console

Page 8: xUnit Style Database Unit Testing

SQL Test Modelcreate procedure test.Thing_DoesStuff_WhenAskedToas declare @input varchar(100) set @input = ...

declare @expected varchar(100) set @expected = ...

declare @result varchar(100) select @result = ...

exec test.AssertEqualString @expected, @result go

Page 9: xUnit Style Database Unit Testing

SQL Test Runner

• Tests packaged into scripts (batches)• Uses system tables to locate tests• Uses transactions to minimise residual

effects• Output to UI/console

Page 10: xUnit Style Database Unit Testing

SQL Asserts

• Value comparisons (string, datetime, …)• Table/result set row count• Table/result set contents• Error handling (constraint violations)

Page 11: xUnit Style Database Unit Testing

Setup & Teardown

• Per-Fixture (static data)• Per-Test (specific data)• Use helper procedures

Page 12: xUnit Style Database Unit Testing

Default Constraint Testcreate procedure test.AddingTask_SetsSubmitTimeas declare @taskid int declare @submitTime datetime

set @taskid = 1

insert into Task values(@taskid, ...)

select @submitTime = t.SubmitTime from Task t where t.TaskId = @taskid

exec test.AssertDateTimeNotNull @submitTime go

Page 13: xUnit Style Database Unit Testing

Trigger Testcreate procedure DeletingUser_DeletesUserSettingsas ... set @userid = 1

insert into AppUser values(@userid, ...) insert into AppUserSettings values(@userid, ...)

delete from AppUser where UserId = @userid

select @rows = count(*) from AppUserSettings where UserId = @userid

exec test.AssertRowCountEqual @rows, 0 go

Page 14: xUnit Style Database Unit Testing

Unique Key Test

create procedure AddingDuplicateCustomer_RaisesErroras ... insert into Customer values(‘duplicate’, ...)

begin try insert into Customer values(‘duplicate’, ...) end try begin catch set @threw = 1 end catch

exec test.ErrorRaised @threwgo

Page 15: xUnit Style Database Unit Testing

Automation

• Enables easy regression testing• Enables Continuous Integration• Performance can be variable

Page 16: xUnit Style Database Unit Testing

Test First Development

• Start with a requirement• Write a failing test• Write production code• Test via the public interface

Page 17: xUnit Style Database Unit Testing

The Public Interface

• Stored procedures• Views• Tables?

Page 18: xUnit Style Database Unit Testing

Implementation Details

• Primary keys• Foreign keys• Indexes• Triggers• Check constraints• Default constraints

Page 19: xUnit Style Database Unit Testing

Deployment Testing

Build version N+1then run unit tests

Build version Nthen patch to N+1then run unit tests

==

Page 20: xUnit Style Database Unit Testing

Buy or Build?

• Batch file, SQL scripts & SQLCMD• TSQLUnit & PL/Unit• Visual Studio• SQL Server/Oracle Express

Page 21: xUnit Style Database Unit Testing

“The Oldwood Thing”http://chrisoldwood.blogspot.com

Chris [email protected]