Friday, April 22, 2016

How to point sql server unit test project to localdb depending on visual studio version?

Leave a Comment

I have a Sql Server database unit test project that I set up following the instructions here: https://msdn.microsoft.com/en-us/library/jj851212(v=vs.103).aspx.

I have everything working perfectly ON MY MACHINE. I have visual studio 2013 Ultimate edition, which means my localdb connection string in my app.config for my unit test project points to the server (localdb)\ProjectsV12.

The problem is that some of my co-workers have professional edition, which means their db lives under: (localdb)\Projects. So they will have to alter the connection string in the app.config if they check it out from TFS and run the unit tests. I'd like it to be as seamless as possible so they don't have to do anything special -- just check out the project, build and run the tests.

How can I solve this so that the unit test project will point to the correct localdb server depending on your version of VS?

2 Answers

Answers 1

It is a problem with migrating projects between different versions of VS. As soon as you open a project with any version other than the version that the project was created in the migration wizard would pop up with the usual questions about SQL Express and LocalDb. But the main issue related to your question seems to be that of organizing your test code folder structure.

  1. Set up your folder structure as same as in TFS. One simple way to make this happen is to go to Visual Studio Online and create a new team test project with a root folder call 'test'. Give access rights to all in the team. Connect using your VS client and map your local repository and then check in your project. Let your team do the same.

Answers 2

I found a solution from the following blog post: https://blogs.msdn.microsoft.com/ssdt/2014/07/24/sql-server-database-projects-and-team-foundation-build/#unitTests

By having everyone run the following command, I determined that everyone had an instance named (localdb)\v11.0

%ProgramFiles%\Microsoft SQL Server\120\Tools\Binn\SqlLocalDB.exe i 

or alternatively:

"%ProgramFiles%\Microsoft SQL Server\110\Tools\Binn\SqlLocalDB.exe" i 

For now, I will point the app.config connection string to v11.0. If I discover that for some reason this instance won't work for a particular database project or someone doesn't have an instance with that name, I will add a prebuild step to install an instance under ProjectsV12:

"%ProgramFiles%\Microsoft SQL Server\120\Tools\Binn\SqlLocalDB.exe" create ProjectsV12 -s 

According to the blog:

If you want your database unit test to deploy the database to LocalDB\ProjectsV12, you will need to add a pre-test script to your build to create the instance. Running this command multiple times is safe.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment