Unit Test Via Visual studio-part1

Source: Internet
Author: User



Write at the beginning: Coding Ain ' t done until all the tests run. No unit Test no BB.



-------------------------------------------------------------



This article mainly describes how to do unit testing under visual Studio (2012+). The main content:


  1. Introduction to Unit Testing
  2. Basic features of Unit testing
  3. Unit Testing with vs stubs


Introduction to Unit Testing


There are many types of tests, and unit tests are used by developers to automatically test their own code, using a diagram to illustrate:


The above method represents an independent logic, not that all methods need to be tested, such as private methods, he should belong to other independent logic, can be done in other ways to test


Unit testing is a powerful guarantee of code quality, a confidence guarantee for refactoring code, and a good unit test for individuals that should have the following characteristics:


  1. Run fast, and if your test runs all day long, you won't be willing to write a case, and you won't be running and maintaining it.
  2. Test cases should not depend on each other, do not run a case first run B
  3. Test case should not rely on external resources, such as service,db, should be able to run at any time (such as network disconnection)
  4. Unit tests should be as important as production code and maintained as code is maintained


Use the stub feature for unit testing in VS:



1. First write a code for testing


An interface for sending email

   /// <summary>
    /// interface of email handler
    /// </summary>
    public interface IEmail
    {
        bool SendEmail(string reveiver, string emailBody, string subject);
    }


Email delivery Implementation, call email service to send mail


   /// <summary>
    /// real email handler
    /// </summary>
    class EmailImp : IEmail
    {
        public bool SendEmail(string reveiver, string emailBody, string subject)
        {
            //call email remote service here to send email 
            return true;
        }
    }


The Business Notification class, called Emailimp sends a message, sends a success, returns 1, fails back 0, and of course the real business is sure to do other things.


 public class Notifier
    {
        public IEmail EmailHandler { get; set; }

        public Notifier()
        {
            this.EmailHandler = new EmailImp();
        }

        public int Notify()
        {
            var sendResult = this.EmailHandler.SendEmail("[email protected]", "email body", "emailSubject");
            if (sendResult)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }


2. Now to test the Notify method, simple steps to generate the test solution and basic test code:





3. The above notify should correspond to at least two case:


  1. When the email service returns true
  2. When the Emial service returns false

As mentioned above, unit testing should not depend on external service,db. But programs generally depend on these. If these external service is down. It's dbdown. How the program is tested. How to control the email Service to return true, False, as we need when testing. This is the problem that the stub solves. Stub is the meaning of stub piling, piling is a very important concept in the test.


4. Vs can complete the stub of the program by increasing the fake assembly:




5. After adding the fake assembly, you can see:




6. Next sub's use, directly through the code to show




The example above is clear that two cases are completely independent of external email service. It is easy to verify our own logic by specifying the return value of the email service as you wish.


The main idea of the stub is to dynamically inject the interface at runtime, and dynamically replace the actual execution class with the stub class we specify to accomplish the function we specify.

To test with a stub, the object being fake must be programmed on the interface.


Of course, based on interface programming, the above stub programmers can also implement their own, why, VS has helped us to achieve.



That's the problem. What if the code is not programmed based on an interface? Is there no way to replace simulations like stubs? The next article introduces another feature: Shim.






Unit Test Via Visual studio-part1


Related Article

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.