pytest.fixture/mark

1.pytest.fixture

[conftest.py]

@pytest.fixture(scope=“session”,autouse=True)

在该目录下的所有的测试用例只会执行一次

@pytest.fixture(scope=“module”,autouse=True)

在该目录下的每个py文件会执行一次

@pytest.fixture(scope=“class”,autouse=True)

在该目录下的每个类会执行一次

@pytest.fixture(scope=“function”,autouse=True)

在该目录下的每个测试函数会执行一次

2.pytest.mark

@pytest.mark.allure_label: allure label marker
@pytest.mark.allure_link: allure link marker
@pytest.mark.allure_display_name: allure test name marker
@pytest.mark.allure_description: allure description
@pytest.mark.allure_description_html: allure description html
@pytest.mark.filterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/latest/warnings.html#pytest-mark-filterwarnings
@pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test.
@pytest.mark.skipif(condition): skip the given test function if eval(condition) results in a True value.  Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. see https://docs.pytest.org/en/latest/skipping.html
@pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False): mark the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expected, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See https://docs.pytest.org/en/latest/skipping.html
@pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2.see https://docs.pytest.org/en/latest/parametrize.html for more info and examples.
@pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see https://docs.pytest.org/en/latest/fixture.html#usefixtures
@pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible.
@pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.

@pytest.mark.flaky(reruns=5, reruns_delay=2) 

@pytest.mark.skip() #1、跳过方法或用例,未备注原因

@pytest.mark.skip(reason='跳过一个方法或一个测试用例') #2、跳过方法或用例,备注了原因

@pytest.mark.skipif(1==1,reason='跳过一个方法或一个测试用例') #3、当条件满足,跳过方法或用例,备注了原因
 

如果你还想从头学起Pytest,可以看看这个:https://www.cnblogs.com/poloyy/p/12675457.html

Pytest 系列(28)- 参数化 parametrize + @allure.title() 动态生成标题

https://www.cnblogs.com/poloyy/p/13891265.html

3.pytest.ini

[pytest.ini]

-x, --exitfirst 遇到失败停止

正常情况下,一个用例失败后,会继续运行后续用例 ,直到所有用例运行完为止。-x 参数会让整个测试在遇到第一次失败就停止。这取决于你有没有这样的需要,一般来说可能用不上。

--maxfail=num 遇到多少次失败就停止

与上面-x一样,不过-x是遇到第一次失败就停止。而--maxfail可以指定遇到几个测试用例失败就停止

[pytest]
addopts = --capture=sys
          -v 
          -q
          --html=../Radar_report.html
          --self-contained-html
          --junitxml=../result.xml
          --alluredir ../allure-results
          --clean-alluredir
        ;   --reruns 2
          --reruns-delay 2
        ;   --cov=./tests
        ;   --cov-report=html
testpaths = ./tests
python_files = *_test.py
python_classes = Test*
python_functions = test_*
markers =
    repeat: repeat times
    optionalhook
    hookwrapper

filterwarnings =
    ignore::DeprecationWarning:.*:

# cli live log
log_cli = True
log_cli_level = info
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_date_format = %Y-%m-%d %H:%M:%S
# capture log
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
log_level = info


; logging.info("info log in test_func2...")
; logging.error("error log in test_func2...")
; logging.warning("warnning log in test_func2...")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值