Search code examples
c#mstestdata-driven-tests

Data source name not found while running the unit test


I am writing a data driven unit test. The input is an excel of type ".xlsx". I took reference from here: https://learn.microsoft.com/en-us/previous-versions/ms404700(v=vs.90)?redirectedfrom=MSDN and used Odbc. Adding the code snippet below:

[TestMethod]        
[DataSource(
            "System.Data.Odbc",
            "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};dbq=|DataDirectory|\\Data.xlsx;defaultdir=.;driverid=1046;fil=excel 12.0;maxbuffersize=2048;maxscanrows=8;pagetimeout=5;readonly=1;safetransactions=0;threads=3;uid=admin;usercommitsync=Yes",
            "Sheet1$",
            DataAccessMethod.Sequential)]        
[DeploymentItem("Folder\\Data.xlsx")]        
[Timeout(60000)]        
public void TestMethod1()
{
  
}

This is how the csproj of project look like:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Library</OutputType>
        <TargetFramework>net472</TargetFramework>
        <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
      </PropertyGroup>
      
      <-- Some dependencies -->

     <ItemGroup>
      <Content Include="..\..\..\Data.xlsx">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </Content>
   </ItemGroup>
     
    </Project>

In local it's running perfectly fine, when I am running the build in msazure the unit test is failing with following error.

Error Message:
   The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library. Error details: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Solution

  • I changed the source file from excel to xml and used the below data driver.

    [DeploymentItem("Data.xml")]
            [DataSource(
                "Microsoft.VisualStudio.TestTools.DataSource.XML",
                "|DataDirectory|\\Data.xml",
                "OracleParameters",
                DataAccessMethod.Sequential)]
    

    This worked in the pipeline also.