SqlTest 2.0.3

dotnet add package SqlTest --version 2.0.3
NuGet\Install-Package SqlTest -Version 2.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SqlTest" Version="2.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SqlTest --version 2.0.3
#r "nuget: SqlTest, 2.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SqlTest as a Cake Addin
#addin nuget:?package=SqlTest&version=2.0.3

// Install SqlTest as a Cake Tool
#tool nuget:?package=SqlTest&version=2.0.3

About

SqlTest is a C# library which provides scaffolding to write tests for SQL Database projects.

Key Uses

  • Incorporate with NUNIT or other test frameworks
  • Write database unit tests / destructive with rollback
  • Mock Tables, Views and functions
  • Generate test data
  • Setup and teardown for integration testing

Getting Started

Create a .net core class library. Install SqlTest nuget package: (https://www.nuget.org/packages/SqlTest/). Optionally, add the following for using the nunit test harness:

Add an appsettings.json file and set the Copy to output directory property to Copy always. The file should be in the following form to set connection properties for the target database:

{
  "Targets": {
    "TestDb": {
      "ServerName": "localhost",
      "Databasename": "TestDb",
      "IntegratedSecurity": true,
      "TrustServerCertificate": true
    },
    "SqlLogin": {
      "ServerName": "localhost",
      "Databasename": "TestDb",
      "User": "SqlUser",
      "Password": "<some password>",
      "TrustServerCertificate": true
    }
  }
}

Example

The following example shows a SQL Test class. Note that the setup and teardown methods are used to create and rollback each test in a transaction. This is a function of the test harness (nunit) which will call the setup method before each test and the teardown after each test. Because tests are rolled back, they can be written in a destructive way in order to create a consistent setup - truncating or faking tables and views and inserting data.

In the case of integration testing, we would not wrap tests in a transaction, but drop faked objects in the TearDown after the integration work has been performed.

Use With Caution: This is best applied to a local database that can be rebuilt as needed. Failures in test code can leave things in an undesired state.

using NUnit.Framework;
using System.Runtime.Versioning;
using System.Transactions;

namespace SqlTest.Tests
{
    [TestFixture]
    public class LoadCustomerTests
    {
        TransactionScope? scope;
        [SupportedOSPlatform("windows")]
        [SetUp]
        public void SetUp()
        {
            scope = new TransactionScope();
            TransactionManager.ImplicitDistributedTransactions = true;

        }
        [TearDown]
        public void TearDown()
        {
            if (Transaction.Current != null && Transaction.Current.TransactionInformation.Status == TransactionStatus.Active)
            {
                if (scope != null)
                {
                    scope.Dispose();
                }
            }
        }

        [Test]
        public void LoadCustomer_CustomerExists_NameIsUpdated()
        {
            //arrange
            Target target = new("TestDb");
            target.CreateFakeTable("Customer");
            target.ExecuteSql("Truncate Table Stage.Customer");
            target.ExecuteSql("INSERT INTO dbo.Customer (Id, CustomerCode, Name) values (1, 'Cust123', 'Old Name')");
            target.ExecuteSql("INSERT INTO Stage.Customer (CustomerCode, Name) values ('Cust123', 'New Name')");

            //act
            target.ExecuteSql("Exec Stage.Load_Customer");

            //assert
            var actual = target.GetActual($"SELECT Name FROM dbo.Customer");
            Assert.That(actual, Is.EqualTo("New Name"));
        }
    }
}

Documentation

Wiki on github

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.3 276 5/1/2023
2.0.2 167 4/18/2023
2.0.1 137 4/18/2023
2.0.0 150 4/18/2023
1.1.0 626 1/14/2021
1.0.14 1,775 1/30/2018
1.0.13 1,029 1/18/2018
1.0.12 937 9/1/2017
1.0.11 993 8/4/2017
1.0.10 898 8/3/2017
1.0.9 912 8/3/2017
1.0.8 876 8/3/2017
1.0.7 1,010 7/26/2017
1.0.6 1,030 7/20/2017
1.0.5 869 7/20/2017
1.0.4 891 7/14/2017
1.0.3 911 7/14/2017
1.0.2 878 7/14/2017
1.0.1 1,023 7/12/2017
1.0.0 1,053 7/11/2017

Updated build to include method documentation