C # Unit Testing

Source: Internet
Author: User

In short, unit testing is a partial test, which is a static class test in a project, a static method test, an instantiation of a class, and a method test for a class. When you have a specific project you can test by running a view of the results, but it can be cumbersome when you have only one class and no complete project, and unit testing solves the problem. Unit testing There are two ways, one is NUnit, and the other is Microsoft's unit test in VS, this will be relatively complex but more comprehensive, I am in the online combination of multiple materials to learn and summarize, will compare the basis, hope to see you have a little help, here in two ways to test the simple example.

First, unit test using NUnit

1, download NUnit, address is: http://www.nunit.org/index.php?p=download

2, download Visual Nunit 2010, Address: http://visualstudiogallery.msdn.microsoft.com/c8164c71-0836-4471-80ce-633383031099

3. Create the project and test

  1.Create a new project in VS2010, because we only need to test the class that we created later, it is not related to what the project is, so you can create the project of the form application or Web application and so on, here I create the console application, for the convenience of viewing, I take the project name as: unit Test using NUnit

  2. Right-click on the reference in Solution Explorer, select Add Reference, locate and add the Nunit.framework.dll file from the NUnit downloaded in step 1 (the default installation path is: C:\Program files \nunit 2.6 \bin\framework\nunit.framewor.dll).

  3. write a generic class, and we'll test the class later. In the right-click Project name, add class, change the class name to Simpleclass, in the Solution Explorer, and the code is as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace unit Testing with NUnit
{
public class Simpleclass
{
<summary>
Division operation
</summary>
public int Division (int num1,int num2)
{
return num1/num2;
}
}
}

  4. once the general class has been written, we will now create a test class. In the right-click Project name, add class, change the class name to TestClass, in the Solution Explorer, and the code is as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using nunit.framework;//to add namespaces

Namespace unit Testing with NUnit
{
[testfixture]//represents the class used for testing
public class TestClass
{
<summary>
Simulation is tested with normal data
</summary>
[test]//represents a test case
public void Divisiontestnatural ()
{
var simpleclass = new Simpleclass ();
var result = Simpleclass.division (10, 5);//Call the division operation in the Simpleclass class with 10 divided by 5
Assert.AreEqual (2, result);//The expected value of the set test is 2, the actual result is the test error if the actual value is not equal to the expectation
}
<summary>
Simulation with anomaly data for testing
</summary>
[Test]
public void Divisiontestnonatural ()
{
var simpleclass = new Simpleclass ();
var result = Simpleclass.division (10, 5);
Assert.AreEqual (3, result);
}
}
}

  5. shortcut key Ctrl+f5 run the project (the information on the Web is written to build the project, but if you test a class and then create a new class to test, you will find that you cannot find your newly created class in visual NUnit, if you do not understand it here, But you'll understand when you're done with the project). Select Other Windows->visual Nunit (I found the data when someone else's data said CTRL+F7 is the shortcut key, but I tried to not work.) If visual NUnit is not found in other Windows then turn vs off and reopen), select the corresponding item in project, Namespace, fixture (when you create the second Test class as I said in the previous article, just build the project without using CTRL + F5 run the project, you will find that you have not found your newly created test class in fixture, and then click the Run button.

Ii. using vs for Unit testing

Waiting to be written ...

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.