NUnit3 Test Adapter vs2015

Source: Internet
Author: User

Installation of NUnit

Preface: what is NUnit?

NUnit is a unit testing framework specifically designed for. NET to write . NUnit is the 4th flagship product of the Xunit family , written entirely in the C # language and fully utilized in writing. NET features, such as reflection, customer attributes, and so on . The most important point is that it is suitable for all . NET language .

First, the installation of NUnit

1, download NUnit(nunit-2.6.4.msi), double click to install until completed.

It is worth reminding that if we are going to write a test class, we need to reference nunit.framework

Second, view the test results (recommended installation plugin)

In general, we want to see the results after running the test, mainly through three ways:

1.NUnit GUI

This is the client after the NUnit installation is complete, the diagram is its interface, we run the test class by loading the program's DLL or assembly to see the test conditions and results.

2. NUnit command line

Is the matter of testing with the shell(the shell of the graphical interface ), where we need to set the environment variable ( add the NUnit installation path under the path path \ c \ ProgramFiles (x86) \nunit 2.6.4\bin)

3,Visual Studio plug-in Nunit Test Adapter(This is the integration of VS and Nunit)

In fact, I prefer this kind of, do not need like the first two, every time to open two windows, and this, we only need to install a plug-in NUnit, you can directly in the VS test and see the test results.

Plugin installation: In the VS toolbar → tools → extensions and updates (U)→ in the pop-up box, select "Online" in the left-most, and then enter the keyword "NUnit" in the search boxto find out "Nunit3 Test Adapter or NUnit Test Adapter", click Install.

Moq & Rhinomocks

Testing with mock objects typically has the following three key steps:

    1. Use interfaces to describe objects that need to be tested
    2. Implement this interface for the actual product code
    3. Implement this interface in a mock object for testing purposes

In the process of using mock objects, the design principle of "interface-oriented programming" is fully embodied, and the good design of the class is also promoted.

Self-fulfilling mock objects is quite tedious work, and fortunately, there are several excellent mock frameworks available in the. NET world that are currently the most commonly used MOQ and rhino mocks frameworks. The latest versions of the two are increasingly consistent in the use of the mocking API, relying on lambda expressions, generics, and extension methods to make great improvements, with the goal of allowing mock objects to integrate with multiple unit test frameworks in a more natural way, with a clear syntax to describe expectations, parameter constraints, The return value, etc., greatly facilitates the use of the developer.

Since both MOQ and Rhino mocks use castle Dynamicproxy to dynamically generate proxy classes, there are limitations on objects that require mock: The method being tested must be of type virtual.

Here is an example of the different implementations of the two (this example is from the samples in the MOQ source code package, just slightly modified to show the characteristics of both):

The object that needs to be tested is as follows:

Expand

The following is the unit test code implemented with these two mock frameworks:

MOQ 4.0 Rhino Mocks 3.6
[test]public void Testpresenterselection () {//arrange var MView = n    EW mock<iordersview> ();    var mrepository = new mock<irepository<order>> ();    var presenter = new Orderspresenter (Mview.object, mrepository.object); Check that the presenter have no selection by default Assert.null (presenter.    Selectedorder); Raise Event mview.raise (io + = io.    Orderselected + = null, new OrderEventArgs {order = New Order ("MOQ", 50)}); Assert Assert.notnull (presenter.    Selectedorder); Assert.AreEqual ("MOQ", presenter. Selectedorder.productname);} 
[test]public void Testpresenterselection () {//arrange var MView = M    Ockrepository.generatemock<iordersview> ();    var mrepository = mockrepository.generatemock<irepository<order>> ();    var presenter = new Orderspresenter (MView, mrepository); Check that the presenter have no selection by default Assert.null (presenter.    Selectedorder); Raise Event mview.raise (io + = io.    Orderselected + = null, NULL, new OrderEventArgs {order = New Order ("MOQ", 50)}); Assert Assert.notnull (presenter.    Selectedorder); Assert.AreEqual ("MOQ", presenter. Selectedorder.productname);} 
[test]public void Testretrieveorders () {//arrange var MView = new M    Ock<iordersview> ();    var mrepository = new mock<irepository<order>> ();    var presenter = new Orderspresenter (Mview.object, mrepository.object);    list<order> defaultorders = new List<order> {New Order ("MOQ"), New Order ("Rhinomock")}; Mrepository.setup (r = R.findall ()).    Returns (defaultorders); Exercise mocks presenter.    OnInit (); Assert Mview.verifyset (v = v.orders = defaultorders);} 
[test]public void Testretrieveorders () {    //arrange    var mView = Mockrepository.generatemock<iordersview > ();    var mrepository = mockrepository.generatestub<irepository<order>> ();    var presenter = new Orderspresenter (MView, mrepository);    list<order> defaultorders = new list<order>         {New Order ("MOQ"), New Order ("Rhinomock")};    Mrepository.stub (ir + = ir. FindAll ()). Return (defaultorders);    Exercise mocks    presenter. OnInit ();    Assert    mview.assertwascalled (v = v.orders = defaultorders);}

Conclusion

With the example above we can easily see that both syntax and API are very close to each other, using either of them can be easily implemented for your testing purposes.

References
    1. Quickstart-moq
    2. [email protected] -Rhino Mocks 3.5
    3. Daniel Cazzulino ' s blog-why do we need yet another NET mocking framework
Category: C #

NUnit3 Test Adapter vs2015

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.