C # unit test,

Source: Internet
Author: User

C # unit test,
1. Use the unit test component that comes with.The project references Microsoft. VisualStudio. TestTools. UnitTesting. Mark the TestClass additional attribute on the test class, mark the TestMethod additional attribute on the method to be tested [test]-[run]-[All tests], you can also choose to analyze the code coverage rate, check the code ratio covered by the current test. Note: 1. the static class Assert is used in the method for testing. Many static methods are provided for testing: 2. the test method must be void without the return type ,:2. Use NUnitIn http://www.nunit.org/index.php? P = download the installation package. After installation, reference nunit. framework. dll in the installation directory in the project. Then, in the class using NUnit. Framework; in the Test class, mark the TestFixture additional property. In the method to be tested, mark the Test additional property. There are two types of test operations: a. The test project is an exe or dll class library. After compilation, Open the nunit.exe program, select the Open Project, select the Assembly for the file type, and Open the exe or dll file generated by C # Project Compilation: B .open the program page. Then, select the program and then choose the nunit.exe program. In this way, you can directly use nunit to open F5 debugging and view the execution information of unit test: 1. and Microsoft. visual Studio. testTools. the usage of UnitTesting is similar. The static method provided in the Assert class is used for testing. The difference is that this Assert is not a static class: Comparison with Microsoft. visual Studio. testTools. the Assert static class of UnitTesting. It can be found that the provided static methods are basically the same, and even the names of the connected parameters are the same. 2. the test method must also be void. Verifiable: Mark the Test additional attribute for a method with a returned value.

[Test] public int TestNUnit4 () {Assert. Greater (10, 11, "do not know the specific result"); return 1 ;}

After running, the system will prompt that the corresponding test method cannot run (note:InvalidInstead of assertion failureFailed), Because the method has a non-null return type, while nunit expects a method without returning results.

3. If you want a method to temporarily not run the test (or Ignore it during the test), you can attach the Ignore attribute to it.

[Test] [Ignore ("the method is not ready yet")] // the method flag the Ignore attribute, and the running of this method is ignored in nunit, rendering yellow. Public void TestNUnit3 () {Assert. AreEqual (10, 11, "it is not equal ");}
Note: The Ignore attribute can be attached to methods or classes, so that test methods in the entire class will Ignore the test run. As follows:
    [TestFixture]    [Ignore("the class is not ready yet")]    public class Test    {                [Test]        public void TestNUnit()        {            Console.WriteLine("12121");            Assert.AreEqual(1, 2, "it is not equal");        }        [Test]        public void TestNUnit1()        {            Assert.AreEqual(1, 1 ,"it is not equal");        }        [Test]        public void TestNUnit2()        {            Assert.AreEqual(1, Assert.Counter);        }        .        .        .    }

4. In Errors and Failures, the following code displays the information about the test method in the case of Errors and assertion Failures:
        [Test]        public void Method1()        {            var a = 1;            var b = 0;            var test = a / b;            Assert.Pass("assert pass");        }

5. Text Output displays the Output content during the test. The Code is as follows:
[TestFixture] public class Test {[Test] public void TestNUnit () {Console. writeLine ("12121"); Assert. areEqual (1, 2, "it is not equal");} // omit other test methods... [SetUp] public void SetUp () {Console. writeLine ("SetUp");} [TearDown] public void TearDown () {Console. writeLine ("TearDown ");}}

Running Effect

There are two outputs: SetUp and TearDown. The additional property SetUp is executed before each test is run and can be used for initialization. TearDown is executed after each test is run and can be used to release resources. There are two additional attributes: TestFixtureSetUp and TestFixtureTearDown provide the same purpose, but are in the test fixture range. Reference: http://confach.cnblogs.com/archive/2005/06/20/177817.aspx

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.