Normalizing path strings and separators in cross-platform unit test scripts

Malcolm Greene python at bdurham.com
Mon Jan 7 13:51:30 EST 2019


Any recommendations on normalizing path strings in cross platform
(Windows, Linux, macOS) for unit tests?
Our goal is to normalize path strings to use forward slash separators so
that we can consistently reference path strings in our unit tests in a
cross platform way.
Example: Under Windows we have two paths that are logically the same but fail to match for test purposes.
assert str(full_path(f'{test_folder_path}/readonly.txt')) == 'C:/udp-app-
master/dev/tmp/readonly.txt'E       AssertionError: assert 'C:\\udp-app-...\readonly.txt' == 'C:/udp-app-
ma.../readonly.txt'
Is there a best practice way to convert Windows style paths (with
backslash path separators) to Linux style paths with forward slash path
separators? I've looked at the os and pathlib libraries without seeing
anything that describes our need.
Any downsides to this approach? 

Thank you



More information about the Python-list mailing list