Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Automated unit testing

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Automated unit testing people are more casual in using the term "unit test". This can lead to confusion, especially when people start to claim that their unit tests "took a long time to execute." Defining a common vocabulary for developer tests helps to effectively classify tests, creating an effective CI system that can perform rapid builds.

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

Listing 6-1 shows a unit test written using the TestNG framework. TestNG is based on annotation, so Javadoc-like annotations like @ testng.test appear in the startPatternTest method. Through Java1.4 's assert statement, this test case verifies that the RegexPackageFilter class correctly filters strings through a regular expression pattern.

Listing 6-1 isolating unit tests using TestNG

Public class RegexPackageFilterTestNG {

/ * *

* @ testng.test

, /

Public void starPatternTest () throws Exception {

Filter filter = new RegexPackageFilter ("java.lang.*")

Assert filter.applyFilter ("java.lang.String"):

"filter returned false"

Assert! filter.applyFilter ("org.junit.TestCase"):

"filter returned true for org.junit.TestCase"

}

}

Some unit tests require fewer external dependencies, which are usually other classes. These dependent classes themselves are relatively simple and do not have very complex inter-class relationships. Sometimes unit tests even use mock objects (mock), which are simple objects that replace real, complex objects. If the dependent object itself does depend on an external entity, such as a file system or database, and these external objects are not virtualized, the test becomes a component test (defined later).

Listing 6-2 shows an example of a unit test written in Ruby that validates the behavior of a filter. This test is still a unit test, although it uses two classes, RegexFilter and SimpleFilter, because it uses only one type to validate the behavior.

Listing 6-2 isolating unit tests using Ruby

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 love to Gooogle on the Internet")) } end end 单元测试的关键在于没有外部的依赖关系,如数据库。这些外部的依赖关系通常会使测试建立和执行的时间变长。单元测试可以在开发周期的早期创建并执行(例如第一天)。因为编码和看到单元测试结果之间的时间很短,所以单元测试是一种有效的除错方法。

Book details: http://bvbroadview.blog.51cto.com/addblog.php

This article is excerpted from the book "continuous Integration: ways to improve Software quality and reduce risk"

Paul M. Duvall (Paul M. Duval) Steve Matyas (Steve. Andrew Glover (Andrew. By Glover)

Translated by Wang Haipeng

Published by Electronic Industry Press

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report