I have some integration tests that run against a SQL Server localdb database - my development app talks to a full SQL instance on localhost, but the integration tests all talk to (localdb)\v11.0
It would be really handy if I could run the ReadyRoll database deployment against localdb as part of my test fixture setup, so we're always testing the code against the latest set of migrations.
Is this possible? The test code is all C# and NUnit is our test framework.
We currently don’t have an API for deploying databases with ReadyRoll, however you could call out to SQLCMD to perform the database deployment as part of your test fixture setup.
Firstly, you’ll need to enable SQLCMD packaging within your database project (under Outputs in Project Settings). Then, ensure that your unit test project has a dependency on the database project, so that the package is built before the tests are executed.
In your test fixture setup, make a call to SQLCMD:
sqlcmd.exe -E -S (localdb)\v11.0 -i “…\MyDbProject\bin\Debug\MyDbProject_Package.sql” -v DatabaseName=MyDatabase
This will deploy any pending migrations, creating the database as necessary. To start with a clean database, you may like to add a separate call to SQLCMD to drop the database prior to deployment.
sqlcmd -E -S (localdb)\v11.0 -Q “IF DB_ID(‘MyDatabase’) IS NOT NULL DROP DATABASE [MyDatabase]”
So that the package artifact is included along with your test results, you may like to copy the “MyDbProject_Package.sql” file to your test project, eg. as part of the “.testsettings” Deployment configuration in Visual Studio.
This question has received the maximum number of answers.