Automated Unit Testing

Source: Internet
Author: User
Tags testng
Automated Unit Testing

People are casual when using the term unit test. This may lead to confusion, especially when people start to claim that their unit test "took a long time to execute. Defining a common vocabulary for developer testing helps to classify tests effectively, so as to create an effective CI system and perform quick building.

Unit testing verifies the behavior of all small elements in the software system, which are usually a class. However, sometimes this one-to-one relationship between the unit test and the tested class is amplified because some tested classes are highly coupled.

Code List 6-1 shows the unit tests written using the testng framework. Testng is based on Annotation. Therefore, javadoc-like annotations such as @ testng. Test appear in the startpatterntest method. Through the assert Statement of java1.4, this test case demonstrates that the regexpackagefilter class correctly filters strings using a regular expression pattern.

Code List 6-1 use testng's isolation unit test

Public class regexpackagefiltertestng {

/**

*@Testng. Test

*/

Public void starpatterntest () throws exception {

 

Filter filter = newregexpackagefilter ("Java. Lang .*");

 

Assertfilter. applyfilter ("Java. Lang. String "):

"Filter returned false ";

 

Assert! Filter. applyfilter ("org. JUnit. testcase "):

"Filter returnedtrue for org. JUnit. testcase ";

}

}

Some unit tests require less external dependencies, which are usually other classes. These dependent classes are relatively simple and do not have complex inter-class relationships. Sometimes, unit tests even use simulated objects (Mock), which are simple objects used to replace real and complex objects. If the dependent objects are indeed dependent on external entities, such as a file system or database, and these external objects are not virtualized, the test becomes a component test (defined later ).

Code List 6-2 shows an example of a unit test written in ruby, which verifies the behavior of a filter. This test is still a unit test, although it uses two classes, regexfilter and simplefilter, because it only uses one type to verify behavior.

Code List 6-2 use Ruby's isolation unit test

Require "test/unit"

Require "filters"

 

Class filterstest <test: Unit: testcase

 

Def test_regex

Fltr = regexfilter. New (/Google | Amazon /)

Assert (fltr. apply_filter ("google "))

End

 

Def test_simple

Fltr = simplefilter. New ("oo ")

Assert (fltr. apply_filter ("google "))

End

 

Def test_filters

Fltrs = [simplefilter. New ("oo"), regexfilter. New (/go + GLE/)]

Fltrs. Each {| fltr |

Assert (fltr. apply_filter ("I loveto gooogle on the Internet "))

}

End

End

The key to unit testing is that there is no external dependency, such as a database. These external dependencies usually make the test establishment and execution longer. Unit tests can be created and executed early in the development cycle (for example, the First Day ). Unit test is an effective debugging method because the time between encoding and unit test results is short.

 

 

This article is excerpted from "continuous integration: the path to software quality improvement and risk reduction ".

[Us] Paul M. Duvall
(Paul. M. Duval) Steve Matyas
(Steve mayyes) by Andrew GLOVER

Translated by Wang Haipeng

Published by Electronic Industry Publishing House

Book details: http://blog.csdn.net/broadview2006/article/details/7660148

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.