GithubHelp home page GithubHelp logo

tsqlt-visual-studio-examples's Introduction

tSQLt-visual-studio-examples

Example solution with database projects for using tSQLt in Visual Studio 2019

Notes

How to rename the database in a Visual Studio Project Mucking with XML

  1. Make sure the solution/project is saved.
  2. Inspect the .proj file for the element
  3. Update the value to the desired name. Save the file. Close the file.
  4. Reload the project in VS. (You usually will be prompted to reload.)

The general project structure we recommend consists of one or more "code" projects and an equal number of "test" projects in your solution. Each test project needs to have a database reference to its code project (and other code projects as needed),as well as, a database reference to the tSQLt DACPAC.

Example Project Structure
Example Project Structure

The "test" project database references must be configured to use "Same database" for their "Database Location".

Add Database Reference
Add Database Reference Dialog

After setting the project database references, you need to change the "debug" database to point to a (developer-edition) SQL Server instance instead of the default localdb included with VS.

Finally, we recommend to set "always re-create database" to true, as it generally increases deployment speed. Do not do this on shared development instances!

The Project's Debug Properties
The Project's Debug Properties

Files

The default method of calling tSQLt.NewTestClass to create a tSQLt test class (the schema to organize your unit tests) does not work in Visual Studio database projects. To work around this issue, we created an alternative:

CREATE SCHEMA [SomeRandomTests] AUTHORIZATION [tSQLt.TestClass];

Following standard VS recommendations, you can then create your tests in independent files. For maintainability it is a good idea to place all objects that belong to a test class in a folder within the project as show in the screenshot at the top.

The maybe indirect but right way.

  1. Ensure that the Solution is saved.
  2. In VS right-click on the project and select "Rename" from the context menu.
  3. Change the name of the project to the desired database name. Click "Yes" when prompted "Do you also want to rename the target database name for the project?"
  4. Save the solution
  5. Change the project name back to the old (or desired) name. Click "No" when prompted "Do you also want to rename the target database name for the project?"

Draft Instructions

How best to work in Visual Studio

Please see the tSQLt-visual-studio-examples

  1. Fork tSQLt-visual-studio-examples
  2. Download the latest version of tSQLt
  3. Unpack the tSQLt download and copy the correct MSSQL version of the dacpac in your Tests project. Use your own dacpacs at your own risk (please don’t).
  4. Create a folder with the following pattern assuming:
  5. The Schema of the Thing to Test is “MySchema”
  6. The Thing to Test is “MyModule”
  7. Create a schema file and “AUTHORIZATION [tSQLt.TestClass]” with a filename similar to “MySchema_MyModuleTests.sql”
  8. Write all tests for “MyModule” inside the “MySchema_MyModuleTests.sql” file separated by “GO”s.
    1. Why? Because if you want to change the name of the test in the future, you need only change it in this one place. Otherwise, if you have each test in its own file, you have to make three critical changes: 1) The name of the file 2) The revision history of this file in git 3) The name of the test within/in/inside the file.
  9. Update the Debug settings for all projects with the Solution:
    1. Make sure that they deploy to a real but UNSHARED AND NOT PRODUCTION server. (Project Name → Properties → Debug → Target Connection String → Edit)
    2. “Always re-create database” is selected. (Project Name → Properties → Debug → Deployment Options → Check “Always re-create database”)

Open “Run_tSQLt.sql” (make sure you’re connected to the database specified for your deploy above) and click “Execute” or “Ctrl+Shift+E”. You can also highlight the third line in the file, the one that starts with EXEC tSQLt.Run, and click Execute to run a single test class.

tsqlt-visual-studio-examples's People

Contributors

lizbaron avatar matthewflatt avatar mbt1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tsqlt-visual-studio-examples's Issues

SDK-style example and nuget packages of tSQLt framework

I tried to use an SDK-style project of SQL Server Data-Tier Application. It feels really natural to use dotnet build and dotnet publish thanks to https://github.com/rr-wfm/MSBuild.Sdk.SqlProj

Best part is, that tSQLt framework can be delivered in a very convenient way, as a nuget package.

Example file Tests.csproj looks like this:

<Project Sdk="MSBuild.Sdk.SqlProj/2.0.0-beta.1">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <SqlServerVersion>Sql150</SqlServerVersion>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\Code\Code.csproj" />
    <PackageReference Include="tSQLt.2019" Version="1.0.7950.1808" />
  </ItemGroup>
</Project>

where tSQLt.2019 is a name of nuget package. Yes, we would need a lot of packages for each SqlServerVersion.

I put the whole example here: https://github.com/cagrin/tSQLt-visual-studio-examples

What do you think about this SDK-style?
Could it be possible to release nuget packages of tSQLt framework?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.