Unit Testing your Database Changes

Sign in to queue

The Discussion

  • User profile image
    mirian

    oi muito grata beijos pra vcs...

  • User profile image
    Noe

    How is allthis clicking continuous integration?

  • User profile image
    Damovisa

    @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
    JohnInMke

    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
    HamishWatson

    @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...!!

    EFCore:

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

    Microsoft.EntityFrameworkCore
    Microsoft.EntityFrameworkCore.SqlServer

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

     

Add Your 2 Cents