Unit Testing of code refactoring

Source: Internet
Author: User

Unit Testing of code refactoring

Code refactoring that does not change the original function under the premise of modifying the relationship between the code, simply to make the code more concise, easy to read, maintainability is higher, does not weaken the original function of the program. An important subject in the refactoring process is unit testing--ensuring that every step of the modification does not change the functionality of the program. In order to eliminate cumbersome coding, testing procedures, refactoring tools are essential, through refactoring tools, you can only write a small amount of code to do unit testing.

First, download and install the refactoring tool: ReSharper or other tools: Open the installation, online, ReSharper, extensions and updates, tools--, Visual Studio .

Install the UNit package: Opens a new project (class library), Visual Studio, right-click Solution, NuGet Package Manager, NUnit.

To add an existing item to a project, select the class to test.

Write the test code:

usingnunit.framework;namespacetestclasslibrary{[Testfixture] Public classtestfemalepatient {Privatefemalepatient _femalepatient; [Test] Public voidtestidealbodyweight () {DoubleExpectedresult =161.15626; DoubleRealresult =_femalepatient.idealbodyweight ();        Assert.AreEqual (Expectedresult, Realresult); } [Test] Public voidtestdailycaloriesrecommended () {DoubleExpectedresult =1325.4; DoubleRealresult =_femalepatient.dailycaloriesrecommended ();        Assert.AreEqual (Expectedresult, Realresult); } [SetUp] Public voidcreatefemalepatientinstance () {_femalepatient=Newfemalepatient (); _femalepatient.heightininches= the; _femalepatient.weightinpounds= the; _femalepatient.age= -; }    }}

[Textfixtrue] indicates the unit to be tested

[Text] Decoration method for the method that you want to test

Right-click Project name, Run Unit Test

Green for passing test

Red for Test failed

Yellow for wait test

Unit Testing of code refactoring

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.