How can we help you today? How can we help you today?

Exception block not getting covered in test case (Catch block)

Hello,

My test case does not achieve 100% code coverage because the CATCH block in the procedure is not being executed during the test. To address this, I wrote a failure scenario intended to trigger the CATCH block. However, the object under test does enter the CATCH block as expected so the test case does not succeed. so used the below tsqlt framework syntax but my test case still not working.  

following syntax: 

Try1:

SET @expected = 'column does not allow nulls. UPDATE fails.'
EXEC tSQLt.ExpectException @ExpectedMessagePattern = '%column does not allow nulls. UPDATE fails.%', @ExpectedSeverity = NULL, @ExpectedState = NULL
 

Try2 :

SET @expected = 'column does not allow nulls. UPDATE fails.'
EXEC tSQLt.ExpectException @ExpectedMessagePattern = '%column does not allow nulls. UPDATE fails.%', @ExpectedSeverity = NULL, @ExpectedState = NULL
 

Try3:

EXEC tSQLt.ExpectNoException @Message ='Cannot insert the value NULL into column ‘’labelStatusId'', table ‘’localdb.dbo.Label''; column does not allow nulls. UPDATE fails.'

 

Kindly share the solution to resolve

Thanks,

Murali
0

Comments

1 comment

  • Jon Kirkwood
    Official comment

    Hi Murali

     

    Thank you for contacting Redgate support regarding your code coverage concern, I suspect this is related to our SQL Test product and have moved this post into a new location on our forums to better represent that.

     

    SQL Test provides a UI wrapper around the tSQLt framework, it doesn’t change or extend the tSQLt functionality itself. 

    In other words, SQL Test runs whatever tSQLt syntax you provide, but the underlying behavior (such as how tSQLt.ExpectException or tSQLt.ExpectNoException works) comes from the tSQLt framework, not from SQL Test.

     

    Because of this, issues with exception handling or coverage inside tSQLt tests are usually best addressed by checking the tSQLt documentation or raising the question with the tSQLt community. https://tsqlt.org/join-the-conversation

    We’re happy to help confirm that SQL Test is running the tests correctly, but troubleshooting tSQLt specific logic is outside the scope of our product support.

     

    That said, some general guidance we’ve seen work for other users:

    • tSQLt.ExpectException only detects errors that escape the procedure — if your CATCH block swallows the error without THROW/RAISERROR, nothing reaches the test framework.
    • For coverage, you may need to either rethrow the error or assert side-effects (like rows in a log table) instead of expecting an exception.

    You can find more details here: tSQLt Documentation – ExpectException

    If you’re not sure whether SQL Test itself is working correctly or if this is purely a tSQLt logic issue, we can potentially help you confirm that.

    Jon Kirkwood

Add comment

Please sign in to leave a comment.