栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2021-11-10

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-11-10

关键字:--reruns

pytest运行参数--reruns主要用于用例运行失败后,重新运行失败的用例,运行规则为遇到失败的用例,就根据重试次数运行失败的用例,然后在执行剩下的用例。举例如下:

import pytest


class TestPytestFeatures(object):
    def test_case_login(self):
        num = 1
        assert num == 2

    def test_case_logout(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)

    def test_case_shopping(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)

    def test_case_walk(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, dict)


if __name__ == "__main__":
    pytest.main(["--reruns=2", "test_case_pytest_features.py"])

"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43, forked-1.3.0, rerunfailures-10.2, xdist-2.4.0
collected 4 items

test_case_pytest_features.py RRF..R                                      [100%]R [100%]F [100%]

================================== FAILURES ===================================
_____________________ TestPytestFeatures.test_case_login ______________________

self = 

    def test_case_login(self):
        num = 1
>       assert num == 2
E       assert 1 == 2

test_case_pytest_features.py:14: AssertionError
______________________ TestPytestFeatures.test_case_walk ______________________

self = 

    def test_case_walk(self):
        tmp = ["a", "b"]
>       assert isinstance(tmp, dict)
E       AssertionError: assert False
E        +  where False = isinstance(['a', 'b'], dict)

test_case_pytest_features.py:26: AssertionError
============================== warnings summary ===============================
test_case_pytest_features.py:32
  E:icp_capphelptest_case_pytest_features.py:32: DeprecationWarning: invalid escape sequence i
 

-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_login - as...
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_walk - Ass...
=============== 2 failed, 2 passed, 1 warning, 4 rerun in 0.14s ===============

"""

若已知某些用例不稳定,则可以使用@pytest.mark.flaky(n)来指定失败用例重试次数。

import pytest


class TestPytestFeatures(object):
    @pytest.mark.flaky(2)
    def test_case_login(self):
        num = 1
        assert num == 2

    def test_case_logout(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)

    def test_case_shopping(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)

    def test_case_walk(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, dict)


if __name__ == "__main__":
    pytest.main([ "test_case_pytest_features.py"])

"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43, forked-1.3.0, rerunfailures-10.2, xdist-2.4.0
collected 4 items

test_case_pytest_features.py RRF..F                                      [100%]

================================== FAILURES ===================================
_____________________ TestPytestFeatures.test_case_login ______________________

self = 

    @pytest.mark.flaky(2)
    def test_case_login(self):
        num = 1
>       assert num == 2
E       assert 1 == 2

test_case_pytest_features.py:15: AssertionError
______________________ TestPytestFeatures.test_case_walk ______________________

self = 

    def test_case_walk(self):
        tmp = ["a", "b"]
>       assert isinstance(tmp, dict)
E       AssertionError: assert False
E        +  where False = isinstance(['a', 'b'], dict)

test_case_pytest_features.py:27: AssertionError
============================== warnings summary ===============================
test_case_pytest_features.py:33
  E:icp_capphelptest_case_pytest_features.py:33: DeprecationWarning: invalid escape sequence i


-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_login - as...
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_walk - Ass...
=============== 2 failed, 2 passed, 1 warning, 2 rerun in 0.10s ===============

"""

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/461542.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号