3

I have a suite of SQL Server Database Unit Tests built in Visual Studio 2013 to test stored procedures and functions. The tests run fine against my local database but I also want them to be run against a DEV server's DB as part of a CI process.

To achieve this I am building the test DLL and copying both the DLL and config file to the DEV server and then running the command line vstest.console.exe to test. This works fine on my local machine but when I try to run it on DEV I get the following exception for each test:

An error occurred while SQL Server unit testing settings were being read from the configuration file. Click the test project, open the SQL Server Test Configuration dialog box from the SQL menu, add the settings to the dialog box, and rebuild the project.

I have tried using slow cheetah to ensure that an App.Config transform is applied before the solution is built and I can see that the transform is being applied in the config file, but still my tests fail.

Has anyone figured out a way to do this? It seems strange that the DLL has to be rebuilt to consume connection string changes in a config file.

I also tried following the suggestions in this post but without success.

2 Answers 2

2

I finally got this working. It seems as if there was a change to the structure of the configuration files that are used for SQL unit testing. In my config I had a section called SqlUnitTesting which contained the connection strings for ExecutionContext and PrivilegedContext. I noticed that when I created a new unit test project the section name had changed to SqlUnitTesting_VS2013. Changing my existing config to this solved the problem. I can only assume that I was calling a slightly different version of vstest.console.exe in the two environments.

To summarise how to get SQL Server Unit Tests working in multiple environments from the command line vstest.console.exe app these are the steps to take:

  1. Install the Slow Cheetah extension for Visual Studio, which manages transformation of your App.config file based on the selected build configuration.
  2. Make sure that your App.config file has a section called SqlUnitTesting_VS2013 which contains your ExecutionContext and PrivilegedContect connection strings.
  3. Set up slow cheetah transforms for each environment that you use, based on the build configuration (debug, release etc.). You'll need to read the Slow Cheetah doco to do this, but basically you're setting up connection strings for each environment that are transformed into the App.config at build time.
  4. Build the solution using your target environment configuration.
  5. Copy the DLL and config from the relevant bin/buildConfiguration directory to your target environment.
  6. Run vstest.console.exe and observe if your tests pass.

I hope this is helpful to someone.

Sign up to request clarification or add additional context in comments.

Comments

0

The following source code sample is based on Greg's answer.

app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>...</configSections>
    <SqlUnitTesting>
       <DataGeneration .. />
       <ExecutionContext .. />
       <PrivilegedContext.. />
    </SqlUnitTesting>
    <startup>...</startup>
</configuration>

app.Release.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <configSections>...</configSections>
    <SqlUnitTesting xdt:Transform="Replace">
       <DataGeneration .. />
       <ExecutionContext .. />
       <PrivilegedContext.. />
    </SqlUnitTesting>
    <startup>...</startup>
</configuration>

For more information on SlowCheetah, you may refer to the following link: Github and Microsoft DevBlogs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.