question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can plugins define custom ways to load fixtures?

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
merwokcommented, 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!

1reaction
asottilecommented, Jan 13, 2019

I found this: https://github.com/pytest-dev/pytest/issues/2424#issuecomment-333387206 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:

# $ tail -n999 testing/* tests/* | sed 's/^=/### =/g'
### ==> testing/file_1.yaml <==
f1: 1

### ==> testing/file_2.yaml <==
f2: 2

### ==> testing/file_3.yaml <==
f3: 3

### ==> testing/file_4.yaml <==
f4: 4

### ==> testing/file_5.yaml <==
f5: 5

### ==> tests/conftest.py <==
import os.path

import pytest
import yaml

DATA = os.path.join(os.path.realpath(os.path.dirname(__file__)), '../testing')


def _fixture(filename):
    def fixture_func():
        with open(os.path.join(DATA, filename)) as f:
            return yaml.safe_load(f)

    fixture_func.__name__, _ = os.path.splitext(filename)
    fixture_func.__doc__ = f'Loads {filename} from ./testing'

    # call fixture late so `__doc__` shows up in `pytest --fixtures`
    return pytest.fixture(fixture_func)


globals().update({
    os.path.splitext(f)[0]: _fixture(f) for f in os.listdir(DATA)
})

### ==> tests/test1.py <==
def test(file_1, file_2):
    assert file_1 == file_2
$ pytest tests/test1.py 
============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: /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_2
E       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 diff

tests/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 ./testing
file_2
    Loads file_2.yaml from ./testing
file_3
    Loads file_3.yaml from ./testing
file_4
    Loads file_4.yaml from ./testing
file_5
    Loads file_5.yaml from ./testing

========================= no tests ran in 0.01 seconds =========================
Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing plugins
pytest loads plugin modules at tool startup in the following way: by scanning the command line for the -p no:name option and blocking...
Read more >
Load Fixtures from Cypress Custom Commands
We will look at fixtures, aliases, hooks, and custom commands. ... How to load or import fixtures to be used in the Cypress...
Read more >
How to test cake3 plugins with fixtures - php
I tryed to create a custom bootstrap.php for testing but I cannot figure what has to be there. I copyed this one https://github.com/Xety/Cake3- ......
Read more >
Modularizing Pytest Fixtures
However, how can we define test fixtures such that they are ... These plugins can then be loaded in a test file or...
Read more >
Customizing your pytest test suite (part 2)
The quickest way to switch between different groups, based on the individual test item, is to create a fixture that returns a specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found