加法测试示例¶
xlsx加法测试是指:在xlsx中编写进行相加的两个数以及预期结果,并由pytest加载、执行、判断。
其步骤类型单一,且不需要任何第三方组件,适合新手练习。
设计用例¶
假设在进行加法测试时,需要3个值:
num1: 第一个数字
num2 : 第二个数字
expected_results:预期的计算结果
则在xlsx中可以这样表示
num1 |
num2 |
expected_results |
|---|---|---|
111 |
222 |
333 |
为符合插件要求,需要加上必填字段,即为:
关键字 |
|||
|---|---|---|---|
name |
加法运算 |
||
add |
111 |
222 |
333 |
如果在测试用例需要包含更多步骤,name 标记下中继续增加内容:
关键字 |
|||
|---|---|---|---|
name |
加法运算 |
||
add |
111 |
222 |
333 |
add |
111 |
333 |
555 |
add |
111 |
333 |
999 |
自此xlsx用例已设计完毕,pytest执行结果如下:
(.venv) C:\demo\pytest-xlsx-demo>pytest
=========================== test session starts ============================
platform win32 -- Python 3.12.0, pytest-8.2.2, pluggy-1.5.0
rootdir: C:\demo\pytest-xlsx-demo
configfile: pytest.ini
plugins: xlsx-2.0.0
collected 1 item
tests\example\test_add.xlsx . [100%]
============================ 1 passed in 0.02s =============================
实现钩子¶
此时pytest虽然能够识别、加载用例内容,却没有根据用例步骤进行具体动作的执行。
既没有进行加法运算,也没有对结果进行断言。
接下来通过实现钩子将xlsx用例步骤变为具体的用例动作
# conftest.py
from pytest_xlsx import XlsxItem
def add(num1,num2,expected_results):
c = num1 + num2
assert c == expected_results
def pytest_xlsx_run_step(item: XlsxItem):
step = item.current_step
action = step.get("关键字")
# `_BlankField` 存储所有空白字段数据
num1, num2, expected_results = step.get("_BlankField")
if action == 'add': # 判断xlsx中的关键字
assert == num1 + num2
return True
实现钩子后重新启动pytest
(.venv) C:\demo\pytest-xlsx-demo>pytest
=========================== test session starts ============================
platform win32 -- Python 3.12.0, pytest-8.2.2, pluggy-1.5.0
rootdir: C:\demo\pytest-xlsx-demo
configfile: pytest.ini
plugins: xlsx-2.0.0
collected 1 item
tests\example\test_add.xlsx F [100%]
================================= FAILURES =================================
_______________________________ Sheet1.加法运算 ________________________________
xlsx content:
| | 关键字 | _BlankField | | |
|-----|----------|---------------|-----|-----|
| | add | 111 | 222 | 333 |
| >>> | add | 111 | 333 | 555 |
| | add | 111 | 333 | 999 |
C:\demo\pytest-xlsx-demo\conftest.py:24: AssertionError
assert 444 == 555
========================= short test summary info ==========================
FAILED tests/example/test_add.xlsx::Sheet1::加法运算
============================ 1 failed in 0.03s =============================
首先看到的是,这个用例 执行失败了
再细看失败原因,正是由于第二个测试步骤 断言失败了
_______________________________ Sheet1.加法运算 ________________________________
xlsx content:
| | 关键字 | _BlankField | | |
|-----|----------|---------------|-----|-----|
| | add | 111 | 222 | 333 |
| >>> | add | 111 | 333 | 555 |
| | add | 111 | 333 | 999 |
C:\demo\pytest-xlsx-demo\conftest.py:24: AssertionError
assert 444 == 555
备注
注意:如果xlsx用例中步骤非常多,则不会全部显示,只显示出错行附近的内存
和普通的pytest测试用例一样的是, 当某个测试步骤执行失败,那么后续的都不再继续执行 。 所以我们只能看到第二个步骤失败,看不到第三个步骤失败(因为它没有执行)。
创建多个用例¶
如果你有多个测试步骤,力求每一个步骤都进行能够执行,可以考虑把步骤分散的不同的用例中中。
按照一般的测试原则,用例之间相互隔离。 所以就算某个用例失败了,其他用例也会继续执行。
这里有一个小技巧:
我们不需要真的创建三个xlsx文件,
只需要在同一个xlsx在文件通过 name
进行分隔,即可创建三个用例
关键字 |
|||
|---|---|---|---|
name |
加法运算A |
||
add |
111 |
222 |
333 |
name |
加法运算B |
||
add |
111 |
333 |
555 |
name |
加法运算C |
||
add |
111 |
333 |
999 |
执行结果如下:
(.venv) C:\demo\pytest-xlsx-demo>pytest -vs
=========================== test session starts ============================
platform win32 -- Python 3.12.0, pytest-8.2.2, pluggy-1.5.0
cachedir: .pytest_cache
rootdir: C:\demo\pytest-xlsx-demo
configfile: pytest.ini
plugins: xlsx-2.0.0
collected 3 items
tests/example/test_add_multiple_cases.xlsx::Sheet1::加法运算A PASSED
tests/example/test_add_multiple_cases.xlsx::Sheet1::加法运算B FAILED
tests/example/test_add_multiple_cases.xlsx::Sheet1::加法运算C FAILED
================================= FAILURES =================================
_______________________________ Sheet1.加法运算B _______________________________
xlsx content:
| | 关键字 | _BlankField | | |
|-----|----------|---------------|-----|-----|
| >>> | add | 111 | 333 | 555 |
C:\demo\pytest-xlsx-demo\conftest.py:24: AssertionError
assert 444 == 555
_______________________________ Sheet1.加法运算C _______________________________
xlsx content:
| | 关键字 | _BlankField | | |
|-----|----------|---------------|-----|-----|
| >>> | add | 111 | 333 | 999 |
C:\demo\pytest-xlsx-demo\conftest.py:24: AssertionError
assert 444 == 999
========================= short test summary info ==========================
FAILED tests/example/test_add_multiple_cases.xlsx::Sheet1::加法运算B
FAILED tests/example/test_add_multiple_cases.xlsx::Sheet1::加法运算C
======================= 2 failed, 1 passed in 0.04s ========================
从执行结果可以看到,1个成功2个失败
失败的原因都是是因为 断言失败(AssertionError)
小节¶
至此,便实现了单一步骤类型的xlsx加法测试,
你可以在测试用例中补充更多的步骤,或者创建更多的xlsx文件里补充测试用例。
也可以修改用例的格式或步骤执行方式,实现其他类型的测试。