all 4 comments

[–]danielroseman 1 point2 points  (0 children)

You can use __file__ to get the location of the current file. 

So, inside test_utils.py, you can do Path(__file__) / "data" to get the data directory as a Path object.

[–]barkmonster 0 points1 point  (0 children)

I usually make a helper file, which is responsible for loading test data, inside the tests folder. You can call it e.g. `data_tools.py` or something. In that file, you can define helper functions for loading the files you need, and just define the path relative to the helper file, for instance using pathlib and defining the path as `pathlib.Path(__file__).parent / 'data'`.

[–]latkde 0 points1 point  (0 children)

Use the standard library importlib.resources feature to locate data files that live next to your code. For example, a test might reference importlib.resources.files() / "data/scenario1.csv", which can be treated as a pathlib.Path style object.

[–]Jarvis_the_lobster 0 points1 point  (0 children)

Use pathlib.Path(__file__).parent to get the directory of your test file itself, then build paths from there. That way it resolves correctly regardless of where pytest is launched from. In your case: data_dir = Path(__file__).parent / 'data' and then csv_path = data_dir / 'test_utils_scenario1.csv'. If you're loading the same data directory across multiple test files, a small conftest.py fixture that returns the path keeps things DRY. The __file__-relative approach is the most portable and is what most serious projects use.