Running Tests

To run the tests, we have a couple of different stored procedures we can use. The first one is tSQLt.RunAll which finds and runs all the tests in the database.

Action: Run all the tests in the tSQLt_Example database

Run the sql code:

exec tSQLt.RunAll;

If you run this, the tests will run, and there is one test that fails, and you should see the following output:


If we break the output down what we get is the details of any tests which have failed and the reason why they failed:

[AcceleratorTests].[test ready for experimentation if 2 particles] failed: (Failure) Expected: <1> but was: <0>

In this case, the test [AcceleratorTests].[test ready for experimentation if 2 particles], failed because at some point in the test it expected a number 1 but the number 0 was found.

After all the failed tests are listed including the reason why they failed, the list of all tests which have run are shown including whether they passed or failed. The listing of which tests have passed is useful as there are some quite strict rules (which we will cover later) about creating the schema and naming tests which, if not followed mean you can think your test is running but it isn't.

Finally, we have an error because one test failed, if all tests pass then we do not get an error raised - we can use this when automating the running of our tests, for example, if the batch failed then something went wrong.

Action: Run a specific test

If you didn't want to run all the tests together, you could run individual tests using tSQLt.Run, run the code:

exec tSQLt.Run '[AcceleratorTests].[test a particle is included only if it fits inside the boundaries of the rectangle]'

The output from running a single test that passed

In the case of running just a single test, because this passed and we had no failures we see the same output, but the "Test Case Summary" at the end did not generate any errors.

Action: Run all tests in a specific test class

The T-SQL language has no concept of classes but it is helpful to group tests together, for example all the tests around a specific stored procedure can be grouped into a "test class" using a schema so in the tSQLt_Example database we have a schema called AcceleratorTests and although, in the sample, there is only one test schema we could have one test schema for each group of tests that we have.

To run all the tests in a schema or "test class" we can pass just the name of the schema to tSQLt.Run, if you run the following script:

exec  tSQLt.Run '[AcceleratorTests]'

We will see the same output as if we had run tSQLt.RunAll but if we had created multiple schemas for our tests, only the tests in the specific schema would have run.