前言:
单元测试是保证程序质量的基石,也是考验编程功底的第一道关卡!
编写和执行单元测试,是为了证明目标代码的行为和我们期望的一致,更是为了给上级模块提供良好稳定的执行环境;
1. 观察目标函数全部的逻辑分支,针对它们设计用例:
项目需求为“判断是否为超人”,下面我基于Python3,用判定覆盖法来设计参数对其进行单元测试:
# coding = utf-8
import pytest
"""第一步:根据传入的参数判断目标是否为超人(Superman)"""
def is_superman(skill, hand):
# 是否拥有飞行能力?
can_fly = skill
# 是否有10个手指?
have_ten_fingers = hand
if not can_fly:
return False
if not have_ten_fingers:
return False
# 返回 True,表示检查的目标是一个真正的"超人"
return True
2. 对is_superman
函数设计一个“逻辑分支”表,可以看到共有3步(有几列就有几步),所以我们至少需要设计3组用例:
**注意:下面表格中的 0 表示 False,1 表示 True**
分支 & 步 | 1 | 2 | 3 |
---|---|---|---|
if | 0 | 1 | - |
if | - | 0 | 1 |
return | - | - | 1 |
3. 遍历目标函数的全部路径的思路:
接下来对这个目标函数设计一个测试器,用3组条件覆盖测试
基于 Python3 的代码举例——测试函数
"""第二步:准备3组参数,逐个传入目标函数并验证其结果"""
def test_person():
"""由于在python里 0==False,1==True ,所以你可以这样设计3组简单的测试用例:"""
test_cases = [
[0, 0],
[1, 0],
[1, 1]
]
"""根据上面3组用例,设计你的预期结果"""
expected_results = [False, False, True]
"""for循环帮你覆盖全部用例"""
for i in range(len(test_cases)):
assert is_superman(test_cases[i][0], test_cases[i][1]) == expected_results[i]
if __name__ == '__main__':
pytest.main()
4. 运行脚本,查看测试结果:
============================= test session starts ==============================
platform darwin -- Python 3.6.8, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /Users/terry/PycharmProjects/SuperMancollected 1 item
test_func_unittest.py . [100%]
=========================== 1 passed in 0.01 seconds ===========================
Process finished with exit code 0
5. 尝试增加一条用例,然后运行脚本,查看新的测试结果:
def test_person():
test_cases = [
[0, 0],
[1, 0],
[1, 1],
[0, 1], # <--增加1条用例
]
expected_results = [False, False, True, True] # <--对应增加一个预期结果
============================= test session starts ==============================
platform darwin -- Python 3.6.8, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /Users/terry/PycharmProjects/SuperMancollected 1 item
test_func_unittest.py F
test_func_unittest.py:26 (test_person)
False != True
Expected :True
Actual :False
<Click to see difference>
def test_person():
"""由于在python里 0==False,1==True ,所以你可以这样设计3组简单的测试用例:"""
test_cases = [
[0, 0],
[1, 0],
[1, 1],
[0, 1],
]
"""根据上面3组用例,设计你的预期结果"""
expected_results = [False, False, True, True]
"""for循环帮你覆盖全部用例"""
for i in range(len(test_cases)):
> assert is_superman(test_cases[i][0], test_cases[i][1]) == expected_results[i]
E assert False == True
E + where False = is_superman(0, 1)
test_func_unittest.py:41: AssertionError
[100%]
=================================== FAILURES ===================================
_________________________________ test_person __________________________________
def test_person():
"""由于在python里 0==False,1==True ,所以你可以这样设计3组简单的测试用例:"""
test_cases = [
[0, 0],
[1, 0],
[1, 1],
[0, 1],
]
"""根据上面3组用例,设计你的预期结果"""
expected_results = [False, False, True, True]
"""for循环帮你覆盖全部用例"""
for i in range(len(test_cases)):
> assert is_superman(test_cases[i][0], test_cases[i][1]) == expected_results[i]
E assert False == True
E + where False = is_superman(0, 1)
test_func_unittest.py:41: AssertionError
=========================== 1 failed in 0.04 seconds ===========================
Process finished with exit code 0