Vs2010 unit test

Source: Internet
Author: User
Tags dsn
C # _ unit test in vs2010

In vs2010, unit testing has powerful functions, making it easy to establish unit testing and write unit testing code, and to manage and run unit testing, private accessors allow you to perform unit tests on private methods as well as data-driven unit tests.

1Create a unit test project

1.1Generate a unit test from the tested code

1) instance: Create a console application in VC # mode. The project name is cunittest.

2) Enter simple addition, subtraction, multiplication, and Division function code, as shown in

3) You can follow the steps below to create a unit test

(1) In the add method body, right-click and choose "create unit test" from the menu ",

(2) In the displayed "create unit test" interface, the add method is automatically checked, indicating the basic framework for creating unit test code for this method. Click OK.

(3) Click OK. In the new test project, enter the name of the new unit test project to be created, and then click "CREATE, A new unit test code project is automatically created.

(4) you can see an additional "addtest" project in "Resolution file resource manager". We can see that the "addtest" project references the assembly of the project under test, and unit test frame. visual Studio. qualitytools. unittestframe, and automatically generate two C # code files assemblyinfo. CS and programtest. CS

(5) programtest. as shown in the CS code, a "programtest" class is automatically generated and identified as a unit test class and a "addtest" test method by using [testclass ,. And use [testmethod.

(6) details of the programtest. CS code file

[Testmethod ()]: indicates that the following code is a test case.

Int A = O; // todo: Initialize to an appropriate value.

Int B = 0; // todo: Initialize to an appropriate value.

These two statements are the input parameters of the function to be tested. We need to modify the value, that is, the place where we enter the test case.

Double expected = 0; // todo: Initialize to an appropriate value.

Double actual;

These two statements are easy to understand. The first one is to define the expected value and initialize it, and the last one is to define the actual value. Default

Assert. areequal (expected, actual );

Assert can be understood as assertion: In vsts, unit testing is based on assertion.

In the default code, assert. Inconclusive indicates that this is an unverified unit test. You can comment it out in the actual program.

1.2Add Unit Test Project

(1) another unit test method is to add a unit test project independently, add a new project to the solution, and select "test project" as the project type ",

(2) Click OK to automatically create a new unit test project. In Solution Explorer, you can see the newly added test project testproject2 ". Compared with "testproject2" and "addtest", we can find that "testproject2" lacks reference to the tested project assembly, and only references the DLL of the unit test framework "Microsoft. visual Studio. qualitytools. unittestframe"

2, Write Test Methods

The basic method of unit test is to call the function of the tested code, enter the parameter value of the function, obtain the returned result, and then compare it with the expected test result. If it is equal, the test is passed, otherwise, the test fails.

1. Use of assert class

Assert. Inconclusive () indicates an unverified test;

Assert. areequal () test whether the specified value is equal. If the value is equal, the test passes;

Aresame () is used to verify that the specified two object variables point to the same object. Otherwise, it is considered as an error.

Arenotsame () is used to verify that the specified two object variables point to different objects. Otherwise, it is considered as an error.

Assert. istrue () test whether the specified condition is true. If it is true, the test passes;

Assert. isfalse () test whether the specified condition is false. If it is false, the test passes;

Assert. isnull () test whether the specified object is a null reference. If it is null, the test passes;

Assert. isnotnull () test whether the specified object is not empty. If not, the test passes;

2. Use of the collectionassert class

Used to verify whether the object set meets the conditions

Use of the stringassert class

Used to compare strings.

Stringassert. Contains

Stringassert. Matches

Stringassert.tar twith

3Data-driven unit testing

Data-driven unit test refers to the input data of a unit test to traverse all rows of a data source. Read data from no row of the data source and pass it to the test method.

3.1,AccessData-driven unit test

1) Open the test View window and select test view.

2) In the test View window, select the unit test method to be configured as data-driven, and press F4. The unit test attribute window is displayed.

3) edit the "data connection string" attribute, click this attribute in the "properties" window, and then click the ellipsis (...). This opens the "Select data source" dialog box, which lists several possible data sources, including ODBC, Microsoft SQL Server, and Microsoft Access. After you select a data source, a dialog box specific to the data source type is displayed. You can use this dialog box to configure the Connection Properties of the data source. After configuring the data connection, the connection string will appear as the value of "data connection string. This string is also stored as an attribute of the unit test method.

4) on this page, select an acess table data. MDB and click "OK" to complete the settings. The "unit test properties" window is displayed. You can see that the data source has been set.

5) After establishing a connection with 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 selected from this list is the table in which the row is retrieved during unit test. Like other attributes such as "data connection string", "data table name" is also stored as an attribute of the unit test method.

6) in "data access method", select "sequence" or "random". The default value is "sequence ". This setting indicates the order in which records are retrieved from the data source table.

You can see that a row has been added before the test method:

7) Use of data sources

The data is provided to a running unit test through the datarow and dataconnection attributes of the testcontext class. The following describes how to use the datarow attribute of the testcontext class to read data rows.

8) the table in the acess data source is

3.2, ReadExcelMethod:

1) create a TXT file on the desktop and change the file name to data. DSN.

2) Select "database connection string", click the button in the right column, change the data source to a Microsoft ODBC data source, and click "OK ".

3) Select use connection string and click Generate

4) Select the Excel Data Source driver and click "Next"

5) Select data. DSN to save the file for the data source and choose "Next ".

6. Select the example input file data.txt and click "OK"

7) Select the sheet where the use case is located and select "finish"

8) data source usage code

4Unit Test Run

There are two ways to run a unit test: Debugging and running. You can debug the unit test code just like debugging common code. You can also run the Code directly. The unit test result is displayed on the "test result" Page. Double-click the test result, you can obtain detailed information about the test results. Code coverage of unit tests can be displayed on the code coverage result interface.

5Additional test attributes

"Additional test attributes ". By default, all comments are commented out. You only need to cancel the comments. This function is used to increase the flexibility of testing. Specific attributes include:

[Classinitialize ()] Run the code before the first test of the class.

[Classcleanup ()] Run the code after all tests in the class are run.

[Testinitialize ()] Run the code before running each test

[Testcleanup ()] Run the code after each test is run

If you enter the test execution time into the log during the test execution, the Code is as follows:

Source: http://zxianf.blog.163.com/blog/static/30120701201101011757499/

Vs2010 unit test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.