洞察探索如何通过一套代码实现跨平台小程序开发与高效管理,助力企业数字化转型
1603
2022-10-15
Pytest----Pytest自动化测试框架中告警的用法
一、使用-W参数可以设置告警不显示或者报错
test_demo.py代码如下
import warningsdef api_v1(): warnings.warn(UserWarning("api v1, should use functions from v2")) return 1def test_one(): assert api_v1() == 1
使用pytest执行结果如下,这里在执行最后会显示告警
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py . [100%]=========================================================================== warnings summary ===========================================================================test_demo.py::test_one D:\src\blog\tests\test_demo.py:5: UserWarning: api v1, should use functions from v2 warnings.warn(UserWarning("api v1, should use functions from v2"))-- Docs: 1 passed, 1 warning in 0.03s =====================================================================
(1)使用 -W 参数,加上error选项,可以将指定的告警转换为错误
$ pytest -W error::UserWarning========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py F [100%]=============================================================================== FAILURES ===============================================================================_______________________________________________________________________________ test_one _______________________________________________________________________________ def test_one():> assert api_v1() == 1test_demo.py:9:_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def api_v1():> warnings.warn(UserWarning("api v1, should use functions from v2"))E UserWarning: api v1, should use functions from v2test_demo.py:5: UserWarning======================================================================= short test summary info ========================================================================FAILED test_demo.py::test_one - UserWarning: api v1, should use functions from v2========================================================================== 1 failed in 0.10s ===========================================================================
(2)使用 -W 参数,加上ignore选项,可以忽略指定的告警
$ pytest -W ignore::UserWarning========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py . [100%]========================================================================== 1 passed in 0.02s ===========================================================================
二、使用pytest.ini配置可以设置告警不显示或者报错
(1)可以在pytest.ini中配置,如下在pytest.ini中配置忽略指定的告警
pytest.ini内容如下
[pytest]filterwarnings =
使用pytest执行结果:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py . [100%]========================================================================== 1 passed in 0.02s ===========================================================================
(2)如下,在pytest.ini中配置将指定告警转换为错误
pytest.ini内容如下
[pytest]filterwarnings =
使用pytest执行结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py F [100%]=============================================================================== FAILURES ===============================================================================_______________________________________________________________________________ test_one _______________________________________________________________________________ def test_one():> assert api_v1() == 1test_demo.py:9:_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def api_v1():> warnings.warn(UserWarning("api v1, should use functions from v2"))E UserWarning: api v1, should use functions from v2test_demo.py:5: UserWarning======================================================================= short test summary info ========================================================================FAILED test_demo.py::test_one - UserWarning: api v1, should use functions from v2========================================================================== 1 failed in 0.10s ===========================================================================
(3)通过对告警内容正则匹配忽略告警
pytest.ini内容如下:
[pytest]filterwarnings =
pytest执行结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py . [100%]========================================================================== 1 passed in 0.02s ===========================================================================
(4)通过对告警内容正则匹配报错
pytest.ini内容如下:
[pytest]filterwarnings =
pytest命令执行结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py F [100%]=============================================================================== FAILURES ===============================================================================_______________________________________________________________________________ test_one _______________________________________________________________________________ def test_one():> assert api_v1() == 1test_demo.py:9:_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def api_v1():> warnings.warn(UserWarning("api v1, should use functions from v2"))E UserWarning: api v1, should use functions from v2test_demo.py:5: UserWarning======================================================================= short test summary info ========================================================================FAILED test_demo.py::test_one - UserWarning: api v1, should use functions from v2========================================================================== 1 failed in 0.10s ===========================================================================
三、使用@pytest.mark.filterwarnings可以设置告警不显示或者报错
test_demo.py代码如下:
import warningsimport pytestdef api_v1(): warnings.warn(UserWarning("api v1, should use functions from v2")) return 1@pytest.mark.filterwarnings("ignore:api v1")def test_one(): assert api_v1() == 1@pytest.mark.filterwarnings("ignore::UserWarning")def test_two(): assert api_v1() == 1@pytest.mark.filterwarnings("error:api v1")def test_three(): assert api_v1() == 1@pytest.mark.filterwarnings("error::UserWarning")def test_four(): assert api_v1() == 1
pytest命令执行结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 4 items test_demo.py ..FF [100%]=============================================================================== FAILURES ===============================================================================______________________________________________________________________________ test_three ______________________________________________________________________________ @pytest.mark.filterwarnings("error:api v1") def test_three():> assert api_v1() == 1test_demo.py:21:_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def api_v1():> warnings.warn(UserWarning("api v1, should use functions from v2"))E UserWarning: api v1, should use functions from v2test_demo.py:6: UserWarning______________________________________________________________________________ test_four _______________________________________________________________________________ @pytest.mark.filterwarnings("error::UserWarning") def test_four():> assert api_v1() == 1test_demo.py:26:_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def api_v1():> warnings.warn(UserWarning("api v1, should use functions from v2"))E UserWarning: api v1, should use functions from v2test_demo.py:6: UserWarning======================================================================= short test summary info ========================================================================FAILED test_demo.py::test_three - UserWarning: api v1, should use functions from v2FAILED test_demo.py::test_four - UserWarning: api v1, should use functions from v2===================================================================== 2 failed, 2 passed in 0.12s ======================================================================
四、对一个测试文件中中的所有测试函数中产生的告警都转换为错误或者都忽略
test_demo.py内容如下:
import warningsimport pytestpytestmark = pytest.mark.filterwarnings("error")@pytest.mark.filterwarnings("ignore:api v1")def test_one(): warnings.warn(UserWarning("user warning in test_one...")) assert 1 == 1@pytest.mark.filterwarnings("ignore::UserWarning")def test_two(): warnings.warn(DeprecationWarning("deprecation warning in test_one...")) assert 1 == 1
pytest执行结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 2 items test_demo.py FF [100%]=============================================================================== FAILURES ===============================================================================_______________________________________________________________________________ test_one _______________________________________________________________________________ @pytest.mark.filterwarnings("ignore:api v1") def test_one():> warnings.warn(UserWarning("user warning in test_one..."))E UserWarning: user warning in test_one...test_demo.py:9: UserWarning_______________________________________________________________________________ test_two _______________________________________________________________________________ @pytest.mark.filterwarnings("ignore::UserWarning") def test_two():> warnings.warn(DeprecationWarning("deprecation warning in test_one..."))E DeprecationWarning: deprecation warning in test_one...test_demo.py:14: DeprecationWarning======================================================================= short test summary info ========================================================================FAILED test_demo.py::test_one - UserWarning: user warning in test_one...FAILED test_demo.py::test_two - DeprecationWarning: deprecation warning in test_one...========================================================================== 2 failed in 0.12s ===========================================================================
将test_demo.py中pytestmark中的error修改为ignore,即:
import warningsimport pytestpytestmark = pytest.mark.filterwarnings("ignore")@pytest.mark.filterwarnings("ignore:api v1")def test_one(): warnings.warn(UserWarning("user warning in test_one...")) assert 1 == 1@pytest.mark.filterwarnings("ignore::UserWarning")def test_two(): warnings.warn(DeprecationWarning("deprecation warning in test_one...")) assert 1 == 1
此时pytest执行结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 2 items test_demo.py .. [100%]========================================================================== 2 passed in 0.03s ===========================================================================
五、关闭所有告警
test_demo.py内容如下:
import warningsimport pytest@pytest.mark.filterwarnings("ignore:api v1")def test_one(): warnings.warn(UserWarning("user warning in test_one...")) assert 1 == 1@pytest.mark.filterwarnings("ignore::UserWarning")def test_two(): warnings.warn(DeprecationWarning("deprecation warning in test_one...")) assert 1 == 1
(1)使用–disable-warnings参数关闭告警详情 如:
$ pytest --disable-warnings========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 2 items test_demo.py .. [100%]==================================================================== 2 passed, 2 warnings in 0.03s =====================================================================
(2)在pytest.ini按照如下配置
[pytest]addopts=--disable-warnings
执行pytest结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 2 items test_demo.py .. [100%]==================================================================== 2 passed, 2 warnings in 0.02s =====================================================================
(3)使用pytest -p no:warnings彻底屏蔽告警
执行如下:
$ pytest -p no:warnings========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 2 items test_demo.py .. [100%]========================================================================== 2 passed in 0.03s ===========================================================================
(4)通过pytest.ini配置如下配置,彻底屏蔽告警
[pytest]addopts=-p no:warnings
此时执行pytest结果如下
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 2 items test_demo.py .. [100%]========================================================================== 2 passed in 0.02s ===========================================================================
六、对产生的告警进行断言
(1)对告警类型进行断言,test_demo.py代码如下
import warningsimport pytestdef test_warning(): with pytest.warns(UserWarning): warnings.warn("my warning", UserWarning)
使用pytest执行结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py . [100%]========================================================================== 1 passed in 0.03s ===========================================================================
(2)通过正则匹配对告警内容进行匹配,test_demo.py代码如下:
import warningsimport pytestdef test_warning(): with pytest.warns(UserWarning, match=r'must be \d+$'): warnings.warn("value must be 42", UserWarning)
pytest执行结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py . [100%]========================================================================== 1 passed in 0.02s ===========================================================================
(3)可以对一个函数中的产生的告警进行断言
test_demo.py代码如下:
import warningsimport pytestdef func_demo(a,b): warnings.warn("value must be 42", UserWarning)def test_warning(): pytest.warns(UserWarning,func_demo,10,20)
使用pytest执行结果如下:
$ pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py . [100%]========================================================================== 1 passed in 0.02s ===========================================================================
七、记录测试函数中产生的告警的fixture:recwarn
如下将整个测试函数中产生的告警均记录下来可供解析,test_demo.py代码如下:
import warningsdef test_hello(recwarn): warnings.warn("hello", UserWarning) warnings.warn("world", UserWarning) assert len(recwarn) == 2 w = recwarn.pop() print("---------------------------") print(w.category) print(w.message) print(w.filename) print(w.lineno) print("---------------------------") w = recwarn.pop() print("---------------------------") print(w.category) print(w.message) print(w.filename) print(w.lineno) print("---------------------------")
执行结果如下:
$ pytest -s========================================================================= test session starts ==========================================================================platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0rootdir: D:\src\blog\tests, configfile: pytest.iniplugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0collected 1 item test_demo.py ---------------------------
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~