Mary June Mary June -4 years ago 86
C# Question

How to specify datasource in data-driven unit test?

I have written a data-driven unit test and now I need to specify the data source for the unit test. The unit test is written in C# with MS Visual Studio. My data source is an excel file with a sheet named "TestData". My code for data source is as follows:

[DataSource(
"System.Data.Odbc",
"Dsn=Excel Files;dbq=.\data.xlsx;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5",
"TestData$",
"Sequential"
)]


What's wrong and how to fix it?

Answer Source

You can put ConnectionString in Config file and add to Attribute

<connectionStrings>  
     <add name="MyExcelConn" connectionString="Dsn=Excel Files;dbq=.\data.xlsx;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName="System.Data.Odbc" />  
</connectionStrings>

Then add to your update your attribute:

[DataSource("MyExcelDataSource")]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download