Introduction to nunit

Source: Internet
Author: User

Nunit is a unit testing framework of the. NET platform, developed from JUnit. It is powerful in supporting all. NET languages.

Http://www.nunit.org for nunit

Introduction 1:

Layout:

  • Left side: Each unit test we write
  • Right: Test progress bar
  • Test execution status: the color of the progress bar.
    1. Green: All test cases run successfully
    2. Yellow: some tests are ignored, but none fail.
    3. RED: failed to execute test cases

Text window label:

Errors and failures: displays failed tests

Tests not run: The test is not executed.

Console. Error: displays the error message generated by running the test. These messages are output by the application code using console. Error.

Console. Out: displays the text message of the output stream printed from the running test to the console. Error.

Bottom Status Bar: indicates the status of the currently running test.

Ready: Ready

Running: Testing in progress (running: Test-name)

Completed: When all tests are completed

Test Cases: indicates the total number of test cases in the loaded Assembly, that is, the number of leaf nodes in the test tree.

Tests run: number of completed tests

Failures: the number of failures in all tests so far

Time: test run time (in seconds)

Introduction 2:

Common attributes

Testfixture attribute: Mark this class as the test class containing the method to be tested. Restrictions on this test class:

  1. The access method must be public; otherwise, nunit cannot see its existence.
  2. There must be a default constructor; otherwise nunit will not construct it.
  3. Constructor should have no side effects, because nunit often constructs this class multiple times at runtime.

Test attribute: a method that marks a class (this class has been marked as testfixture) can be tested. Restrictions on this test method:

  1. The access method must be public.
  2. Parameters are not allowed.
  3. No return value

Introduction 3:

Use

First, install nunit. The run file after installation is "nunit.exe". Its Directory is c: \ Program Files (x86) \ nunit 2.6.3 \ bin.

Next, create a project:

1. Open Visual Studio

2. Create a class library project

3. Add a reference to "nunit. Framework. dll" in the class library project. The installation location of this class library is c: \ Program Files (x86) \ nunit 2.6.3 \ bin \ framework

4. Add a class file and add the namespace "using nunit. Framework;" to the class file ;"

5. Add the property (testfixtrue) to the class and (TEST) to the test method, as shown below:

The completed class file is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using NUnit.Framework;namespace Demo_Nunit{    [TestFixture]    public class Class1    {        [Test]        public void Add()        {            int a = 1;            int b = 2;            int sum = a + b;            Assert.AreEqual(sum, 3);        }        [Test]        public void Multiply()        {            int a = 2;            int b = 3;            int multi = a * b;            Assert.AreEqual(multi, 5);        }        [Test]        [Ignore("This is ignored")]        public void Divid()        {                    }    }}

Preparation
1. Click the class library project, right-click, and select Properties.

2. Select the debug tab and set start action to start an external project. Select c: \ Program Files (x86) \ nunit 2.6.3 \ bin \ nunit.exe"

3. compile the project and start

4. Verify that nunit is started. Select File and open project and select the compiled class library file.

Click the test to be executed and click Run.

Introduction 4:

Assert

Assert (assert) is a class, including the following static methods:

1. Assert. areequal (Object expected, object actual [, string message])

Verifies that two objects are equal if they are not equal, an nunit. framwork. assertionexception is thrown

Parameter description:

Expected: Expected Value (usually hard-coded)

Actual: the actual value of the tested code,

Message: an optional message that will be reported when an error occurs.

When comparing floating-point numbers (float or double), you must specify an additional error parameter.

2. Assert. areequal (Object expected, object actual, float tolerance [, string message])

Parameter description:

Tolerance: The specified error, that is, accurate to X digits after the decimal point. For example, precise to four digits after the decimal point, assert. areequal (0.6667, 2.0/3, 0.0001 );

3. Assert. arenotequal (Object expected, object actual)

Asserts that two objects are not equal

4. Assert. aresame (Object expected, object actual [, string message])

Asserts that two objects refer to the same object

Verify whether the expected and actual parameters reference the same object.

5. Assert. arenotsame (Object expected, object actual [, string message])

Asserts that two objects do refer to the same object

6. Assert. isnull (object [, string message])

7. Assert. isnotnull (object [, string message])

8. Assert. istrue (bool condition [, string message])

9. Assert. isfalse (bool condition [, string message])

10. Assert. Fail ([String message])

Failed the test immediately

This assertion is used to mark a branch that should not be reached.

Introduction to nunit

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.