SqlTest 2.0.3
dotnet add package SqlTest --version 2.0.3
NuGet\Install-Package SqlTest -Version 2.0.3
<PackageReference Include="SqlTest" Version="2.0.3" />
paket add SqlTest --version 2.0.3
#r "nuget: SqlTest, 2.0.3"
// 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
Product | Versions 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. |
-
net7.0
- Microsoft.Extensions.Configuration (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.4)
- Microsoft.Extensions.Configuration.Json (>= 7.0.0)
- Microsoft.SqlServer.SqlManagementObjects (>= 170.13.0)
- NUnit (>= 3.13.3)
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 | 309 | 5/1/2023 |
2.0.2 | 192 | 4/18/2023 |
2.0.1 | 155 | 4/18/2023 |
2.0.0 | 168 | 4/18/2023 |
1.1.0 | 689 | 1/14/2021 |
1.0.14 | 1,806 | 1/30/2018 |
1.0.13 | 1,053 | 1/18/2018 |
1.0.12 | 966 | 9/1/2017 |
1.0.11 | 1,011 | 8/4/2017 |
1.0.10 | 920 | 8/3/2017 |
1.0.9 | 925 | 8/3/2017 |
1.0.8 | 891 | 8/3/2017 |
1.0.7 | 1,033 | 7/26/2017 |
1.0.6 | 1,052 | 7/20/2017 |
1.0.5 | 890 | 7/20/2017 |
1.0.4 | 906 | 7/14/2017 |
1.0.3 | 933 | 7/14/2017 |
1.0.2 | 902 | 7/14/2017 |
1.0.1 | 1,046 | 7/12/2017 |
1.0.0 | 1,074 | 7/11/2017 |
Updated build to include method documentation