How to re-deploy, re-create database on each test run

前端 未结 3 934
鱼传尺愫
鱼传尺愫 2021-01-05 14:11

Currently I\'m using Visual Studio 2012 RC and SQL Server 2012 RTM.

I\'d like to know how to re-deploy/re-create a test database for each test run.

Keep in m

相关标签:
3条回答
  • 2021-01-05 14:49

    The fastest solution, while a bit of a hack, is really straightforward. You can set the DB Projects properties under the debugging tab to "always re-created DB". Then test in two clicks, do a debug/build, then run all tests. You should get a freshly built DB on localDB for you tests to be ran against. You can also change the target for the debugging DB (again the DB projects properties) to whatever you want, so you can deploy to a .dacpac, or to an existing SQL DB or wherever. It means testing in two steps, and if your build is long, it may be annoying, but it works. Otherwise, I believe scripting is your only option.

    0 讨论(0)
  • 2021-01-05 14:50

    Edit: Although this doesn't answer the question in a plain SQL Server way, an easy Entity Framework approach would be the following: I found that I could create and destroy my database every time correctly by using the DbContext.Database.CreateIfNotExists() and DbContext.Database.Delete() methods in my setup and cleanup phases of my tests.

    0 讨论(0)
  • 2021-01-05 15:00

    As mentioned you'll probably want to use the VS 2012 .Local.testsettings > Setup and Cleanup scripts to create / tear down you SQL Server database.

    enter image description here

    For the script you may want to use powershell with a .dacpac (rather than just a T-SQL script), since you are using a SSDT project. Here's a link to some example code - in particular you may want to take a look at the 'Deploy-Dac' command.

    If you are unfamiliar with .dacpacs as the (build) output of SSDT-created database projects, take a look at this reference link.

    0 讨论(0)
提交回复
热议问题