Unit testing: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Subpagination Bot
m (Add {{subpages}} and remove any categories (details))
imported>Tom Morris
(added section on frameworks, removed stuff for the subpages)
Line 17: Line 17:
Once a test (or suite of tests) has been written, the programmer then writes just enough programming code to make the tests pass. At this point, he stops because there is nothing left to do. Once all tests pass, there is no "debug-test" cycle.  
Once a test (or suite of tests) has been written, the programmer then writes just enough programming code to make the tests pass. At this point, he stops because there is nothing left to do. Once all tests pass, there is no "debug-test" cycle.  


Advocates such as Kent Beck and Martin Fowler consider this a great advantage (see [[Refactoring]]).
Advocates such as [[Kent Beck]] and [[Martin Fowler]] consider this a great advantage (see [[Refactoring]]).


==Testing and refactoring==
==Testing and refactoring==
Line 27: Line 27:
Usually refactoring is accomplished in a series of small steps as the program evolves towards the new design.
Usually refactoring is accomplished in a series of small steps as the program evolves towards the new design.


==See also==
==Unit testing frameworks==
*[[Software bug]]s
As unit testing has become popular, a variety of unit testing frameworks have been created for those following [[test driven development]]. Most of these follow from the design of [[JUnit]], the unit testing framework for the [[Java programming language]]. Other languages have similar frameworks - [[C Sharp|C#]] has [[NUnit]], [[Python programming language|Python]] has the in-built 'unittest' module, [[Ruby programming language|Ruby]] has 'test/unit' and so on.


==Bibliography==
In addition, some have suggested that [[behavior driven development]] may be a better practice to follow than [[test driven development]], and in order to facilitate this, people have worked on creating frameworks that structure tests by behavior: [[Ruby programming language|Ruby's]] [[RSpec]] was the first available, and others have followed for other languages - including [[JBehave]] for [[Java programming language|Java]], [[NSpec]] for [[C Sharp|C#]]/[[.NET Framework]].
 
{{cite book
  | last = Fowler
  | first = Martin
  | authorlink = Martin Fowler
  | coauthors = John Brant, William Opdyke, Don Roberts
  | title = Refactoring: Improving the Design of Existing Code
  | publisher = Addison Wesley
  | month = July
  | year =  1999
  | series = Object Technology
  | isbn = 0201485672
}}
 
{{cite book
  | last = Beck
  | first = Kent
  | authorlink = Kent Beck
  | coauthors = Cynthia Andres
  | title = Extreme Programming Explained: Embrace Change
  | edition = 2nd edition
  | publisher = Addison-Wesley Professional
  | month = November
  | year = 2004
  | series = The XP Series
  | isbn = 0321278658
}}

Revision as of 11:23, 28 June 2008

This article is developing and not approved.
Main Article
Discussion
Related Articles  [?]
Bibliography  [?]
External Links  [?]
Citable Version  [?]
 
This editable Main Article is under development and subject to a disclaimer.

Unit testing of computer software ensures that a component of a computer program works as intended. For example, a square root function should return a number which, when multiplied by itself, is sufficiently close to the original argument of the function.

The two most common ways of testing a component are:

  1. Write it first, then check for bugs
  2. Automated unit testing

Code and fix

The first sort of unit testing, derisively called "code and fix" by prominent author Steve McConnell, has several problems. It takes a lot of time and often does not find all errors. The cost of fixing software bugs rises exponentially during the software development cycle. If the problem is found in the design phase, it's relatively easy to fix. If it's found while coding is going on, but the software has not shipped (i.e., been published), it's harder to fix but still feasible. If the software ships with a defective routine, then at best a new release will have to be created and distributed. At worst, loss of equipment and human life can occur (see Ariadne software bug).

Automated unit testing

The second sort of unit testing seems paradoxical. The programmer writes a suite of automated tests, i.e., a program which verifies the program. The simplest sort of test exercises a routine by feeding it inputs and comparing outputs to expected values. For example, a square root routine should return a result of 5.000000 for an argument of 25.

Some advocates of test-first development even recommend writing a test that won't compile, if there is no existing code in place to test. In that case, the first task is to write stub routines so that the test compiles (but fails).

Once a test (or suite of tests) has been written, the programmer then writes just enough programming code to make the tests pass. At this point, he stops because there is nothing left to do. Once all tests pass, there is no "debug-test" cycle.

Advocates such as Kent Beck and Martin Fowler consider this a great advantage (see Refactoring).

Testing and refactoring

In test-first development, the credo is "Don't write code until you have a failing test" and "Do the simplest thing that could possibly work."

With a suite of automated unit tests in place, refactoring is transformed from a risky venture into a valuable and risk-free process. The structure of the computer program can be changed radically, with no chance of introducing software defects, because after each change the programmers run the automated tests again. As long as all the tests pass, there is no problem.

Usually refactoring is accomplished in a series of small steps as the program evolves towards the new design.

Unit testing frameworks

As unit testing has become popular, a variety of unit testing frameworks have been created for those following test driven development. Most of these follow from the design of JUnit, the unit testing framework for the Java programming language. Other languages have similar frameworks - C# has NUnit, Python has the in-built 'unittest' module, Ruby has 'test/unit' and so on.

In addition, some have suggested that behavior driven development may be a better practice to follow than test driven development, and in order to facilitate this, people have worked on creating frameworks that structure tests by behavior: Ruby's RSpec was the first available, and others have followed for other languages - including JBehave for Java, NSpec for C#/.NET Framework.