Microsoft Unit Tests - Data source cannot be found

2019-08-09 11:38发布

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条回答
等我变得足够好
2楼-- · 2019-08-09 11:42

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.

查看更多
登录 后发表回答