Vs2015 data-driven unit test and vs2015 data unit test
Today in the test of the boss let me this cainiao to do vs2015 under the c # unit test, and gave me reference http://www.cnblogs.com/kingmoon/archive/2011/05/13/2045278.html
However, my current ide is vs2015. The general unit test is the same as vs2010. When I perform a data-driven unit test, I feel that there are many differences between the two versions of ide, baidu blog cannot get the answer either. I asked boss again. boss asked me to check msdn and cainiao gawain for the first time, on msdn, I spoke about unit tests and data-driven unit tests in detail, but few people read them patiently. According to my findings, the official practice is to configure data-driven code, I believe that most of the pictures are hard to understand, so I will summarize my process.
The simple unit test section is not described. It is the same as the vs2010 method in the above link.
Here I will only show "visual operations combined with configuration for data-driven unit tests ":
First create a c # project CUnitTest for testing. Here we choose to perform a unit test by adding (int a, int B ):
Right-click solution, add, and create a project
Create a unit test project
In the project, select Add new Data source:
Click Create connection:
I use the excel Data driver here, so select the microsoft odbc data source and click OK
Select "use connection string" and click Generate:
Select microsoft excel driver:
Click Next
Click to browse
Save as datatest
Click Next to complete
Select work books and create the data2.xls data source on the table. The test data is as follows:
Select this workbook
Then, follow these steps to enter the Computer Administrator password.
The connection test is successful. Click OK.
Click "Next" until the task is completed.
Open the solution on the right and test the app. config under the project.
Is configured
You can refer to the above msdn code configuration, web site https://msdn.microsoft.com/zh-cn/library/ms243192.aspx
The general steps are as follows:
Create the app. config file.
Custom configuration section.
Define the connection string.
Define the data source.
Use the performanceattribute class to access the data source.
The Code is as follows:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 5 </configSections> 6 <connectionStrings> 7 <add name="CUnitTestTests.Properties.Settings.ConnectionString1" 8 connectionString="Driver={Microsoft Excel Driver (*.xls)};dbq=C:\Users\jiazhen\Desktop\data2.xls;defaultdir=C:\Users\jiazhen\Desktop;driverid=790;exclusive=0;fil=excel 8.0;filedsn=C:\Users\jiazhen\Desktop\data724.dsn;maxbuffersize=2048;maxscanrows=8;pagetimeout=5;readonly=1;safetransactions=0;threads=3;uid=admin;usercommitsync=Yes;pwd=Mjw131023" 9 providerName="System.Data.Odbc" />10 </connectionStrings>11 <microsoft.visualstudio.testtools>12 <dataSources>13 <add name="MyExcelDataSource1" connectionString="CUnitTestTests.Properties.Settings.ConnectionString1" dataTableName="Sheet1$" dataAccessMethod="Sequential"/>14 </dataSources>15 </microsoft.visualstudio.testtools>16 </configuration>
Note: The version in configsection is related to your. net framwork version.
After the configuration is complete, modify the test code:
Here, pay attention to the part I checked out with a red pen.
Right-click to run the test
Test successful !!!
For details about msdn, refer to the following two URLs:
Walkthrough: Use the configuration file to define the data source
Https://msdn.microsoft.com/zh-cn/library/ms243192.aspx
How to: create a data-driven unit test
Https://msdn.microsoft.com/zh-cn/library/ms182527 (v = vs.140). aspx
I hope the summary will be useful to you. If you have any questions, please comments !!