使用 Python 中的 unittest 进行单元测试
在本教程中,我们将学习如何使用内置模块 **unittest** 进行 **单元测试**。测试在软件开发中起着重要作用。您将在进入生产环境之前了解问题。
我们将学习使用名为 **unittest** 的内置模块在 Python 中进行测试的基础知识。让我们开始本教程。
什么是单元测试?
如果您以登录系统为例。登录表单中的每个字段都是一个单元/组件。测试这些单元/组件的功能称为 **单元测试**。
示例
让我们看看 unittest 框架的基本结构。
# importing unittest module import unittest # unittest will test all the methods whose name starts with 'test' class SampleTest(unittest.TestCase): # return True or False def test(self): self.assertTrue(True) # running the test unittest.main()
输出
如果您运行上述程序,您将得到以下结果。
---------------------------------------------------------------------- Ran 1 test in 0.001s OK
2. 测试字符串方法
现在,我们将使用示例测试用例测试不同的字符串方法。请记住,方法名称必须以 **test** 开头。
让我们简要介绍一下我们将编写的每个方法。
test_string_equality
此方法使用 **unittest.TestCase** 的 **assertEqual** 方法测试两个字符串是否相等。
test_string_case
此方法使用 **unittest.TestCase** 的 **assertEqual** 方法测试两个字符串的大小写是否相等。
test_is_string_upper
此方法使用 **unittest.TestCase** 的 **assertTrue** 和 **assertFalse** 方法测试字符串是否为大写。
示例
# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):
# string equal
def test_string_equality(self):
# if both arguments are equal then it's succes
self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
# comparing the two strings
def test_string_case(self):
# if both arguments are equal then it's succes
self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
# checking whether a string is upper or not
def test_is_string_upper(self):
# used to check whether the statement is True or False
# the result of expression inside the **assertTrue** must be True to pass the test case
# the result of expression inside the **assertFalse** must be False to pass the test case
self.assertTrue('TUTORIALSPOINT'.isupper())
self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main()输出
如果您运行上述代码,如果所有测试用例都通过,则会得到以下结果。
... ---------------------------------------------------------------------- Ran 3 tests in 0.001s OK
示例
运行以下程序以查看失败的测试用例输出。
# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):
# string equal
def test_string_equality(self):
# if both arguments are equal then it's succes
self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
# comparing the two strings
def test_string_case(self):
# if both arguments are equal then it's succes
self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
# checking whether a string is upper or not
def test_is_string_upper(self):
# used to check whether the statement is True or False
# the result of expression inside the **assertTrue** must be True to pass the test case
# the result of expression inside the **assertFalse** must be False to pass the test case
self.assertTrue('TUTORIALSPOINt'.isupper())
self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main()输出
如果您运行上述程序,则会得到以下结果。
======================================================================
FAIL: test_is_string_upper (__main__.TestingStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "p:/Python Work/Stopwatch/practice.py", line 21, in test_is_string_upper
self.assertTrue('TUTORIALSPOINt'.isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.016s
FAILED (failures=1)即使只有一个测试用例失败,我们也会收到失败消息。
结论
如果您在本教程中有任何疑问,请在评论部分提出。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP