1

I have tried unit testing with test data from excel sheet and it worked fine (after following the instructions from MSDN dosuments). However now I would like to take the test data directly from data base, can anyone please tell me the steps to follow as I was unable to find related document in the Website.

I am using Microsoft Visual Studio 2012 and Microsoft SQL Server Management Studio. Is there anything else also I need in order to acheive the task?

Regards,

SJ

2
  • Which testing framework are you using? How are you accessing the excel spreadsheet? Commented Nov 5, 2013 at 14:35
  • Have you tried isolating the database from your logic, and writing unit tests that don't require it? As long as you're testing from the database, you're continuing to promote external dependencies in your code, which keeps it hard to test and hard to prove correct. Commented Nov 6, 2013 at 3:31

1 Answer 1

1

I am using Microsoft Unit Test Framework. In this we can add a data source for unit testing. I added the following data source for Excel sheet:

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

  <microsoft.visualstudio.testtools>
    <dataSources>
      <add name="MyExcelDataSource"
           connectionString="MyExcelConn"
           dataTableName="Sheet1$"
           dataAccessMethod="Sequential"/>
   </dataSources>
  </microsoft.visualstudio.testtools>

Now wondering if we can somehow do the same with data table. Reference : http://msdn.microsoft.com/en-us/library/ms182527.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

From that very same document in "Specifying the Data Source": [DataSource(@"Provider=Microsoft.SqlServerCe.Client.4.0;Data Source=C:\Data\MathsData.sdf", "AddIntegersData")]. So you clearly can.
@StephenByrne In that example they are using sdf files which I wanted to avoid. So I got the solution as: [DataSource("System.Data.SqlClient", "Data Source=.\\SQLEXPRESS;Initial Catalog=UnitTesting;Integrated Security=True", "dbo.Company", DataAccessMethod.Sequential)] . Thanks anyway!
Cool. It's the same concept for all Datasources though so bear it in mind for future reference - you can use this to connect to excel, sdf, full-on sql server, 3rd party databases (Oracle, etc) or even plain old text files - anything you have ole or odbc drivers for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.