XUnit
Encyclopedia
Various code-driven
testing
frameworks have come to be known collectively as xUnit. These frameworks allow testing of different elements (units) of software, such as functions and classes. The main advantage of xUnit frameworks is that they provide an automated solution with no need to write the same tests many times, and no need to remember what should be the result of each test.
Such frameworks are based on a design by Kent Beck
, originally implemented for Smalltalk
as SUnit
. Erich Gamma
and Kent Beck
ported SUnit to Java, creating JUnit
. From there, the framework was also ported to other languages, e.g., CppUnit
(for C++), NUnit
(for .NET
). They are all referred to as xUnit and are usually free, open source software. They are now available for many programming languages and development platforms.
(also known as a test context) is the set of precondition
s or state needed to run a test. The developer should set up a known good state before the tests, and after the tests return to the original state.
The setup and teardown methods serve to initialize and clean up test fixtures.
is a function or macro that verifies the behavior (or the state) of the unit under test.
Failure of an assertion typically throws an exception, aborting the execution of the current test.
and HttpUnit
.
Programming approach to unit testing:
Test automation
Test automation is the use of software to control the execution of tests, the comparison of actual outcomes to predicted outcomes, the setting up of test preconditions, and other test control and test reporting functions...
testing
Software testing
Software testing is an investigation conducted to provide stakeholders with information about the quality of the product or service under test. Software testing can also provide an objective, independent view of the software to allow the business to appreciate and understand the risks of software...
frameworks have come to be known collectively as xUnit. These frameworks allow testing of different elements (units) of software, such as functions and classes. The main advantage of xUnit frameworks is that they provide an automated solution with no need to write the same tests many times, and no need to remember what should be the result of each test.
Such frameworks are based on a design by Kent Beck
Kent Beck
Kent Beck is an American software engineer and the creator of the Extreme Programming and Test Driven Development software development methodologies. Beck was one of the 17 original signatories of the Agile Manifesto in 2001....
, originally implemented for Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...
as SUnit
SUnit
SUnit is a unit testing framework for the programming language Smalltalk. It is the original source of the xUnit design, originally written by the creator of Extreme Programming, Kent Beck. SUnit allows writing tests and checking results in Smalltalk...
. Erich Gamma
Erich Gamma
Erich Gamma is Swiss computer scientist and co-author of the influential Software engineering textbook, Design Patterns: Elements of Reusable Object-Oriented Software. He co-wrote the JUnit software testing framework with Kent Beck and led the design of the Eclipse platform's Java Development Tools...
and Kent Beck
Kent Beck
Kent Beck is an American software engineer and the creator of the Extreme Programming and Test Driven Development software development methodologies. Beck was one of the 17 original signatories of the Agile Manifesto in 2001....
ported SUnit to Java, creating JUnit
JUnit
JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks collectively known as xUnit that originated with SUnit....
. From there, the framework was also ported to other languages, e.g., CppUnit
CPPUnit
CppUnit is a unit testing framework module for the C++ programming language, described as a C++ port of JUnit. The library is released under the GNU Lesser General Public License. The library can be compiled for a variety of POSIX platforms, allowing unit-testing of 'C' sources as well as C++ with...
(for C++), NUnit
NUnit
NUnit is an open source unit testing framework for Microsoft .NET. It serves the same purpose as JUnit does in the Java world, and is one of many in the xUnit family.- Example :Example of an NUnit test fixture:using NUnit.Framework; [TestFixture]...
(for .NET
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
). They are all referred to as xUnit and are usually free, open source software. They are now available for many programming languages and development platforms.
xUnit architecture
All xUnit frameworks share the following basic component architecture, with some varied implementation details.Test fixtures
A test fixtureTest fixture
A test fixture is something used to consistently test some item, device, or piece of software.-Electronics:Circuit boards, electronic components, and chips are held in place and subjected to controlled electronic test signals. One example is a bed of nails tester.-Software:Test fixture refers to...
(also known as a test context) is the set of precondition
Precondition
In computer programming, a precondition is a condition or predicate that must always be true just prior to the execution of some section of code or before an operation in a formal specification....
s or state needed to run a test. The developer should set up a known good state before the tests, and after the tests return to the original state.
Test suites
A test suite is a set of tests that all share the same fixture. The order of the tests shouldn't matter.Test execution
The execution of an individual unit test proceeds as follows:The setup and teardown methods serve to initialize and clean up test fixtures.
Assertions
An assertionAssertion (computing)
In computer programming, an assertion is a predicate placed in a program to indicate that the developer thinks that the predicate is always true at that place.For example, the following code contains two assertions:...
is a function or macro that verifies the behavior (or the state) of the unit under test.
Failure of an assertion typically throws an exception, aborting the execution of the current test.
xUnit frameworks
Many xUnit frameworks exist for various programming languages and development platforms.xUnit extensions
Extensions are available to extend xUnit frameworks with additional specialized functionality. Examples of such extensions include XMLUnit, XmlUnit.Xunit, DbUnit, HtmlUnitHtmlUnit
HtmlUnit is a headless web browser written in Java. It allows high-level manipulation of websites from other Java code, including filling and submitting forms and clicking hyperlinks. It also provides access to the structure and the details within received web pages. HtmlUnit emulates parts of...
and HttpUnit
HttpUnit
HttpUnit is an open source software testing framework used to perform testing of web sites without the need for a web browser. HttpUnit supports HTML form submission, JavaScript, HTTP basic access authentication, automatic page redirection, and cookies. Written in Java, HttpUnit allows Java test...
.
See also
Unit testing in general:- Unit testing
- Software testingSoftware testingSoftware testing is an investigation conducted to provide stakeholders with information about the quality of the product or service under test. Software testing can also provide an objective, independent view of the software to allow the business to appreciate and understand the risks of software...
Programming approach to unit testing:
- Test-driven developmentTest-driven developmentTest-driven development is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new...
- Extreme programmingExtreme ProgrammingExtreme programming is a software development methodology which is intended to improve software quality and responsiveness to changing customer requirements...
External links
- Kent Beck's original testing framework paper
- Other list of various unit testing frameworks
- OpenSourceTesting.org lists many unit testing frameworks, performance testing tools and other tools programmers/developers may find useful
- Test automation patterns for writing tests/specs in xUnit.
- Martin Fowler on the background of xUnit.