Using Xunit for unit testing

Source: Internet
Author: User
Tags xunit

Whether you love or not, unit testing is an essential part of the stability of a software. After Visual Studio 2012, the test browser in VS is also able to integrate with third parties, which is very handy. Currently in. NET Framework of testing tools mainly have NUnit, built-in MSTest and xunit these three tools, this article will briefly explain how to use the Xunit this test framework in VS the Rising star.

Install Xunit:

Xunit installation now does not require plug-in support, directly using NuGet installation of the following two libraries:

    • Pm> Install-package Xunit
    • Pm> Install-package xunit.runner.visualstudio-pre (Visual Studio Test browser support, VS2015 must now have Pre-installed)

To write a test case:

A simple test case is as follows:

public class TestClass1
{
[Fact]
public void Testmax ()
{
Assert.equal (3, Math.max (3, 2));
}

[Fact]
public void Testfail ()
{
Assert.equal (2, Math.max (3, 2));
}
}

Executing test Cases

Execute the Runall in the test browser (without compiling after the modification, it will be compiled automatically)

Test method Description:

To declare a test case:

Xunit do not need to testclass such attribute to mark the test case class, only the following conditions can be met:

    • The test class must be public
    • Test cases tagged with Factattribute

Assertion:

The Assert class is used to validate the output of a test test function.

Assert.equal (3, Math.max (3, 2));

You can also use some extended assertion libraries, commonly known as xunit.should libraries, which are validated in the form of extension functions and look more comfortable.

Pm> Install-package Xunit.should

Math.max (3, 2). SHOULDBE (3);

However, this library and the new version of the Xunit adaptation is not very good, I wrote a simple version here:

    Static class Assertextension    {public        static void shouldbe<t> (this t value, T exprected)        {            if ( Equals (value, exprected))                return;            var msg = $ "is inconsistent with the expected result, the current value is ' {format (value} '), and the expectation is ' {format (exprected} ');            throw new InvalidOperationException (msg);        }        public static void shouldnotbe<t> (this t value, T notexprected)        {            if (! Equals (value, notexprected))                return;            var msg = $ "is inconsistent with the expected result and the current value should not be ' {format (notexprected} ') ';            throw new InvalidOperationException (msg);        }
static string Format (object value) { return value = = null? "NULL": value. ToString (); } }

Construction and destruction:

Xunit does not use the setup and teardown tags to indicate the build and destructor of the test case, it will plug in the test case class every time the test case executes, and if it implements the IDispose interface, it is more concise and straightforward to call the Dispose function. Other words:

    • In the test case class, the constructor points to the data-building operation
    • Point to the data cleanup operation in the Dispose function

Anomaly Testing

Instead of marking an exception capture by attribute, Xunit uses the Assert.throws assertion function directly to verify the exception.

public class TestClass1
{
[Fact]
public void TestException ()
{
Assert.throws<invalidoperationexception> (() = operation ());
}

void operation ()
{
throw new InvalidOperationException ();
}
}

Change the test case name:

[Fact (DisplayName = "max function test")]

To skip a test case:

[Fact (Skip = "Refactoring Not completed")]

Group:

[Trait ("Group", "Category")]

Using Xunit for unit testing

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.