这里没有软件测试的泛泛理论,只有博主的最佳实践。 博主的研究方向为静态分析和性能测试,致力于各种测试工具的引入、评估和开发。 本博的测试文章均为作者原创,转载请务必注明出处。

Unit Testing

上一篇 / 下一篇  2008-05-04 10:25:50 / 个人分类:C++test

本文的英文部分源于C++test 用户手册的相关部分,中文部分是huior添加的注释。

单元测试的对象

"Unit testing" refers to testing software code at the simplest functional point, which is typically a single class or a function.

谁来做单元测试?

Unit testing is typically performed by developers inside a development cycle, rather
than in the QA phase.

单元测试的目的

Using unit testing, you can ensure that the application building blocks are solid
before they are integrated, thus improving the quality of the entire application. When you test early, it is typically less difficult and time-consuming to identify and fix defects at this point.

单元测试的内容

Manual unit testing typically involves writing test harnesses by hand, specifying input data, and supplying stubs for missing functions. C++test automates these tasks to make the unit testing process more efficient and consistent.

Generally, unit testing can include:

  • Exception testing (also known as white-box testing, stress testing, construction testing, or reliability testing), which is used to confirm that code is structurally sound and can handle the full range of feasible inputs and their combinations without throwing an unexpected exception.

      要做到这一点,还真是不容易,尤其是handle the full range of feasible inputs and their combinations,这样岂不是“穷尽测试”了? 

  • Functional testing, which is used to verify that software building blocks conform to their specification and that all of the intended functionality is included and working correctly. Creating functional tests at the unit level involves human input to specify particular input and state conditions, and expected output. Functional testing can be implemented as white box testing
    (testing with knowledge of internals and implementation of a unit under test), or black box testing, which is solely based on external behavīor of a unit under test.

      单元级别的功能测试具体实施,既可以用白盒的方法,也可以用黑盒的方法,这一点和和传统的功能测试定义有点出入。

  • Regression testing, which is used to verify that existing code behavīor does not change as the code base evolves. This is typically done by developing a set of tests, verifying their correctness, and running them after code changes to catch deviations in code behavīor. Regression testing may rely on both exception testing and functional testing.

      单元的回归测试也很重要,不过执行起来难度更大。很多单位连单元测试实施都觉得困难,更别说回归了。

C++test针对单元测试的解决方案

C++test can perform all of described types of unit testing; the level and scope of testing performed can be customized to reflect your requirements and test preferences.


Exception testing is performed when you run the test cases that C++test generates automatically. This type of testing exposes unexpected exceptions and checks that the class is structurally sound. The success of reliability testing hinges upon thorough coverage of the code, so you might want to extend the automatically-generated test cases as needed to increase code coverage. C++test measures test
coverage to help you assess coverage and determine where additional tests are necessary.

衡量测试是否完备的重要指标是coverage。


Functional testing is performed when you extend the automatically-generated test cases to verify whether the class's public interface operates as described in the specification.
Regression testing involves regularly testing the evolving code base by running all available test cases and checking if the expected outcomes have changed. C++test reports error messages when test case outcomes from the current test run do not match the expected test case outcomes.


Test cases can be implemented and saved in C or C++ source code. You can extend and modify generated test cases within the IDE’s text editor. These test cases use a format similar to the popular CppUnit format. C++test’s tests offer more extensive capabilities than CppUnit, including the ability to test C code and provide programmatic access to private and protected data and member functions
within the test framework. Existing CppUnit test cases can be imported into C++test and used in concert with automatically-generated test cases.

单元测试测试用例的表现形式就是C或C++源代码。既然是源代码,实施起来就会非常灵活,你可以自己添加新的用例扩展或者修改已有的用例,当然要求测试者有相当的编程经验才能完成这样的工作


TAG:

 

评分:0

我来说两句

Open Toolbar