Is migration to pathlib mandatory in newer versions of pytest when using path module?

96 views Asked by At

I have been using the path.py library in my Python projects for path manipulations and recently encountered issues when running tests with the latest versions of pytest.

ERROR test - pytest.PytestRemovedIn8Warning: The (fspath: py.path.local) argument to BlackItem is deprecated. Please use the (path: ...
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!

I understand that pytest has been transitioning from py.path.local to pathlib.Path, but it's unclear whether this transition mandates that all path manipulations in my tests also need to be migrated to pathlib.

My question : is the migration to pathlib in the context of pytest testing environments mandatory, or just recommended?

Updating pytest: I updated pytest to the latest version to ensure that all new features and fixes are applied. I expected that the update might resolve any deprecation warnings related to path handling, but after updating, the warnings remained.

Using pathlib in a test file: I tried replacing instances of path.py with pathlib.Path in one of my test files to see if this would eliminate the warnings. I expected that this change would make the warnings go away and that my tests would still pass. However, while the deprecation warnings were reduced, some tests started failing due to differences in how pathlib and path.py handle path operations. ( which is normal as I did not fully migrate to pathlib)

1

There are 1 answers

1
Tzane On

Judging from the error message, they are aiming to drop path.py parameters in pytest 8 and the deprecations have started in pytest 7. So it's probably a good idea to start the adopting pathlib in your own test suite as well, unless you want to freeze pathlib to a previous version.

In order to support the transition from py.path.local to pathlib, the fspath argument to Node constructors like pytest.Function.from_parent() and pytest.Class.from_parent() is now deprecated.

fspath argument for Node constructors replaced with pathlib.Path

Some related discussion on GitHub: pytest 7: path vs. fspath, pathlib vs. py.path