Microsoft Unit Tests - Data source cannot be found in the test configuration settings

会有一股神秘感。 提交于 2019-12-11 11:59:37

问题


I am trying to build Test class of data-driven unit tests in C#. I want to use 3 databases: one from SQL, one from Access and one from Excel. This is my app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="microsoft.visualstudio.testtools"
             type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, 
                   Microsoft.VisualStudio.QualityTools.UnitTestFramework, 
                   Version=10.0.0.0, 
                   Culture=neutral"/>
  </configSections>

  <connectionStrings>
    <add name="MyJetConn"
         connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
                           Data Source=H:\SQA\CoolMath\CoolMath\Database1.accdb;
                           Persist Security Info=False;"
         providerName="System.Data.OleDb" />

    <add name="MyExcelConn"
         connectionString="Dsn=Excel Files;
                           dbq=H:\SQA\CoolMath\CoolMath\CoolMathExcelDataTable.xlsx;
                           defaultdir=.;
                           driverid=1046;
                           maxbuffersize=2048;
                           pagetimeout=5"
         providerName="System.Data.Odbc" />

    <add name="MSSQLConn"
         connectionString="Data Source=H:\SQA\CoolMath\CoolMath\SQLExpress;
                           Initial Catalog=MSSQLDB;
                           Integrated Security=SSPI;"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <microsoft.visualstudio.testtools>
    <dataSources>
      <add name="MyJetDataSource"
           connectionString="MyJetConn"
           dataTableName="CoolMathAcessDataTable"
           dataAccessMethod="Sequential"/>
      <add name="MyExcelDataSource"
           connectionString="MyExcelConn"
           dataTableName="Sheet1$"
           dataAccessMethod="Sequential"/>
      <add name="MSSQLDataSource"
           connectionString="MSSQLConn"
           dataTableName="dbo.CoolMathDataTable"
      dataAccessMethod="Sequential"/>
    </dataSources>
  </microsoft.visualstudio.testtools>
</configuration>

When I try to run the tests, they all fail with the message:

"Data source cannot be found in the test configuration settings".

I can't see what am I doing wrong, perhaps it is the location of the databases? (they all in the same library as the code project and the XML file).

Above the Unit Tests I've place the following attributes:

[TestMethod()] [DeploymentItem("CoolMath\\CoolMathExcelDataTable.xlsx")]    
[DataSource("MyExcelDataSource")]

回答1:


With Visual Studio the relative paths for DataSources have moved. I'm using XML files, but I would not be surprised if the xlsx files have the same problem. I had to use the following to get the files to work

[DeploymentItem("TestData\\Search.xml")] [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", @"|DataDirectory|\\..\TestData\Search.xml", "Row", DataAccessMethod.Sequential)] Note the path in line 2, it is starts with a \..\ This is because relative paths changed from Solution to Project (or visa versa). Test_Data is a folder at the top of the project.



来源:https://stackoverflow.com/questions/29353086/microsoft-unit-tests-data-source-cannot-be-found-in-the-test-configuration-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!