Are there any good patterns for handling file-bas...
# general
g
Are there any good patterns for handling file-based fixtures (e.g. example data) with "nested" tests, i.e, next to sources? Our prior art is all
tests/fixtures
in a separate subdir, but it doesn't seem like the "pants way" but putting a
src/fixtures
dir seems weird too.
b
The "pants way" here is just the "pytest way". How are you referencing your fixtures in
tests/fixtures
? FWIW we just use
conftest.py
files and Pants auto-picks those up
For fixtures that live outside
conftest.py
we have the "string imports" feature of Pants enabled, and use
pytest_plugins = ["<http://path.to|path.to>.fixture"]
g
The way we've done it previously is just an
__init__.py
with a
FIXTURES = Path(__file__).parent / 'fixtures'
and then various helpers to load from that. Maybe this is super-wrong from pytest too. But I imagined I'd need some way of explicitly telling pants these are resources for the tests?
b
Yeah, you'll need to have Pants clued in to resource/file dependencies in most cases
h
I like to have tests next to sources (e.g.,
src/python/foo/bar.py
,
src/python/foo/bar_test.py
) and having
src/python/foo/fixtures
seems fine to me!
The reason other tools want
test/
and
fixtures/
dirs separate from
src/
is that they don't know how to not bundle those along with the source at packaging time. But pants does know that.
g
👍 Thanks for the input! I guess I'll get used to having fixtures in src.
h
You can always tuck them away in a completely different dir if you prefer, Pants is agnostic to that. But from an organizational standpoint it seems easier to keep test data near where it's used.