TDD lab 2: Use the unit test function of vsts

Source: Internet
Author: User

The function we want to achieve is to automatically send an email to us at the time of payment. If the salary is less than 0.1 million (the dream of many of us), let him send failure.

1. First, we will build a test project and a test class.

Rename the test class to salarymessagetest.

[Testmethod]

Public VoidSendemailtome ()

{

Salarymessage =NewSalarymessage ();

}

Press Ctrl + Shift + B and the compilation fails.

2. Now we create a salarymessage class to pass the test. But we think we should separate it from this test project, and we will create a class library for vsunitdemo.

 

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingSystem. text;

 

NamespaceVsunitdemo

{

Public Class Salarymessage

{

 

}

}

 

3. Introduce the namespace to the test project

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingMicrosoft. visualstudio. testtools. unittesting;

UsingVsunitdemo;

CTRL + R, a test all, pass

4. Now, we need an email containing the email address, name, content, and write test.

[Testmethod]

Public VoidMail_has_emailaddress_name_content ()

{

Mail M =NewMail

{

Emailaddress =Wangdeshui@gmail.com",

Name ="Jack Wang",

Content ="Well done! "

};

Assert. Areequal (M. Name,"Jack Wang");

Assert. Areequal (M. emailaddress,Wangdeshui@gmail.com");

Assert. Areequal (M. content,"Well done! ");

}

CTRL + Shift + B compilation fails, prompting that there is no mail class. Now we add the mail class and related attributes to vsunitdemo.

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingSystem. text;

 

NamespaceVsunitdemo

{

Public Class Mail

{

Public StringEmailaddress {Get;Set;}

Public StringName {Get;Set;}

Public StringContent {Get;Set;}

}

}

CTRL + R, a we do not need to switch back to the test project

[Testmethod]

Public VoidSendemailtome ()

{

SalarymessageSalarymessage =New Salarymessage();

Assert. Isnotnull (salarymessage );

MailM =New Mail

{

Emailaddress =Wangdeshui@gmail.com",

Name ="Jack Wang",

Content ="Well done! "

};

BoolIssuccess = salarymessage. sendemailtome (m );

Assert. Istrue (issuccess );

}

Compilation failed

 

5. Add the sendemailtome Method

NamespaceVsunitdemo

{

Public Class Salarymessage

{

Public BoolSendemailtome (MailM)

{

If(M! =Null)Return True;

Return False;

}

}

}

CTRL + R, a test pass

6. Add a request. If the name, address, or content of the sent email is null, a parameter exception is thrown.

[Testmethod]

[Expectedexception(Typeof(Argumentnullexception),"All fields of mail is required")]

Public VoidMail_emailaddressisrequired_nameisrequired_contentisrequired ()

{

SalarymessageSalarymessage =New Salarymessage();

Assert. Isnotnull (salarymessage );

MailM1 =New Mail

{

Emailaddress =Wangdeshui@gmail.com",

Name ="",

Content ="Well done! "

};

MailM2 =New Mail

{

Emailaddress =Wangdeshui@gmail.com",

Name ="",

Content ="Well done! "

};

MailM3 =New Mail

{

Emailaddress =Wangdeshui@gmail.com",

Name ="",

Content ="Well done! "

};

Assert. Istrue (salarymessage. sendemailtome (M1 ));

Assert. Istrue (salarymessage. sendemailtome (m2 ));

Assert. Istrue (salarymessage. sendemailtome (m3 ));

}

Place the mouse in the test method and press Ctrl + R to run the test method.

6. ModifyCodeAs follows: Ctrl + R, a passed the test

7. Now I suddenly remembered that there was no salary in the email. Oh, this is a big mistake.

8. Add the salary attribute, CTRL + R, and a pass the test.

9. We are worried about testing whether the code is overwritten. Open the test run configuration file. If you do not have this option, choose solutin to add a new project and add the test run configuration file.

 

CTRL + R, a right-click test results, and select code coverage

Obviously, our public void mail_emailaddressisrequired_nameisrequired_contentisrequired () is not fully covered. Obviously, we only tested three control combinations. I will not demonstrate them here.

 

10. Now, if the salary is less than 100000, we will let him send failure, return false, add Test

[Testmethod]

Public VoidSendemailtome_returnfalse_ifsalarylessthan100000 ()

{

SalarymessageSalarymessage =New Salarymessage();

Assert. Isnotnull (salarymessage );

MailM =New Mail

{

Emailaddress =Wangdeshui@gmail.com",

Name ="Jack Wang",

Content ="Well done! ",

Salary= 9000

};

BoolIssuccess = salarymessage. sendemailtome (m );

Assert. Isfalse (issuccess );

}

CTRL + R, a test failed

11. modify the code

NamespaceVsunitdemo

{

Public Class Salarymessage

{

Public BoolSendemailtome (MailM)

{

 

If(M! =Null&&!String. Isnullorempty (M. emailaddress)

&&!String. Isnullorempty (M. Name )&&!String. Isnullorempty (M. Content)

)

{

ReturnIsenough (M. Salary )?True:False;

 

}

Else

{

Throw New Argumentnullexception("Parameter is null");

}

}

 

Private BoolIsenough (DecimalSalary)

{

ReturnSalary <100000?False:True;

}

}

}

 

CTRL + R, a pass the test

12. the problem is that we have introduced a private method. How can we test whether this method is correct? Generally, we do not test private methods, but now we do not want to debug it, that is, we want to test private methods, it doesn't matter. vsts provides us with such a function.

Add a test method

 

OK. This article mainly demonstrates the power of the unit test tool of vsts In TDD. I only demonstrated some functions. If you are interested, you can study it on your own.

Appendix:

List some shortcut keys

CTRL + R, a test all tests in Solution

CTRL + R, t test method of the current mouse

CTRL + R, n test all tests of the current namespace

CTRL + R, C test all tests of the current class

 

CTRL + R, CTRL + A all tests in the debugging Solution

CTRL + R, CTRL + T test method for debugging the current mouse

CTRL + R, CTRL + N debug all tests of the current namespace

CTRL + R, CTRL + C debug all tests of the current class

 

Vsts unit tests also provide many attributes, such as [ignore], and can filter tests.

 

Conclusion: I think the unit test of vsts is very good and integrated with IDE very well. I used nunit all the time, but now I think it is very useful.

 

Author: Wang Deshui. I got up at home today and got awake. I tried TDD with the unit test of vsts.

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.