DataGrip 2022.2 Help

Run tSQLt tests

tSQLt is a framework for unit testing on Microsoft SQL Server. You can use it to test stored procedures, functions, views, and triggers of a database.

After the installation, the tSQLt script creates the tSQLt schema that stores framework functions and procedures. Some procedures have the Private_ prefix and can be used only by the framework.

Step 1. Create objects to be tested

For illustration purposes, you can create the FinancialApp schema from the tSQLt tutorial at tsqlt.org.

  1. Uncompress tSQLt_demo.zip. A link to the archive is at the beginning of this topic.

  2. In DataGrip, navigate to File | Open.

  3. Right-click the Microsoft SQL Server data source and select Run SQL Script.

  4. In the file browser, navigate to the demoApp.sql from the tSQLt_demo.zip archive.

Step 2. Create a class with tests

In tSQLt, all tests are collected under a single class. A class is a schema.

  • To create a new class, use the NewTestClass procedure. For example, EXEC tSQLt.NewTestClass 'testFinancialApp'.

    Create the TestClass class

Step 3. Create tests for the class

  1. In the Database Explorer (View | Tool Windows | Database Explorer), right-click the Microsoft SQL Server data source and select Open Query Console.

  2. Type the code of a test. You can take examples from the tSQLt tutorial.

  3. Click the Execute button (the Execute button) or press Ctrl+Enter.

    Create a function to be tested

Step 4. Enable created schemas

  1. Click the N of N link near the data source name. In the schema selection window, select schemas that you created (for example, tSQLt, testFinancialApp, and FinancialApp).

  2. Press Enter.

    Enable the utPLSQL schema

Step 5. Run tSQLt tests by using a run configuration

  1. Open the Run/Debug Configuration dialog in one of the following ways:

    • Select Run | Edit Configurations from the main menu.

    • With the Navigation bar visible (View | Appearance | Navigation Bar), choose Edit Configurations from the run/debug configuration selector.

  2. In the Run/Debug Configuration dialog, click the Add New Configuration icon (the Add New Configuration icon) and select tSQLt Test.

    The fields that appear in the right-hand pane display the default settings for the selected configuration type.

    • Data source: the name of a data source. tSQLt supports Microsoft SQL Server.

    • Database: the name of a database (for example, testFinancialApp).

    • Paths: a qualified path to the testing package (for example, testFinancialApp.test that ConvertCurrency converts using given conversion rate).

  3. Apply the changes and close the dialog.

  4. Click the Run <configuration_name> button The Run button.

    Run files by using run/debug configurations

Productivity tips

Run tests from the editor

  1. In the Database Explorer (View | Tool Windows | Database Explorer), double-click the test.

  2. In the editor, click the Run icon the Run icon in the gutter and select Run <procedure_name>.

    Run tests from the editor

Rerun failed tests

  • In the Run tool window, click the Rerun failed tests button

    Rerun failed tests
Last modified: 17 March 2022