| 1 | Pytest 6: Dynamically adding xfail marker in test no longer ignores failure |
| 2 | <!-- |
| 3 | Thanks for submitting an issue! |
| 4 | |
| 5 | Here's a quick checklist for what to provide: |
| 6 | --> |
| 7 | |
| 8 | ## Description |
| 9 | |
| 10 | With pytest 5.x, we can dynamically add an xfail to a test `request` object using `request.node.add_marker(mark)` (see example below). In 5.x this treated the failing test like a a test marked statically with an `xfail`. With 6.0.0rc0 it raises. |
| 11 | |
| 12 | ## Versions |
| 13 | |
| 14 | <details> |
| 15 | |
| 16 | ``` |
| 17 | $ pip list |
| 18 | Package Version Location |
| 19 | ----------------------------- ------------------------------- -------------------------------------------------------------- |
| 20 | a 1.0 |
| 21 | aioftp 0.13.0 |
| 22 | aiohttp 3.6.2 |
| 23 | alabaster 0.7.12 |
| 24 | apipkg 1.5 |
| 25 | aplus 0.11.0 |
| 26 | appdirs 1.4.3 |
| 27 | appnope 0.1.0 |
| 28 | arrow 0.15.7 |
| 29 | aspy.yaml 1.3.0 |
| 30 | astropy 3.2.3 |
| 31 | asv 0.4.1 |
| 32 | async-timeout 3.0.1 |
| 33 | atomicwrites 1.3.0 |
| 34 | attrs 19.1.0 |
| 35 | aws-sam-translator 1.15.1 |
| 36 | aws-xray-sdk 0.95 |
| 37 | Babel 2.7.0 |
| 38 | backcall 0.1.0 |
| 39 | binaryornot 0.4.4 |
| 40 | black 19.10b0 |
| 41 | bleach 3.1.0 |
| 42 | blurb 1.0.7 |
| 43 | bokeh 1.3.4 |
| 44 | boto 2.49.0 |
| 45 | boto3 1.7.84 |
| 46 | botocore 1.10.84 |
| 47 | bqplot 0.12.12 |
| 48 | branca 0.3.1 |
| 49 | cachetools 4.1.0 |
| 50 | certifi 2019.9.11 |
| 51 | cffi 1.13.2 |
| 52 | cfgv 2.0.1 |
| 53 | cfn-lint 0.25.0 |
| 54 | cftime 1.0.4.2 |
| 55 | chardet 3.0.4 |
| 56 | Click 7.0 |
| 57 | click-plugins 1.1.1 |
| 58 | cligj 0.5.0 |
| 59 | cloudpickle 1.2.2 |
| 60 | colorama 0.4.3 |
| 61 | colorcet 2.0.2 |
| 62 | coloredlogs 14.0 |
| 63 | cookiecutter 1.7.2 |
| 64 | cookies 2.2.1 |
| 65 | coverage 4.5.4 |
| 66 | cryptography 2.8 |
| 67 | cycler 0.10.0 |
| 68 | Cython 3.0a5 |
| 69 | cytoolz 0.10.1 |
| 70 | dask 2.4.0 /Users/taugspurger/Envs/pandas-dev/lib/python3.7/site-packages |
| 71 | DateTime 4.3 |
| 72 | decorator 4.4.0 |
| 73 | defusedxml 0.6.0 |
| 74 | Deprecated 1.2.7 |
| 75 | distributed 2.4.0 |
| 76 | docker 4.1.0 |
| 77 | docutils 0.15.2 |
| 78 | ecdsa 0.14.1 |
| 79 | entrypoints 0.3 |
| 80 | et-xmlfile 1.0.1 |
| 81 | execnet 1.7.1 |
| 82 | fastparquet 0.3.3 /Users/taugspurger/sandbox/fastparquet |
| 83 | feedparser 5.2.1 |
| 84 | Fiona 1.8.8 |
| 85 | flake8 3.7.9 |
| 86 | flake8-rst 0.7.1 |
| 87 | fletcher 0.3.1 |
| 88 | flit 2.1.0 |
| 89 | flit-core 2.1.0 |
| 90 | fsspec 0.7.4 |
| 91 | future 0.18.2 |
| 92 | gcsfs 0.6.2 |
| 93 | geopandas 0.6.0+1.g95b8e1a.dirty /Users/taugspurger/sandbox/geopandas |
| 94 | gitdb2 2.0.5 |
| 95 | GitPython 3.0.2 |
| 96 | google-auth 1.16.1 |
| 97 | google-auth-oauthlib 0.4.1 |
| 98 | graphviz 0.13 |
| 99 | h5py 2.10.0 |
| 100 | HeapDict 1.0.1 |
| 101 | holoviews 1.12.6 |
| 102 | humanfriendly 8.1 |
| 103 | hunter 3.1.3 |
| 104 | hvplot 0.5.2 |
| 105 | hypothesis 4.36.2 |
| 106 | identify 1.4.7 |
| 107 | idna 2.8 |
| 108 | imagesize 1.1.0 |
| 109 | importlib-metadata 0.23 |
| 110 | importlib-resources 1.0.2 |
| 111 | iniconfig 1.0.0 |
| 112 | intake 0.5.3 |
| 113 | ipydatawidgets 4.0.1 |
| 114 | ipykernel 5.1.2 |
| 115 | ipyleaflet 0.13.0 |
| 116 | ipympl 0.5.6 |
| 117 | ipython 7.11.1 |
| 118 | ipython-genutils 0.2.0 |
| 119 | ipyvolume 0.5.2 |
| 120 | ipyvue 1.3.2 |
| 121 | ipyvuetify 1.4.0 |
| 122 | ipywebrtc 0.5.0 |
| 123 | ipywidgets 7.5.1 |
| 124 | isort 4.3.21 |
| 125 | jdcal 1.4.1 |
| 126 | jedi 0.16.0 |
| 127 | Jinja2 2.11.2 |
| 128 | jinja2-time 0.2.0 |
| 129 | jmespath 0.9.4 |
| 130 | joblib 0.14.1 |
| 131 | json5 0.9.4 |
| 132 | jsondiff 1.1.1 |
| 133 | jsonpatch 1.24 |
| 134 | jsonpickle 1.2 |
| 135 | jsonpointer 2.0 |
| 136 | jsonschema 3.0.2 |
| 137 | jupyter 1.0.0 |
| 138 | jupyter-client 5.3.3 |
| 139 | jupyter-console 6.0.0 |
| 140 | jupyter-core 4.5.0 |
| 141 | jupyterlab 2.1.2 |
| 142 | jupyterlab-server 1.1.4 |
| 143 | kiwisolver 1.1.0 |
| 144 | line-profiler 2.1.1 |
| 145 | llvmlite 0.33.0 |
| 146 | locket 0.2.0 /Users/taugspurger/sandbox/locket.py |
| 147 | lxml 4.5.0 |
| 148 | manhole 1.6.0 |
| 149 | Markdown 3.1.1 |
| 150 | MarkupSafe 1.1.1 |
| 151 | matplotlib 3.2.2 |
| 152 | mccabe 0.6.1 |
| 153 | memory-profiler 0.55.0 |
| 154 | mistune 0.8.4 |
| 155 | mock 3.0.5 |
| 156 | more-itertools 7.2.0 |
| 157 | moto 1.3.6 |
| 158 | msgpack 0.6.2 |
| 159 | multidict 4.5.2 |
| 160 | munch 2.3.2 |
| 161 | mypy 0.730 |
| 162 | mypy-extensions 0.4.1 |
| 163 | nbconvert 5.6.0 |
| 164 | nbformat 4.4.0 |
| 165 | nbsphinx 0.4.2 |
| 166 | nest-asyncio 1.3.3 |
| 167 | nodeenv 1.3.3 |
| 168 | notebook 6.0.1 |
| 169 | numexpr 2.7.1 |
| 170 | numpy 1.19.0 |
| 171 | numpydoc 1.0.0.dev0 |
| 172 | oauthlib 3.1.0 |
| 173 | odfpy 1.4.0 |
| 174 | openpyxl 3.0.3 |
| 175 | packaging 20.4 |
| 176 | pandas 1.1.0.dev0+1758.g035e1fe831 /Users/taugspurger/sandbox/pandas |
| 177 | pandas-sphinx-theme 0.0.1.dev0 /Users/taugspurger/sandbox/pandas-sphinx-theme |
| 178 | pandocfilters 1.4.2 |
| 179 | param 1.9.2 |
| 180 | parfive 1.0.0 |
| 181 | parso 0.6.0 |
| 182 | partd 1.0.0 |
| 183 | pathspec 0.8.0 |
| 184 | patsy 0.5.1 |
| 185 | pexpect 4.7.0 |
| 186 | pickleshare 0.7.5 |
| 187 | Pillow 6.1.0 |
| 188 | pip 20.0.2 |
| 189 | pluggy 0.13.0 |
| 190 | poyo 0.5.0 |
| 191 | pre-commit 1.18.3 |
| 192 | progressbar2 3.51.3 |
| 193 | prometheus-client 0.7.1 |
| 194 | prompt-toolkit 2.0.9 |
| 195 | psutil 5.6.3 |
| 196 | ptyprocess 0.6.0 |
| 197 | py 1.9.0 |
| 198 | pyaml 20.4.0 |
| 199 | pyarrow 0.16.0 |
| 200 | pyasn1 0.4.7 |
| 201 | pyasn1-modules 0.2.8 |
| 202 | pycodestyle 2.5.0 |
| 203 | pycparser 2.19 |
| 204 | pycryptodome 3.9.8 |
| 205 | pyct 0.4.6 |
| 206 | pydata-sphinx-theme 0.1.1 |
| 207 | pydeps 1.9.0 |
| 208 | pyflakes 2.1.1 |
| 209 | PyGithub 1.44.1 |
| 210 | Pygments 2.4.2 |
| 211 | PyJWT 1.7.1 |
| 212 | pyparsing 2.4.2 |
| 213 | pyproj 2.4.0 |
| 214 | pyrsistent 0.15.4 |
| 215 | pytest 5.4.3 |
| 216 | pytest-asyncio 0.10.0 |
| 217 | pytest-cov 2.8.1 |
| 218 | pytest-cover 3.0.0 |
| 219 | pytest-forked 1.0.2 |
| 220 | pytest-repeat 0.8.0 |
| 221 | pytest-xdist 1.29.0 |
| 222 | python-boilerplate 0.1.0 |
| 223 | python-dateutil 2.8.0 |
| 224 | python-jose 2.0.2 |
| 225 | python-jsonrpc-server 0.3.2 |
| 226 | python-language-server 0.31.4 |
| 227 | python-slugify 4.0.1 |
| 228 | python-utils 2.4.0 |
| 229 | pythreejs 2.2.0 |
| 230 | pytoml 0.1.21 |
| 231 | pytz 2019.2 |
| 232 | pyviz-comms 0.7.2 |
| 233 | PyYAML 5.1.2 |
| 234 | pyzmq 18.1.0 |
| 235 | qtconsole 4.5.5 |
| 236 | regex 2020.6.8 |
| 237 | requests 2.24.0 |
| 238 | requests-oauthlib 1.3.0 |
| 239 | responses 0.10.6 |
| 240 | rsa 4.0 |
| 241 | rstcheck 3.3.1 |
| 242 | s3fs 0.4.2 |
| 243 | s3transfer 0.1.13 |
| 244 | scikit-learn 0.22.2.post1 |
| 245 | scipy 1.3.1 |
| 246 | seaborn 0.9.0 |
| 247 | Send2Trash 1.5.0 |
| 248 | setuptools 49.2.0 |
| 249 | Shapely 1.6.4.post2 |
| 250 | six 1.12.0 |
| 251 | smmap2 2.0.5 |
| 252 | snakeviz 2.0.1 |
| 253 | snowballstemmer 1.9.1 |
| 254 | sortedcontainers 2.1.0 |
| 255 | sparse 0.10.0 |
| 256 | Sphinx 3.1.1 |
| 257 | sphinxcontrib-applehelp 1.0.2 |
| 258 | sphinxcontrib-devhelp 1.0.2 |
| 259 | sphinxcontrib-htmlhelp 1.0.3 |
| 260 | sphinxcontrib-jsmath 1.0.1 |
| 261 | sphinxcontrib-qthelp 1.0.3 |
| 262 | sphinxcontrib-serializinghtml 1.1.4 |
| 263 | sphinxcontrib-websupport 1.1.2 |
| 264 | sphinxcontrib.youtube 0.1.2 |
| 265 | SQLAlchemy 1.3.11 |
| 266 | sshpubkeys 3.1.0 |
| 267 | statsmodels 0.10.2 |
| 268 | stdlib-list 0.6.0 |
| 269 | sunpy 1.1.dev518+gcad2d473f.d20191103 /Users/taugspurger/sandbox/sunpy |
| 270 | tables 3.6.1 |
| 271 | tabulate 0.8.6 |
| 272 | tblib 1.4.0 |
| 273 | terminado 0.8.2 |
| 274 | test 1.0.0 |
| 275 | testpath 0.4.2 |
| 276 | text-unidecode 1.3 |
| 277 | thrift 0.13.0 |
| 278 | toml 0.10.0 |
| 279 | toolz 0.10.0 |
| 280 | tornado 6.0.3 |
| 281 | tqdm 4.37.0 |
| 282 | traitlets 4.3.2 |
| 283 | traittypes 0.2.1 |
| 284 | typed-ast 1.4.0 |
| 285 | typing-extensions 3.7.4 |
| 286 | ujson 1.35 |
| 287 | urllib3 1.25.5 |
| 288 | vaex 3.0.0 |
| 289 | vaex-arrow 0.5.1 |
| 290 | vaex-astro 0.7.0 |
| 291 | vaex-core 2.0.2 |
| 292 | vaex-hdf5 0.6.0 |
| 293 | vaex-jupyter 0.5.1.post0 |
| 294 | vaex-ml 0.9.0 |
| 295 | vaex-server 0.3.1 |
| 296 | vaex-viz 0.4.0 |
| 297 | virtualenv 16.7.5 |
| 298 | wcwidth 0.1.7 |
| 299 | webencodings 0.5.1 |
| 300 | websocket-client 0.56.0 |
| 301 | Werkzeug 0.16.0 |
| 302 | wheel 0.34.2 |
| 303 | widgetsnbextension 3.5.1 |
| 304 | wrapt 1.11.2 |
| 305 | xarray 0.14.1+36.gb3d3b448 /Users/taugspurger/sandbox/xarray |
| 306 | xlwt 1.3.0 |
| 307 | xmltodict 0.12.0 |
| 308 | yarl 1.3.0 |
| 309 | zict 1.0.0 |
| 310 | zipp 0.6.0 |
| 311 | zope.interface 4.7.1 |
| 312 | ``` |
| 313 | |
| 314 | </details> |
| 315 | |
| 316 | - [ ] pytest and operating system versions |
| 317 | |
| 318 | Pytest 6.0.1rc0 and MacOS 10.14.5 |
| 319 | |
| 320 | ```python |
| 321 | # file: test_foo.py |
| 322 | import pytest |
| 323 | |
| 324 | |
| 325 | def test_xfail_test(request): |
| 326 | mark = pytest.mark.xfail(reason="xfail") |
| 327 | request.node.add_marker(mark) |
| 328 | assert 0 |
| 329 | ``` |
| 330 | |
| 331 | With 5.4.3 |
| 332 | |
| 333 | ``` |
| 334 | |
| 335 | $ pytest -rsx test_foo.py |
| 336 | =============================================================================== test session starts ================================================================================ |
| 337 | platform darwin -- Python 3.7.6, pytest-5.4.3, py-1.9.0, pluggy-0.13.0 |
| 338 | hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/Users/taugspurger/sandbox/.hypothesis/examples') |
| 339 | rootdir: /Users/taugspurger/sandbox |
| 340 | plugins: xdist-1.29.0, hypothesis-4.36.2, forked-1.0.2, repeat-0.8.0, asyncio-0.10.0, cov-2.8.1 |
| 341 | collected 1 item |
| 342 | |
| 343 | test_foo.py x [100%] |
| 344 | |
| 345 | ============================================================================= short test summary info ============================================================================== |
| 346 | XFAIL test_foo.py::test_xfail_test |
| 347 | xfail |
| 348 | ================================================================================ 1 xfailed in 0.07s ================================================================================ |
| 349 | ``` |
| 350 | |
| 351 | With 6.0.0rc0 |
| 352 | |
| 353 | ``` |
| 354 | $ pytest -rsx test_foo.py |
| 355 | =============================================================================== test session starts ================================================================================ |
| 356 | platform darwin -- Python 3.7.6, pytest-6.0.0rc1, py-1.9.0, pluggy-0.13.0 |
| 357 | hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/Users/taugspurger/sandbox/.hypothesis/examples') |
| 358 | rootdir: /Users/taugspurger/sandbox |
| 359 | plugins: xdist-1.29.0, hypothesis-4.36.2, forked-1.0.2, repeat-0.8.0, asyncio-0.10.0, cov-2.8.1 |
| 360 | collected 1 item |
| 361 | |
| 362 | test_foo.py F [100%] |
| 363 | |
| 364 | ===================================================================================== FAILURES ===================================================================================== |
| 365 | _________________________________________________________________________________ test_xfail_test __________________________________________________________________________________ |
| 366 | |
| 367 | request = <FixtureRequest for <Function test_xfail_test>> |
| 368 | |
| 369 | def test_xfail_test(request): |
| 370 | mark = pytest.mark.xfail(reason="xfail") |
| 371 | request.node.add_marker(mark) |
| 372 | > assert 0 |
| 373 | E assert 0 |
| 374 | |
| 375 | test_foo.py:7: AssertionError |
| 376 | ``` |
| 377 | |