I'm using the tSQLt unit testing tool. I want to test if an actual row count is within .1% of an expected value. Seems like I could use a EXEC tSQLt.AssertIntValueInRange
procedure, but I couldn't find anything like this. I would settle for writing the logic myself and conditionally calling EXEC tSQLt.FailAssertion
, but apparently this doesn't exist either.
Would somebody please advise on how to use tSQLt for checking if an integer value is within x percent of an expected value?
CodePudding user response:
You'd have to do the comparison logic yourself, but it's pretty easy. You could define a BIT variable, do your comparison in SQL and then assert the value of the BIT variable:
DECLARE @IsWithinRange BIT;
DECLARE @RowCount DECIMAL(18,2);
DECLARE @Threshold INT = 1000;
SELECT @RowCount = CONVERT(DECIMAL(18,2), COUNT(*)) FROM MyTable;
SET @IsWithinRange = IIF(@RowCount/@Threshold <= .1, 1, 0);
EXEC tsqlt.AssertEquals @exptected = 1, @actual = @IsWithinRange;
CodePudding user response:
Actually, there’s a tSQLt.Fail
that you can use for this. here’s the documentation.
But, going after a row count in unit tests often leads to tests that are hard to read and maintain while providing little value towards proving that the module under test is working as designed.
It would be a better approach to provide specific data in your test (use tSQLt.FakeTable to make that easier) and then compare actual result content to an expected result with tSQLt.AssetEqualsTable.