Test the. Net program with nunit-nunit entry

Source: Internet
Author: User

I recently learned the importance of project testing on the Internet. I feel that I have gained a lot. I want to help programmers like me.

 

I am using a strong nunit for. Net program testing,: http://www.nunit.org/index.php? P = download the latest version, and then install it.

 

Next we will start our development and testing. I am writing a simple computing function class.

 

First, create a new class library in.

The code for adding the calculate. CS class is as follows:

namespace NunitTest{    public class Calculate    {        public int Add(int a,int b)        {            return a + b;        }        public int Minus(int a,int b)        {            return a - b;        }        public int Multiply(int a,int b)        {            return a * b;        }        public int Divide(int a,int b)        {            return a / b;        }    }}
Calculate is the class to be tested.
Add the test class calculatetest. CS to the project. First, add nunit. Framework. DLL to the project,
When nunit is installed, the installer has added the nunit assembly to the. NET application of the project. Click Add directly.
 
To use the nunit test project, you must add the client property identifier and add [testfixture] to indicate that this class is used for testing.
    [TestFixture]    public class CalculateTest    {    }
Next, add the Test method: Add [test] to indicate that this method is used for testing. The test method cannot have a return value or a parameter.
    [TestFixture]    public class CalculateTest    {        [Test]        public void TestAdd()        {            Calculate cal = new Calculate();            int result = cal.Add(1, 4);            Assert.AreEqual(5, result);        }        [Test]        public void TestDivide()        {            Calculate cal = new Calculate();            int result = cal.Divide(3, 1);            Assert.AreEqual(3, result);        }    }

 

Assert is the "assertion" of the test: In the test framework, assertion is the core of the unit test. We need to assert its program in the test. If an assertion fails, method calls do not return values, and an error is reported. If a test contains multiple assertions, none of those that follow the failure assertions will be executed, so it is best to have only one asserted for each test method.

 

Start nunit, file ---- open project

 

Add nunittest. dll.

 

We can see the two test methods we added. Testadd and testdivide. Click Run to start testing

 

 

 

Green indicates that the test is successful.

 

But do you think that the Division testdivide () in the test has the division of zero divide. We did not make any judgment in the program, and we directly returned the result.

Let's change the test class.

        [Test]        public void TestDivide()        {            Calculate cal = new Calculate();            int result = cal.Divide(3, 0);            Assert.AreEqual(3, result);        }

Run nunit,

 

Although the operation here is very simple, it is necessary to test the complex situation.

        [Test]        [ExpectedException(typeof(DivideByZeroException))]        public void TestDivide()        {            Calculate cal = new Calculate();            int result = cal.Divide(3, 0);            Assert.AreEqual(3, result);        }
 
[Expectedexception (typeof (dividebyzeroexception)] indicates the test class I expect to throw an exception whose divisor cannot be zero.
 
Run nunit again and the test passes.
 
 
 

Author: peng _ lovley
Source: http://blog.csdn.net/dupeng0811
The copyright of this article is shared by the author and csdn. You are welcome to reprint it. Please provide the original article connection on the article page.

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.