https://pantsbuild.org/ logo
#general
Title
# general
g

gorgeous-winter-99296

03/07/2023, 8:35 AM
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

bitter-ability-32190

03/07/2023, 4:30 PM
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

gorgeous-winter-99296

03/07/2023, 4:31 PM
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

bitter-ability-32190

03/07/2023, 4:33 PM
Yeah, you'll need to have Pants clued in to resource/file dependencies in most cases
h

happy-kitchen-89482

03/07/2023, 6:37 PM
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

gorgeous-winter-99296

03/08/2023, 9:55 AM
👍 Thanks for the input! I guess I'll get used to having fixtures in src.
h

happy-kitchen-89482

03/08/2023, 6:27 PM
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.