C # Unit Testing

Source: Internet
Author: User

1. Use the VS own Unit test component. Project Reference Microsoft.VisualStudio.TestTools.UnitTesting. Mark the TestClass attached property on the test class, mark the TestMethod attached property "test"-"run"-"all tests" on the method you want to test, or choose to analyze code coverage to see the code ratios that are covered by the current test run. Description: 1. Use assert in the method to test the static class, which provides a lot of static methods for testing use: 2. The method to be tested must be void, without a return type: 2. Using NUnit  Download the installation package in Http://www.nunit.org/index.php?p=download, after installation, reference the Nunit.framework.dll in the installation directory in the project. The using nunit.framework;   in the class is then tagged with the testfixture attached property on the test class, and the method to be tested is to tag the test attached property. There are two types of test operations a. The test project is an EXE or DLL class library. After the build is compiled, open the Nunit.exe program, and then select Open Project, then file type to select the assembly, open the C # project to compile the generated EXE or DLL file:    b. Open the project's property page, debug-Start action section, select Start an external program, and then select the Nunit.exe program. So F5 debugging, you can open directly with NUnit, view the unit test execution information         Description: 1. Similar to the use of Microsoft.VisualStudio.TestTools.UnitTesting, it is also tested using the static method provided in the Assert class, which is slightly different from the static class:     comparing Microsoft.VisualStudio.TestTools.UnitTesting's Assert static class, you can see that the static methods provided are broadly consistent, even with the same parameter names. 2. The method of testing must also be void. You can verify that you tag the test attached property with a method that has a return value.
        [Test]        publicint  TESTNUNIT4 ()        {            assert.greater (  One " don't know the exact result " );             return 1 ;        }

After running, the corresponding test method is not expected to run, (note that the Invalid, not the assertion failed Failed), because the method has a non-empty return type, and NUnit expects no method to return the result.

3. If you want a method to temporarily not run the test (or ignore it when testing), you can attach the Ignore property to it.

[         Test]        [Ignore ("Themethodis not a ready yet")]//  Method flags the Ignore property, which ignores the operation of this method in NUnit and renders the yellow color. public        void  TestNUnit3 ()        {            assert.areequal (  "it is not equal");        }
Note: The Ignore property can be attached to a method or attached to a class so that test methods in the entire class will ignore the test run. As shown below:
[Testfixture] [Ignore ("The class is isn't ready yet")]     Public classtest {[Test] Public voidTestnunit () {Console.WriteLine ("12121"); Assert.AreEqual (1,2,"It is not equal"); } [Test] Public voidTestNUnit1 () {assert.areequal (1,1,"It is not equal"); } [Test] Public voidTestNUnit2 () {assert.areequal (1, Assert.counter);    } . . . }

In 4.Errors and failures, the following code is displayed for the test method in the event of an error and an assertion failure:
        [Test]        publicvoid  Method1 ()        {            var1;             var0;             var test = A/ b;            Assert.pass ("assert Pass");        }

The contents of the output in the test run are displayed in 5.Text output. The code is as follows:
[Testfixture] Public classtest {[Test] Public voidTestnunit () {Console.WriteLine ("12121"); Assert.AreEqual (1,2,"It is not equal"); }        //omit other test methods ...[SetUp] Public voidSetUp () {Console.WriteLine ("SetUp"); } [TearDown] Public voidTearDown () {Console.WriteLine ("TearDown"); }    }

Run effect

There are two outputs of setup and teardown. The additional properties of the Setup function are executed before each test run and can be used as initialization work; teardown is executed after each test run and can be used as a release resource. There are two additional properties: Testfixturesetup and Testfixtureteardown also provide the same purpose, but in the range of test fixture. Reference: Http://confach.cnblogs.com/archive/2005/06/20/177817.aspx

C # Unit Testing

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.