COMMIT A TRANSACTION IN DATABASE A FROM tSQLt Database Using Visual Studio 2019

38 views
Skip to first unread message

Kamalpresna Boopathy

unread,
Jun 18, 2021, 2:49:40 PM6/18/21
to tSQLt
Hi,

I have a test case runs in tSQLt database to insert data to primary database table and compare results with asserts equal. Test case is passed but inserted data disappear from primary database. How can we make the data persistent, instead of rolling back after test case execution? Also, this is happening only if i run the test class from Visual Studio.

if i run the test class using SQL Server Managment Studio , Data is retained in primary database. How to fix this issue from Visual Studio 2019? How does the test case execution process differs between Visual Studio and SSMS?

Thanks
Kamal

Bill Wood

unread,
Jun 18, 2021, 3:08:16 PM6/18/21
to ts...@googlegroups.com
Hi Kamal,

If you want the data to persist, then you need to not use the FakeTable command to create a test version of the table.

You can insert the data into the table and assert the results without faking the table if there is no need to fake it for testing purposes.

Regards,

Bill.

--
You received this message because you are subscribed to the Google Groups "tSQLt" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tsqlt+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tsqlt/64d416d3-468c-4223-9817-88bc87afd60dn%40googlegroups.com.

Kamalpresna Boopathy

unread,
Jun 18, 2021, 3:16:21 PM6/18/21
to tSQLt
Thank you Bill for your response!.

I'm not using FakeTable command, but still data is not persistent

Bill Wood

unread,
Jun 18, 2021, 3:22:01 PM6/18/21
to ts...@googlegroups.com
Sorry, I misspoke. Since the entire test is wrapped in a transaction it will roll it back. The assumption is the test would be written based on the fact that you want the test environment to be in the same state after it runs as it was before.

You'd have to write the data outside the test for it not to be rolled back. In that case, it probably doesn't fit the paradigm of unit testing and may fall into the realm of data testing if you are looking for the data to persist.

I apologize for the misleading response.

Kamalpresna Boopathy

unread,
Jun 18, 2021, 3:40:51 PM6/18/21
to tSQLt
No worries. That's what I assumed too. Thanks for clarifying.

But data is persistent when executing the test class from SQL Server Management Studio,  which is confusing me. What would be the difference between Visual studio and SSMS execution?

Sebastian Meine

unread,
Jun 18, 2021, 4:00:55 PM6/18/21
to ts...@googlegroups.com
Kamalpresna,

tSQLt executes tests within a transaction. So any insert will get rolled back as Bill said.

However, for tSQLt to have that control, you cannot execute test stored procedures directly. If you do that, your test will just behave like a normal stored procedure.

Instead you need to call tSQLt.Run or tSQLt.RunAll


 
Thanks,

Sebastian


Bryant McClellan

unread,
Jun 21, 2021, 7:37:48 AM6/21/21
to ts...@googlegroups.com
FWIW we distribute templates for creating tSQLt tests. They have a trap to look for an open transaction. If they don't detect one they will abort. That helps keep tests from altering objects and data when not called via tSQLt.

G Bryant McClellan
Senior Solutions Architect
260.748.5393 (direct)

Do it Best Corp.
6502 Nelson Road | Fort Wayne, IN 46803




Reply all
Reply to author
Forward
0 new messages