dbForge Unit Test for SQL Server: Complete Guide to Automated Database Testing

How to Use dbForge Unit Test for SQL Server — Step‑by‑Step Tutorial

This tutorial walks through creating, running, and maintaining unit tests for SQL Server using dbForge Unit Test. It assumes you have dbForge Studio for SQL Server installed and a working connection to your SQL Server instance.

Prerequisites

  • dbForge Studio for SQL Server (with Unit Test feature enabled) installed.
  • SQL Server accessible and a database with objects you want to test.
  • A SQL Server login with sufficient permissions to create test objects and run code.

1. Create a Unit Test Project

  1. Open dbForge Studio for SQL Server.
  2. In the main menu, choose File → New → Project → Unit Test Project.
  3. Enter a project name (e.g., MyDatabaseTests) and select a folder to save it.
  4. Choose the target database connection (the database you’ll test).
  5. Click Create. A new Unit Test Project with a default structure appears in the Unit Test Explorer.

2. Add a Unit Test

  1. In Unit Test Explorer, right‑click the project or a test folder → Add → Unit Test.
  2. Name the test (e.g., TestGetActiveUsers).
  3. The test editor opens and includes sections for Setup, Test Body, and TearDown.

3. Write Setup and TearDown Scripts

  • Use Setup to prepare test data/state (create temp tables, insert rows).
  • Use TearDown to clean up (drop temp objects, rollback transactions if used).

Example Setup:

sql

CREATE TABLE #Users (Id INT PRIMARY KEY, Name NVARCHAR(100), IsActive BIT); INSERT INTO #Users (Id, Name, IsActive) VALUES (1, ‘Alice’, 1), (2, ‘Bob’, 0);

Example TearDown:

sql

IF OBJECTID(‘tempdb..#Users’) IS NOT NULL DROP TABLE #Users;

4. Implement the Test Body

  • Call the stored procedure or function under test and use assertions to validate results.
  • dbForge supports assertions like AreEqual, IsTrue, IsNull, RowCount, QueryResultEquals, etc.

Example test body:

sql

– Execute target procedure that should return active users EXEC dbo.GetActiveUsers; – assume this returns a result set – Use QueryResultEquals to compare with expected result set saved in a file

To assert programmatically:

  1. Capture results into a temp table:

sql

INSERT INTO #ActualResults EXEC dbo.GetActiveUsers;
  1. Use assertions:

sql

– Example: check row count SELECT COUNT(*) AS ActualCount FROM #ActualResults; – Then in dbForge UI, add assertion: ActualCount = 1

5. Use Data-Driven Tests (Optional)

  • Create multiple input sets using data files or tables and iterate tests against them.
  • In the test editor, link a data source (CSV or table) and map columns to parameters.

6. Run Tests

  1. In Unit Test Explorer select a single test, folder, or the entire project.
  2. Click Run. Tests execute and show pass/fail status with execution time.
  3. Expand test results to see SQL output, error messages, and assertion details.

7. Debug Failing Tests

  • Open the test SQL, run sections manually in the Query Editor to reproduce issues.
  • Use PRINT or SELECT statements in Setup/Test/TearDown to inspect intermediate data.
  • Fix the code under test or the test expectations and re-run.

8. Integrate with Continuous Integration (CI)

  • Export test results to formats supported by your CI (XML/HTML) if available.
  • Use command-line automation (if provided by your dbForge edition) or schedule test runs via PowerShell invoking dbForge CLI or by running test scripts on a build agent that has dbForge installed.

9. Best Practices

  • Isolate tests: each test should set up and clean up its own data.
  • Keep tests small and focused (one assertion intent per test).
  • Use test-specific test databases or schemas to avoid interference.
  • Version-control test projects alongside database code.
  • Automate test runs on pull requests or nightly builds.

10. Troubleshooting Tips

  • Permission errors: ensure the test account can create objects and execute the code under test.
  • Flaky tests: use transactions/rollbacks or reliable cleanup to avoid state carryover.
  • Large result comparisons: export expected results to files and use QueryResultEquals to compare sets.

11. Example: Complete Simple Test

Setup:

sql

CREATE TABLE #Users (Id INT, Name NVARCHAR(100), IsActive BIT); INSERT INTO #Users VALUES (1, ‘Alice’, 1);

Test body:

sql

INSERT INTO #ActualResults EXEC dbo.GetActiveUsers;

Assertion (in dbForge UI): RowCount(#ActualResults) = 1

TearDown: “`sql IF OBJECT_ID(‘tempdb..#Users’) IS NOT NULL DROP TABLE #Users;

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *