NUnit

Last updated
NUnit
Original author(s) Charlie Poole, James Newkirk, Alexei Vorontsov, Michael Two, Philip Craig, Rob Prouse, Simone Busoli, Neil Colvin
Developer(s) The NUnit Project,
.NET Foundation
Stable release
4.0.0 / 26 November 2023;1 day ago (2023-11-26)
Repository github.com/nunit
Written in C#
Operating system .NET Framework, Mono
Type Unit testing tool
License MIT License for 3.0, BSD-style (modified zlib license) for 2.x
Website www.nunit.org

NUnit is an open-source unit testing framework for the .NET Framework and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.[ citation needed ]

Contents

Features

NUnit provides a console runner (nunit3-console.exe), which is used for batch execution of tests. The console runner works through the NUnit Test Engine, which provides it with the ability to load, explore and execute tests. When tests are to be run in a separate process, the engine makes use of the nunit-agent program to run them.[ citation needed ]

The NUnitLite runner may be used in situations where a simpler runner is more suitable. It allows developers to create self-executing tests.[ citation needed ]

Assertions

NUnit provides a rich set of assertions as static methods of the Assert class. If an assertion fails, the method call does not return and an error is reported. If a test contains multiple assertions, any that follow the one that failed will not be executed. For this reason, it's usually best to try for one assertion per test.[ citation needed ]

Nunit 3.x is supporting multiple assertions.

[Test]publicvoidComplexNumberTest(){ComplexNumberresult=SomeCalculation();Assert.Multiple(()=>{Assert.AreEqual(5.2,result.RealPart,"Real part");Assert.AreEqual(3.9,result.ImaginaryPart,"Imaginary part");});}

Classical

Before NUnit 2.4, a separate method of the Assert class was used for each different assertion. It continues to be supported in NUnit, since many people prefer it.[ citation needed ]

Each assert method may be called without a message, with a simple text message or with a message and arguments. In the last case the message is formatted using the provided text and arguments.[ citation needed ]

// Equality assertsAssert.AreEqual(objectexpected,objectactual);Assert.AreEqual(objectexpected,objectactual,stringmessage,paramsobject[]parms);Assert.AreNotEqual(objectexpected,objectactual);Assert.AreNotEqual(objectexpected,objectactual,stringmessage,paramsobject[]parms);// Identity assertsAssert.AreSame(objectexpected,objectactual);Assert.AreSame(objectexpected,objectactual,stringmessage,paramsobject[]parms);Assert.AreNotSame(objectexpected,objectactual);Assert.AreNotSame(objectexpected,objectactual,stringmessage,paramsobject[]parms);// Condition asserts// (For simplicity, methods with message signatures are omitted.)Assert.IsTrue(boolcondition);Assert.IsFalse(boolcondition);Assert.IsNull(objectanObject);Assert.IsNotNull(objectanObject);Assert.IsNaN(doubleaDouble);Assert.IsEmpty(stringaString);Assert.IsNotEmpty(stringaString);Assert.IsEmpty(ICollectioncollection);Assert.IsNotEmpty(ICollectioncollection);

Constraint based

Beginning with NUnit 2.4, a new Constraint-based model was introduced. This approach uses a single method of the Assert class for all assertions, passing a Constraint object that specifies the test to be performed. This constraint-based model is now used internally by NUnit for all assertions. The methods of the classic approach have been re-implemented on top of this new model.[ citation needed ]

Example

Example of an NUnit test fixture:[ citation needed ]

usingNUnit.Framework;[TestFixture]publicclassExampleTestOfNUnit{[Test]publicvoidTestMultiplication(){Assert.AreEqual(4,2*2,"Multiplication");// Equivalently, since version 2.4 NUnit offers a new and// more intuitive assertion syntax based on constraint objects// [http://www.nunit.org/index.php?p=constraintModel&r=2.4.7]:Assert.That(2*2,Is.EqualTo(4),"Multiplication constraint-based");}}// The following example shows different ways of writing the same exception test.[TestFixture]publicclassAssertThrowsTests{[Test]publicvoidTests(){// .NET 1.xAssert.Throws(typeof(ArgumentException),newTestDelegate(MethodThatThrows));// .NET 2.0Assert.Throws<ArgumentException>(MethodThatThrows);Assert.Throws<ArgumentException>(delegate{thrownewArgumentException();});// Using C# 3.0     Assert.Throws<ArgumentException>(()=>{thrownewArgumentException();});}voidMethodThatThrows(){thrownewArgumentException();}}// This example shows use of the return value to perform additional verification of the exception.[TestFixture]publicclassUsingReturnValue{[Test]publicvoidTestException(){MyExceptionex=Assert.Throws<MyException>(delegate{thrownewMyException("message",42);});Assert.That(ex.Message,Is.EqualTo("message"));Assert.That(ex.MyParam,Is.EqualTo(42));}}// This example does the same thing using the overload that includes a constraint.[TestFixture]publicclassUsingConstraint{[Test]publicvoidTestException(){Assert.Throws(Is.Typeof<MyException>().And.Message.EqualTo("message").And.Property("MyParam").EqualTo(42),delegate{thrownewMyException("message",42);});}}

The NUnit framework discovers the method ExampleTestOfNUnit.TestMultiplication() automatically by reflection.[ citation needed ]

Extensions

FireBenchmarks is an addin able to record execution time of unit tests and generate XML, CSV, XHTML performances reports with charts and history tracking. Its main purpose is to enable a developer or a team that work with an agile methodology to integrate performance metrics and analysis into the unit testing environment, to easily control and monitor the evolution of a software system in terms of algorithmic complexity and system resources load.[ citation needed ]

NUnit.Forms is an expansion to the core NUnit framework and is also open source. It specifically looks at expanding NUnit to be able to handle testing user interface elements in Windows Forms. As of January 2013, Nunit.Forms is in Alpha release, and no versions have been released since May 2006.[ citation needed ]

NUnit.ASP is a discontinued [9] expansion to the core NUnit framework and is also open source. It specifically looks at expanding NUnit to be able to handle testing user interface elements in ASP.Net.[ citation needed ]

See also

Related Research Articles

<span class="mw-page-title-main">Design by contract</span> Approach for designing software

Design by contract (DbC), also known as contract programming, programming by contract and design-by-contract programming, is an approach for designing software.

Common Intermediate Language (CIL), formerly called Microsoft Intermediate Language (MSIL) or Intermediate Language (IL), is the intermediate language binary instruction set defined within the Common Language Infrastructure (CLI) specification. CIL instructions are executed by a CLI-compatible runtime environment such as the Common Language Runtime. Languages which target the CLI compile to CIL. CIL is object-oriented, stack-based bytecode. Runtimes typically just-in-time compile CIL instructions into native code.

In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior.

Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of its arguments. This is a generalization of single-dispatch polymorphism where a function or method call is dynamically dispatched based on the derived type of the object on which the method has been called. Multiple dispatch routes the dynamic dispatch to the implementing function or method using the combined characteristics of one or more arguments.

In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures—are tested to determine whether they are fit for use. It is a standard step in development and implementation approaches such as Agile.

In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes the number, types, and order of the arguments required by a function. A type signature is typically used during overload resolution for choosing the correct definition of a function to be called among many overloaded forms.

In computer programming, specifically object-oriented programming, a class invariant is an invariant used for constraining objects of a class. Methods of the class should preserve the invariant. The class invariant constrains the state stored in the object.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In computing based on the Java Platform, JavaBeans is a technology developed by Sun Microsystems and released in 1996, as part of JDK 1.1.

<span class="mw-page-title-main">Dependency injection</span> Software programming technique

In software engineering, dependency injection is a programming technique in which an object or function receives other objects or functions that it requires, as opposed to creating them internally. Dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs. The pattern ensures that an object or function which wants to use a given service should not have to know how to construct those services. Instead, the receiving 'client' is provided with its dependencies by external code, which it is not aware of. Dependency injection makes implicit dependencies explicit and helps solve the following problems:

TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities.

NUnitAsp is a tool for automatically testing ASP.NET web pages. It's an extension to NUnit, a tool for test-driven development in .NET.

Generics are a facility of generic programming that were added to the Java programming language in 2004 within version J2SE 5.0. They were designed to extend Java's type system to allow "a type or method to operate on objects of various types while providing compile-time type safety". The aspect compile-time type safety was not fully achieved, since it was shown in 2016 that it is not guaranteed in all cases.

In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric Evans and Martin Fowler.

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (null) behavior. The null object design pattern, which describes the uses of such objects and their behavior, was first published as "Void Value" and later in the Pattern Languages of Program Design book series as "Null Object".

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers, allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock. Hamcrest has been included in JUnit 4 since 2012, but was omitted from JUnit 5 in 2017.

<span class="mw-page-title-main">Jasmine (software)</span> Open-source testing framework for JavaScript

Jasmine is an open-source testing framework for JavaScript. It aims to run on any JavaScript-enabled platform, to not intrude on the application nor the IDE, and to have easy-to-read syntax. It is heavily influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.

QUnit is a JavaScript unit testing framework. Originally developed for testing jQuery, jQuery UI and jQuery Mobile, it is a generic framework for testing any JavaScript code. It supports client-side environments in web browsers, and server-side.

EasyMock is an open-source testing framework for Java released under the Apache License. The framework allows the creation of test double objects for the purpose of Test-driven Development (TDD) or Behavior Driven Development (BDD).

References

  1. "NUnit 3 Test Adapter".
  2. "Parallelizable Attribute". GitHub .
  3. "TestCaseData". GitHub .
  4. Prouse, Rob (2015-11-04). "Testing .NET Core using NUnit 3".
  5. Prouse, Rob (2015-03-25). "NUnit 3.0 Test Runner for Android and iOS".
  6. "NUnit Version 3 for Compact Framework".
  7. "NUnit Version 3 for SilverLight 5.0".
  8. "CategoryAttribute". GitHub . Retrieved 2015-12-15.
  9. "NUnit.ASP website main page". SourceForge . Retrieved 2008-04-15.

Bibliography