You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can plugins define custom ways to load fixtures? #4636
Not sure if this is a feature request or a documentation issue!
I would like to define fixtures in YAML files. I could write a yaml-loader fixture + use parametrize to give a filename to the loader, but it would be even better if I could instead write test_something(some_data) + some_data.yml and have the data from the YAML file loaded and passed to the test function.
(I am not asking about a YAML test collector, as found in an example in the docs and in plugins like yamlwsgi.)
From reading docs and tickets, it is not clear to me if this is possible.
It looks like pytest_runtest_setup might be able to transform a test function before it’s run, but the documentation on that hook is very scarce.
I think I understand the question, but don't know enough about the pytest internals to answer.
Basically the ask is to have something like:
testing/
data/
foo.yaml
bar.yaml
And then something that allows you to generate fixtures so tests like this would work:
deftest_foo(foo):
... # the `foo` fixture contains the loaded contents of testing/data/foo.yamldeftest_bar(bar):
... # the `bar` fixture contains the loaded contents of testing/data/bar.yaml
$ pytest tests/test1.py ============================= test session starts ==============================platform linux -- Python 3.6.7, pytest-4.1.1, py-1.7.0, pluggy-0.8.1rootdir: /tmp/t, inifile:collected 1 item tests/test1.py F [100%]=================================== FAILURES ===================================_____________________________________ test _____________________________________file_1 = {'f1': 1}, file_2 = {'f2': 2} def test(file_1, file_2):
> assert file_1 == file_2E AssertionError: assert {'f1': 1} == {'f2': 2}E Left contains more items:E {'f1': 1}E Right contains more items:E {'f2': 2}E Use -v to get the full difftests/test1.py:2: AssertionError=========================== 1 failed in 0.06 seconds ===========================
even threw in a little bit to make pytest --fixtures output work correctly:
$ pytest --fixtures | tail -14------------------------ fixtures defined from conftest ------------------------file_1 Loads file_1.yaml from ./testingfile_2 Loads file_2.yaml from ./testingfile_3 Loads file_3.yaml from ./testingfile_4 Loads file_4.yaml from ./testingfile_5 Loads file_5.yaml from ./testing========================= no tests ran in 0.01 seconds =========================
Activity
nicoddemus commentedon Jan 12, 2019
Hi @merwok
Sorry, I didn't quite understand that, can you please elaborate?
asottile commentedon Jan 12, 2019
I think I understand the question, but don't know enough about the pytest internals to answer.
Basically the ask is to have something like:
And then something that allows you to generate fixtures so tests like this would work:
merwok commentedon Jan 13, 2019
Yes this is the idea!
asottile commentedon Jan 13, 2019
I found this: #2424 (comment) not sure if it implements exactly what you're looking for but looks promising.
A little bit of hackery, but it looks like it works:
even threw in a little bit to make
pytest --fixturesoutput work correctly:merwok commentedon Jan 13, 2019
That’s interesting! Using module-level
__dir__and__getattr__I could even avoid mucking with globals. I will try this idea, thanks!