In VS2010, unit tests are powerful, making unit tests and Writing unit test code, and managing and running unit tests simple, with private accessors allowing unit testing of private methods and supporting data-driven unit testing.
1. Build unit test Project 1.1, generate unit test from tested code
1) Example: Create a console application in vc# mode, project named Cunittest
2) Enter a simple add, subtract, multiply, divide function code, as shown in
3) The unit test can be established as follows:
(1) In the Add method body, right-click, select "Create Unit Test" in the menu,
(2) in the "Create Unit Test" interface that appears, the Add method is automatically ticked, indicating the basic framework for creating unit test code for this method, click the OK button
(3) When you click OK, in the new test project, enter the name of the new project for the unit test that you want to create, and then click the Create button to automatically create a new unit test code project.
(4) in the "Resolve File Explorer" You can see a more "Addtest" project, you can see that the "Addtest" project refers to the project of the Test Assembly, and unit Test framework Microsoft.VisualStudio.QualityTools.UnitTestFrame, and automatically generates two C # code files AssemblyInfo.cs and ProgramTest.cs
(5) as shown in the ProgramTest.cs code, you can see that an "Programtest" class is automatically generated and identified as a unit test class using [TestClass ()] and a "addtest" test method. Identified with [TestMethod ()].
(6) ProgramTest.cs code file details
[TestMethod ()]: illustrates that the following code is a test case
Int a = O; TODO: Initialize to the appropriate value
int b = 0; TODO: Initialize to the appropriate value
These two sentences are the input parameters of the measured function, and we need to modify its value, which is where we enter the test case.
Double expected = 0; TODO: Initialize to the appropriate value
Double actual;
These two words are easy to understand, the previous sentence is to define the expected value and initialize it, after the sentence is to define the actual values. Default
Assert.AreEqual (expected, actual);
Assert can be understood here as an assertion: unit testing in VSTS is based on assertion testing.
The default code in Assert.Inconclusive indicates that this is an unverified unit test. It can be commented out in the actual program.
1.2. Add Unit Test Project
(1) Another unit test method is to add a unit test project independently, add a new project to the solution, select the project type as "Test Project",
(2) When you click OK, a new unit test project is automatically generated, and the newly added test project "TestProject2" is visible in Solution Explorer. Comparing "TestProject2" and "addtest", it is found that "TestProject2" has fewer references to the project assembly being tested, only referencing the DLL of the unit test framework " Microsoft.VisualStudio.QualityTools.UnitTestFrame "
2. Writing test methods
The basic method of unit testing is to call the function of the code under test, enter the function's parameter value, get the return result, and then compare with the expected test results, if the equivalence is considered to pass the test, otherwise it is considered that the test does not pass.
1, the use of the Assert class
Assert.Inconclusive () indicates a test that is not validated;
Assert.AreEqual () tests whether the specified values are equal, and if so, the test passes;
Aresame () is used to verify that the specified two object variables are pointing to the same object, otherwise it is considered an error
Arenotsame () is used to verify that the specified two object variables are pointing to different objects, otherwise it is considered an error
Assert.istrue () tests if the specified condition is true, and if true, the test passes;
Assert.isfalse () tests whether the specified condition is false and if false, the test passes;
Assert.isnull () tests whether the specified object is a null reference, and if it is empty, the test passes;
Assert.isnotnull () tests whether the specified object is non-null, and if not NULL, the test passes;
2, the use of Collectionassert class
Used to verify whether the collection of objects satisfies the criteria
Use of the Stringassert class
Used to compare strings.
Stringassert.contains
Stringassert.matches
Stringassert.tartwith
3. Data-Driven Unit testing
A data-driven unit test refers to the input data of a unit test that traverses all rows of a data source. Read data from a data source without a row and pass it on to the test method
3.1. Access data-driven Unit test
1) Open the Test View window and select Test View
2) in the Test View window, select the unit test method that you want to configure as a data-driven mode, and then press F4 to open the Properties window for the unit test
3) Edit the data connection string property, click the property in the Properties window, and then click the ellipsis (...). This opens the Select a Data Source dialog box, which lists several possible data sources, including ODBC, Microsoft SQL Server, and Microsoft Access. Selecting a data source opens a dialog box specific to that data source type, and you can use this dialog box to configure the connection properties for that data source. After the data connection is configured, the connection string appears as a value for the data connection string. This string is also stored as a property of the unit test method
4) In this interface, select a acess table Data.mdb, click the "OK" button to complete the setup and return to the "Unit Test Properties" window. You can see that the data source is already set up.
5) After establishing a connection to the data source, you can select a data table. When you click the drop-down list in the Value column of the Properties window, the tables in the connected database are listed. The table you select from this list is the one that will retrieve the rows in the unit test when you run it. Like other properties such as the data connection string, the data table name is also stored as a property of the unit test method.
6) In the data access method, select order or random, and the default value is Order. This setting represents the order in which records are retrieved from the table in the data source.
As you can see, a line has been added before the test method:
7) Use of data sources
Data is provided to a running unit test through the DataRow and DataConnection properties of the TestContext class. The following is the use of the DataRow property of the TestContext class to read into the data row
8) The table in the Acess data source is
3.2. How to read Excel:
1) Create a new TXT file on the desktop and change the file name to Data.dsn
2) Select "Database connection string", click the button on the right column, change the data source to Microsoft ODBC data source, click "OK" button
3) Select Use connection string, click Generate
4) Select the driver for the Excel data source and click "Next"
5) Select Data.dsn to save the file for the data source and always select Next.
6) in the pop-up selection workbook, select the input file for the use case data.txt, and click "OK"
7) Select the sheet page where the use case is located and select "Done"
8) Usage Code of the data source
4. Unit Test operation
Unit tests run in two ways: Debug and run. You can debug the unit test code just like normal code, or you can run it directly, and the results of the unit test will be displayed in the test Results screen, and you can double-click the test results to get detailed information about the test results. Code coverage for unit tests can be shown in the Code coverage results interface.
5. Additional Test Properties
Additional test properties. The default is commented out, as long as we cancel the comment can be used. The addition of this feature is largely to increase the flexibility of testing. The specific attributes are:
[ClassInitialize ()] Run code before running the first test of a class
[ClassCleanup ()] Run code after all tests in the class are run
[TestInitialize ()] Run the code before running each test
[TestCleanup ()] Run code after each test run
If the test execution time is entered into the log when the test is executed, the code is as follows
Go C # unit tests in VS2010