fixed-path
A test reads or writes a fixed path.
- Group
- Tests
- Runs
- On by default
- Level
- note
- Reads
- Tests
- Fix
- None: where the code takes its path from is the author's to say
- Defends
- Resilience: the test fails only when the code is broken.
Why
The test gives a call that reads or writes a file an absolute path written in the test, which another test, another run, or another machine may have written.
The fix, as Ur suggests it: use a file under the test's own temporary directory.
For agents
Give each test files of its own under the framework's temporary directory; never read or write a fixed absolute path.
Example
shop/export.pydef export(orders, path):
with open(path, "w") as out:
out.write("\n".join(orders))
tests/test_export.pyfrom shop.export import export
def test_export_writes_one_order_a_line():
export(["kettle", "teapot"], "/tmp/orders.txt")
with open("/tmp/orders.txt") as written:
assert written.read() == "kettle\nteapot"
$ ur lint --rule fixed-path
note[fixed-path]: test `test_export_writes_one_order_a_line` uses the fixed path `/tmp/orders.txt`, which another test or an earlier run may have written
--> ./tests/test_export.py:6:10 test `test_export_writes_one_order_a_line`
= hint: use a file under the test's own temporary directory
= see: TotT: Avoiding Flakey Tests, Google Testing Blog (2008-04-17), https://testing.googleblog.com/2008/04/tott-avoiding-flakey-tests.html
0 clone classes and 1 test finding in 2 files
Source
TotT: Avoiding Flakey Tests, Google Testing Blog (2008-04-17).