tSQLt - Database Unit Testing for SQL Server

Database Unit Testing for SQL Server

  • Home
  • Docs
    • Documents
    • Quick Start
    • Articles
  • Training
  • Downloads
tSQLt » Documents » Assertions » Fail

Fail

Syntax

tSQLt.Fail [ [@Message0 = ] message part ]
          [, [@Message1 = ] message part ]
          [, [@Message2 = ] message part ]
          [, [@Message3 = ] message part ]
          [, [@Message4 = ] message part ]
          [, [@Message5 = ] message part ]
          [, [@Message6 = ] message part ]
          [, [@Message7 = ] message part ]
          [, [@Message8 = ] message part ]
          [, [@Message9 = ] message part ]

Arguments

[@Message0 – @Message9 = ] message part
Optional. The message parts contain the message which will be displayed as the failure message for the test case. Multiple parameters are provided so that the caller does not have to first build up a string variable to pass to the fail procedure. All @Message parameters are NVARCHAR(MAX) with a default of an empty string.

Return Code Values

Returns 0

Error Raised

Raises a ‘failure’ error when called, resulting in failure of the test case.

Result Sets

None

Overview

Fail simply fails a test case with the specified failure message. Frequently, use of one of the tSQLt.Assert… procedures is more appropriate. However, there are times when calling Fail is the only option or more convenient.

Examples

Example: Using Fail to check that a randomly generated number is in the expected range
This test case repeatedly calls a random number generator. If the generated random number is ever outside of the specified range, the test case is failed using the tSQLt.Fail procedure. This example also demonstrates passing multiple message parameters to Fail:

CREATE PROCEDURE testRandom.[test GetRandomInt(1,10) does not produce values less than 1 or greater than 10]
AS
BEGIN
    SET NOCOUNT ON;

    EXEC Random.SeedRandomOnTime;

    DECLARE @numTrials INT; SET @numTrials = 10000;
    DECLARE @i INT; SET @i = 0;
    DECLARE @r INT;

    WHILE @i < @numTrials
    BEGIN
        EXEC Random.GetRandomInt 1, 10, @r OUTPUT;
        IF @r < 1 OR @r > 10
        BEGIN
            EXEC tSQLt.Fail 'Invalid random value returned: ', @r;
        END;

        SET @i = @i + 1;
    END;
END;
GO

See Also

  • AssertEmptyTable
  • AssertEquals
  • AssertEqualsString
  • AssertEqualsTable
  • AssertEqualsTableSchema
  • AssertLike
  • AssertNotEquals
  • AssertObjectDoesNotExist
  • AssertObjectExists
  • AssertResultSetsHaveSameMetaData
  • Fail
Share and Enjoy:
  • FacebookFacebook
  • TwitterTwitter
  • LinkedInLinkedIn
  • RedditReddit
  • StumbleUponStumbleUpon
  • TechnoratiTechnorati
  • PrintPrint

Navigation

  • Full user guide
  • Why you should use the tSQLt framework
  • How to get more out of tSQLt
  • Join the conversation
  • Downloads
  • Articles
  • Release Notes
  • New Logo
  • Training
  • Sponsors & Contributors
  • Why you should unit test SQL Server Code
  • Documents
    • Quick Start
    • tSQLt Tutorial
    • tSQLt Keyboard Shortcuts
    • Test Creation and Execution
      • NewTestClass
      • DropClass
      • RunAll
      • Run
      • RenameClass
    • Assertions
      • AssertNotEquals
      • AssertEmptyTable
      • AssertEquals
      • AssertEqualsString
      • AssertEqualsTable
      • AssertObjectExists
      • AssertResultSetsHaveSameMetaData
      • Fail
      • AssertObjectDoesNotExist
      • AssertEqualsTableSchema
      • AssertLike
    • Expectations
      • ExpectException
      • ExpectNoException
    • Isolating Dependencies
      • ApplyConstraint
      • FakeTable
      • SpyProcedure
      • FakeFunction
      • RemoveObjectIfExists
      • ApplyTrigger
      • RemoveObject

Links

  • tSQLt on GitHub
  • tSQLt Mailing List
  • twitter hashtag (#tsqlt)
  • tSQLt tag on stackoverflow.com
  • SQL Server Community on Slack (#tsqlt)
  • #tSQLtLive on YouTube

Sponsors

sqlity.net
redgate.com

A sqlity.net llc Web Property. | ©2010 - 2020, All Rights Reserved. | Privacy Policy | Terms of Use