Unittest Framework 简明教程

UnitTest Framework - Overview

单元测试是一种软件测试方法,通过该方法可以测试单个单元的源代码,例如函数、方法和类,以确定它们是否适合使用。直观上,可以将单元视为应用程序中最小的可测试部分。单元测试是程序员在开发过程中创建的短代码片段。它构成了组件测试的基础。

Unit testing is a software testing method by which individual units of source code, such as functions, methods, and class are tested to determine whether they are fit for use. Intuitively, one can view a unit as the smallest testable part of an application. Unit tests are short code fragments created by programmers during the development process. It forms the basis for component testing.

单元测试可以通过以下两种方式完成:

Unit testing can be done in the following two ways −

Manual Testing

Automated Testing

Executing the test cases manually without any tool support is known as manual testing. Since test cases are executed by human resources so it is very time consuming and tedious. As test cases need to be executed manually so more testers are required in manual testing. It is less reliable as tests may not be performed with precision each time because of human errors. No programming can be done to write sophisticated tests which fetch hidden information.

Taking tool support and executing the test cases by using automation tool is known as automation testing. Fast Automation runs test cases significantly faster than human resources. The investment over human resources is less as test cases are executed by using automation tool. Automation tests perform precisely same operation each time they are run and are more reliable. Testers can program sophisticated tests to bring out hidden information.

JUnit 是 Java 编程语言的单元测试框架。JUnit 在测试驱动开发的发展中非常重要,并且是单元测试框架系列中的一个,统称为 xUnit,它起源于 JUnit。您可以 JUnit Tutorial 这里。

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 JUnit. You can find out JUnit Tutorial here.

Python 单元测试框架有时称为“PyUnit”,是由 Kent Beck 和 Erich Gamma 开发的 JUnit 的 Python 语言版本。从 Python 2.1 版开始,PyUnit 构成了 Python 标准库的一部分。

The Python unit testing framework, sometimes referred to as “PyUnit,” is a Python language version of JUnit developed by Kent Beck and Erich Gamma. PyUnit forms part of the Python Standard Library as of Python version 2.1.

Python 单元测试框架支持测试自动化、测试的设置和关闭代码共享、测试聚合到集合中以及测试与报告框架的独立性。unittest 模块提供了使为一组测试轻松支持这些特性的类。

Python unit testing framework supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework. The unittest module provides classes that make it easy to support these qualities for a set of tests.

本教程为初学者准备,旨在帮助他们了解 Python 测试框架的基本功能。完成本教程后,您将发现自己在使用 Python 测试框架方面的专业知识处于中等水平,您可以从该水平提升到更高水平。

This tutorial has been prepared for the beginners to help them understand the basic functionality of Python testing framework. After completing this tutorial you will find yourself at a moderate level of expertise in using Python testing framework from where you can take yourself to the next levels.

您应该具备使用 Python 语言进行软件开发的合理专业知识。我们的 Python tutorial 是开始学习 Python 的好地方。也希望具备软件测试基础知识。

You should have reasonable expertise in software development using Python language. Our Python tutorial is a good place to start learning Python. Knowledge of basics of software testing is also desirable.

Environment Setup

用于编写测试的类位于“unittest”模块中。如果您使用的是旧版的 Python(早于 Python 2.1),则可以 http://pyunit.sourceforge.net/ 下载该模块。但是,unittest 模块现在是标准 Python 发行版的一部分;因此它不需要单独安装。

The classes needed to write tests are to be found in the 'unittest' module. If you are using older versions of Python (prior to Python 2.1), the module can be downloaded from http://pyunit.sourceforge.net/. However, unittest module is now a part of the standard Python distribution; hence it requires no separate installation.