Unit Testing your Database Changes

Play Unit Testing your Database Changes

The Discussion

  • User profile image

    oi muito grata beijos pra vcs...

  • User profile image

    How is allthis clicking continuous integration?

  • User profile image

    @Noe: Good question!

    The part we didn't really show (and probably should have) was that all of this testing can run in your pipeline. Just as you should run your code unit tests in your IDE before commiting, you can do the same with database tests.

  • User profile image

    So what I don't understand is how you would incorporate this with EF or EF Core migrations. Do you have any additional resources on that?

  • User profile image

    @JohnInMke:The funny thing is - I first learnt about unit testing data using EF. The DEV team were writing unit tests in C# for checking that when their code first approach was being performed - that the database was still "safe". There is a specific provider for interacting with SQL Server for EF.

    By writing unit tests in EF our database interactions were not causing the problems we initially had. We performed Arrange within the EF developed code to seed the database with known data, we then performed the Act part where we ran the code we'd written and then finally ran Assert to make sure that things were what we expected. Thus we had confidence that using automated testing during build time from there on that our code would be deemed "safe" from other changes too.

    We had some clients who didn't want EF used which prompted us to look at SQL Server Data Tools and we had a eureka moment where DBAs could write unit tests as well for Stored Procs. So it all started off with EF...!!


    Same deal (mostly) the packages that need to be added to the EFCore project for SQL Server are:


    The premise from there on in is the same for unit testing database interactions.


  • User profile image
    Gerald Britton

    When you did tsqlt.uninstall, it did not uninstall your unit test. That's a problem. if you have many devs writign tsqlt unit tests, how do you ensure that none of them every get deployed as part of the CI pipeline?

  • User profile image

    Most impressive!  I had no idea.  Will start using this tomorrow.

Add Your 2 Cents