【源码解读】
@_with_exception(Skipped)
def skip(msg: str = "", *, allow_module_level: bool = False) -> "NoReturn":
"""Skip an executing test with the given message.
This function should be called only during testing (setup, call or teardown) or
during collection by using the ``allow_module_level`` flag. This function can
be called in doctests as well.
:param bool allow_module_level:
Allows this function to be called at module level, skipping the rest
of the module. Defaults to False.
.. note::
It is better to use the :ref:`pytest.mark.skipif ref` marker when
possible to declare a test to be skipped under certain conditions
like mismatching platforms or dependencies.
Similarly, use the ``# doctest: +SKIP`` directive (see `doctest.SKIP
<https://docs.python.org/3/library/doctest.html#doctest.SKIP>`_)
to skip a doctest statically.
"""
__tracebackhide__ = True
raise Skipped(msg=msg, allow_module_level=allow_module_level)
【翻译】
“”“
跳过带有给定消息的正在执行的测试。
只有在测试(设置、调用或拆卸)或
在收集期间使用“allow\u module\u level”标志。此函数可以
也可以叫医生来。
:param bool allow\模块\级别:
允许在模块级调用此函数,跳过其余函数
模块的。默认为False。
注:
最好使用:ref:pytest.mark.skipif文件引用
marker when
在某些条件下可以声明要跳过的测试
比如不匹配的平台或依赖关系。
“”"
注意看:这里建议最好使用(pytest.mark.skipif),所以后续我们的使用就基于skipif,而skip不多做介绍
直接看test_case_13、14,
13中的用例,可输入reason、condition,reason默认None,条件默认真。
14中的条件例如:1<3,若为真,则跳过该类里面的所有用例,若为假,则继续执行。
# coding=utf-8
import pytest
@pytest.fixture()
def test_case_3():
print('---3号用例完成---')
@pytest.fixture()
def test_case_4():
print('---4号用例完成---')
@pytest.fixture()
def test_case_5():
print('---5号用例完成---')
@pytest.fixture()
def test_case_6():
print('---6号用例完成---')
@pytest.fixture()
def test_case_7():
print('---7号用例完成---')
@pytest.fixture()
def test_case_8():
print('---8号用例完成---')
# (1)这里按照【从下到上的顺序】,执行优先级是3、4、5
@pytest.mark.usefixtures('test_case_5')
@pytest.mark.usefixtures('test_case_4')
@pytest.mark.usefixtures('test_case_3')
class Testlogin001:
# 被pytest.fixture()装饰的函数,函数名可以作为变量传递给测试用例,最终在执行测试用例之前执行这个装饰过的函数
def test_case_1(self, test_case_8):
print('---1号用例完成---')
# (2)这里按照调用了前面的函数test_case_6,局部的调用,执行优先级是最高的。
@pytest.mark.usefixtures('test_case_7')
@pytest.mark.usefixtures('test_case_6')
def test_case_2(self):
print('---2号用例完成---')
# 单参数单值
@pytest.mark.parametrize('arg', [1])
def test_case_9(self, arg):
print("传入的值为:{}".format(arg))
assert arg == 1
# 单参数多值
@pytest.mark.parametrize('arg',['abc',1,{'a':1,'b':3},(4,5)])
def test_case_10(self, arg):
print(f"传入的值为:{arg}")
# 多参数多值
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("5-2", 3), ("5*2", 10)])
def test_case_11(self, test_input, expected):
print(f"原值:{test_input} 期望值{expected}")
assert eval(test_input) == expected
@pytest.mark.xfail(condition=1<3, reason='该功能尚未完善,还在调测中')
def test_case_12(self):
print('---12号用例完成---')
@pytest.mark.skipif(reason='test_case_13用例还在调测中')
def test_case_13(self):
print('---13号用例完成---')
@pytest.mark.skipif(1<3, reason='Testlogin2模块还在调测中')
class Testlogin2:
def test_case_14(self):
print('---14号用例完成---')
if __name__ == "__main__":
pytest.main(['-vs', 'test_1.py'])
C:\Python39\python.exe D:/se_frame/Cases/MapAaaCases/test_1.py
============================= test session starts =============================
platform win32 -- Python 3.9.0, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\Python39\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.9.0', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.32', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '9.1.1', 'xdist': '2.2.0'}}
rootdir: D:\se_frame, configfile: pytest.ini
plugins: allure-pytest-2.8.32, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.2.0
collecting ... collected 13 items
# 此处省略了部分结果
test_1.py::Testlogin001::test_case_11[5*2-10]
启动浏览器
---进入要执行模块的的界面---
---3号用例完成---
---4号用例完成---
---5号用例完成---
原值:5*2 期望值10
PASSED
退出浏览器
test_1.py::Testlogin001::test_case_12
启动浏览器
---进入要执行模块的的界面---
---3号用例完成---
---4号用例完成---
---5号用例完成---
---12号用例完成---
XPASS (该功能尚未完善,还在调测中)
退出浏览器
test_1.py::Testlogin001::test_case_13 SKIPPED (test_case_13用例还在...)
test_1.py::Testlogin2::test_case_14 SKIPPED (Testlogin2模块还在调测中)
--------- generated html file: file://D:\se_frame\Reports\report.html ---------
================== 10 passed, 2 skipped, 1 xpassed in 0.24s ===================
Process finished with exit code 0