Bluebaron
Bluebaron

Reputation: 2476

Can't create a simple SQL Test

I'm trying to get just basic unit testing on an SQL database through a Visual Studio project.

The first thing I tried was just creating a simple .Net Core Unit Testing library.

Then I added an SQL Test, and it couldn't link to the SqlDatabaseTestClass`. Looking around, this is a fairly ubiquitous issue, but I found an obscure comment at the bottom of https://developercommunity.visualstudio.com/content/problem/29842/database-unit-test-project-does-not-build-in-visua.html that said that it's probably a linking issue.

So I created a .Net Framework Unit Test project. Now that has no option to add an SQL Test.

So I found an article that says that I should try creating an SQLDatabase https://www.codeguru.com/csharp/.net/net_data/create-sql-server-database-unit-tests-using-visual-studio.html

But then I got an error that The server version or database compatibility level is not supported. Import Database in VS 2015 SQL Server Database Project

The solution to that was to go to the SQL Server Object Explorer and right click on the DB and create an SQL project by right clicking on the Database. And that worked! Finally I have a project with database tables.

But now when I right click on a stored procedure, I don't get the option to create a unit test.

Upvotes: 0

Views: 483

Answers (1)

Bluebaron
Bluebaron

Reputation: 2476

I figured it out.

  1. Open the View -> SQL Object Explorer in the top menu bar in Visual Studio 2017. Make sure it's the Object Explorer and not the Server Explorer. These instructions presuppose that you have your connections setup properly.

  2. Right-click on your database and click Create New Project. Make sure the directory is correct because it wasn't for me.

  3. After the database imports, go to your Error List and resolve all those errors. Now you should be able to right-click on a stored procedure in either the SQL Server Object Explorer and select Create Unit Tests.

Allowing Visual Studio to do all the heavy lifting sorted me out.

Upvotes: 1

Related Questions