How to identify the rerun mode using pytest-rerunfailures?

Viewed 282

I use pytest-rerunfailures for re-running failed tests.

Is there any way to identify that the current test running is in rerun mode or is running for the 1st time (not a rerun)?

1 Answers

Yes, there is a way.

config fixture has all the options associated with a particular test run. Use config.option.lf which will return True/False denoted whether it is a normal run/rerun.

I hope you are already familiar with pytest hook functions. For the below example, I used the pytest_configure hook function. But you can use any hook which gives access to config fixture

Example (conftest.py):-

def pytest_configure(config):
    print(config.option)
    print(config.option.lf)

For the above example result will look like this:-

Namespace(keyword='', markexpr='', maxfail=0, continue_on_collection_errors=False, confcutdir=None, noconftest=False, keepduplicates=False, collect_in_virtualenv=False, importmode='prepend', basetemp=None, durations=None, durations_min=0.005, version=0, plugins=['no:warning'], traceconfig=False, debug=False, showfixtures=False, show_fixtures_per_test=False, verbose=0, no_header=False, no_summary=False, reportchars='fE', disable_warnings=False, showlocals=False, tbstyle='auto', showcapture='all', fulltrace=False, color='auto', code_highlight='yes', capture='fd', runxfail=False, pastebin=None, assertmode='rewrite', xmlpath=None, junitprefix=None, doctestmodules=False, doctestreport='udiff', doctestglob=[], doctest_ignore_import_errors=False, doctest_continue_on_failure=False, last_failed_no_failures='all', stepwise=False, stepwise_skip=False, teamcity=0, no_teamcity=0, markers=False, usepdb=False, usepdb_cls=None, trace=False, lf=False, failedfirst=False, newfirst=False, cacheshow=None, cacheclear=False, pythonwarnings=None, strict_config=False, strict_markers=False, strict=False, inifilename=None, rootdir=None, collectonly=False, pyargs=False, ignore=None, ignore_glob=None, deselect=None, help=False, override_ini=None, setuponly=False, setupshow=False, setupplan=False, log_level=None, log_format=None, log_date_format=None, log_cli_level=None, log_cli_format=None, log_cli_date_format=None, log_file=None, log_file_level=None, log_file_format=None, log_file_date_format=None, log_auto_indent=None, file_or_dir=['test_pytest.py::test_group_a_uppercase'])
False
Related