tSQLt - Database Unit Testing for SQL Server

Database Unit Testing for SQL Server

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

AssertEquals

Syntax

tSQLt.AssertEquals [@expected = ] expected value
                 , [@actual = ] actual value
                [, [@message = ] 'message' ]

Arguments

[@expected = ] expected value
The expected value for the test. @expected is SQL_VARIANT with no default.
[@actual = ] actual value
The actual value resulting from processing during the test. @actual is SQL_VARIANT with no default.
[@message = ] ‘message’
Optional. String containing an additional failure message to be used if the expected and actual values are not equal. @message is NVARCHAR(MAX) with no default.

Return Code Values

Returns 0

Error Raised

Raises a ‘failure’ error if expected and actual are not equal.

Raises an ‘Operand type clash’ error if the value passed for @expected or @actual is not compatible with SQL_VARIANT. The most common form of this is VARCHAR(MAX) – for this tSQLt.AssertEqualsString is provided.

Result Sets

None

Overview

AssertEquals compares two values for equality. If they are not equal, the test case is failed; otherwise, AssertEquals does not affect test processing. For the purposes of AssertEquals, NULL is considered equal to NULL. Any non-NULL value is considered not equal to NULL.

Examples

Example: AssertEquals to check the results of a function
This test case uses AssertEquals to compare the return result of a function with an expected value.

CREATE PROCEDURE testFinancialApp.[test that ConvertCurrencyUsingLookup converts using conversion rate in CurrencyConversion table]
AS
BEGIN
    DECLARE @expected MONEY; SET @expected = 3.2;
    DECLARE @actual MONEY;
    DECLARE @amount MONEY; SET @amount = 2.00;
    DECLARE @sourceCurrency CHAR(3); SET @sourceCurrency = 'EUR';
    DECLARE @destCurrency CHAR(3); SET @destCurrency = 'USD';

------Fake Table
    EXEC tSQLt.FakeTable 'FinancialApp', 'CurrencyConversion';

    INSERT INTO FinancialApp.CurrencyConversion (id, SourceCurrency, DestCurrency, ConversionRate)
                                         VALUES (1, @sourceCurrency, @destCurrency, 1.6);
------Execution
    SELECT @actual = amount FROM FinancialApp.ConvertCurrencyUsingLookup(@sourceCurrency, @destCurrency, @amount);

------Assertion
    EXEC tSQLt.assertEquals @expected, @actual;
END;
GO

Example: A variety of the possibilities of AssertEquals

The examples below show what to expect from AssertEquals when called with different values.

    EXEC tSQLt.AssertEquals 12345.6789, 12345.6789; -- pass
    EXEC tSQLt.AssertEquals 'hello', 'hello'; -- pass
    EXEC tSQLt.AssertEquals N'hello', N'hello'; -- pass

    DECLARE @datetime DATETIME; SET @datetime = CAST('12-13-2005' AS DATETIME);
    EXEC tSQLt.AssertEquals @datetime, @datetime; -- pass

    DECLARE @bit BIT; SET @bit = CAST(1 AS BIT);
    EXEC tSQLt.AssertEquals @bit, @bit; -- pass

    EXEC tSQLt.AssertEquals NULL, NULL; -- pass
    EXEC tSQLt.AssertEquals 17, NULL; -- fail
    EXEC tSQLt.AssertEquals NULL, 17; -- fail

    EXEC tSQLt.AssertEquals 12345.6789, 54321.123; -- fail
    EXEC tSQLt.AssertEquals 'hello', 'goodbye'; -- fail

    DECLARE @datetime1 DATETIME; SET @datetime1 = CAST('12-13-2005' AS DATETIME);
    DECLARE @datetime2 DATETIME; SET @datetime2 = CAST('07-19-2005' AS DATETIME);
    EXEC tSQLt.AssertEquals @datetime1, @datetime2; -- fail

    DECLARE @bit1 BIT; SET @bit1 = CAST(1 AS BIT);
    DECLARE @bit2 BIT; SET @bit2 = CAST(1 AS BIT);
    EXEC tSQLt.AssertEquals @bit1, @bit2; -- pass

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