且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

TFS构建的SQL服务器单元测试

更新时间:2023-02-21 10:20:14

嗨安德鲁,


感谢您发布此处。


根据错误消息,似乎构建代理程序服务帐户无法正确读取配置文件。


因此,请确保服务帐户首先拥有访问目标SQL Server的权限。


此外,如果您正在使用VSTest VNext,这意味着VSTest无法读取配置文件,VSTest可以使用.testsettings文件告诉它自定义单元测试执行环境的各个方面。


所以,你可以尝试根据设置创建.testsettings文件,然后在构建定义中指定
运行设置文件,然后再次尝试


请参考类似的线程进行故障排除:



***R egards。


We has a solution which is being built by TFS and SQL server unit tests which should be run on each build. The tests part also consists of a Database project which should be deployed to localDB before tests are running. The config for SQL server unit test project is:

<configSections>
        <section name="SqlUnitTesting" type="Microsoft.Data.Tools.Schema.Sql.UnitTesting.Configuration.SqlUnitTestingSection, Microsoft.Data.Tools.Schema.Sql.UnitTesting, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <connectionStrings>
    <add name="SnapDBContext" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=SampleDatabase;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>
    <SqlUnitTesting>
        <DatabaseDeployment DatabaseProjectFileName="..\..\..\..\Database\SampleDatabase.sqlproj" Configuration="Debug" />
        <DataGeneration ClearDatabase="true" />
        <ExecutionContext Provider="System.Data.SqlClient" ConnectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=SampleDatabase;Integrated Security=True;Pooling=False" CommandTimeout="30" />
        <PrivilegedContext Provider="System.Data.SqlClient" ConnectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=SampleDatabase;Integrated Security=True;Pooling=False" CommandTimeout="30" />
    </SqlUnitTesting>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

Everything work fine on my local machine but on TFS it fails with an error:

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.

The guys assumes that the tests worked fine on TFS 2013 but after the upgrading to TFS 2015 we started to get this error. Tried different solutions found here and over the internet but nothing helps. What am I missing? Thank you.

Hi Andrew,

Thank you for posting here.

According to the error message, it seems that the build agent service account cannot read the configuration files correctly.
So, please make sure the service account has the permission to access the target SQL Server first.

Besides, If you are using VSTest in VNext, that means the configuration file can't be read by VSTest, VSTest can use a .testsettings file to tell it to customize various aspects of the unit test execution environment.

So, you can try to create a .testsettings file based on the settings, then specify the Run Settings File in build definition, then try it again.

Please reference the similar threads for the troubleshoot:

Best Regards.