Test the guarantees of a change tool
Write tests that catch unwanted writes, lost settings and misleading change results.
Test effects, not just return values
A configuration function can return the expected value while still damaging a file. A useful test inspects both. For a preview, save the exact file bytes before the call and compare them afterwards. For an application, parse the resulting JSON and check that requested values changed while unrelated values survived.
The supplied config_writer.update(path, changes, apply=False) returns whether a change is needed. A preview can return True without applying anything. An unchanged request returns False, and an applied change returns True. This differs from the earlier plan object's applied field, so read the contract before writing assertions.
Give each test its own workspace
Use tempfile.TemporaryDirectory inside each test. The context manager creates a fresh directory and removes it afterwards, preventing one test's file changes from determining another test's result. Use Path objects to construct paths and explicit fixture contents to describe the starting state.
Check the no-op case as well. A tool that always says it changed something can trigger unnecessary restarts or make reports noisy even if its file contents look correct.
The automatic checks first run your suite against the correct implementation, then try three deliberately broken versions. Your tests must fail with assertions for each broken behavior. This checks whether your suite can detect a regression, not whether it resembles the explained solution. In the main guard, explicitly load your TestCase with unittest.defaultTestLoader.loadTestsFromTestCase(ChangeTests) and pass that suite to unittest.TextTestRunner().run. Explicit loading works in the browser execution namespace as well as in a standalone script.
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as root:
path = Path(root) / "settings.json"
path.write_text('{"workers":2}')
print(path.read_text())
Put it into practice.
Write unittest.TestCase tests for the supplied config_writer.update(path,changes,apply=False). It returns True when values differ, even in preview; False for a no-op. Preview must leave exact bytes unchanged. apply=True merges requested settings and preserves unrelated keys. A no-op must not rewrite the file. Use temporary directories and a main guard that explicitly loads your TestCase with unittest.defaultTestLoader.loadTestsFromTestCase, then runs it with unittest.TextTestRunner. Your suite must pass the supplied implementation and fail assertions for three mutations: preview writes, application drops unrelated keys, and no-op returns True. Do not replace or patch the implementation in your tests.
See an explained solution
import unittest, tempfile, json
from pathlib import Path
from config_writer import update
class ChangeTests(unittest.TestCase):
def test_preview(self):
with tempfile.TemporaryDirectory() as root:
path = Path(root) / "config.json"
path.write_text('{ "workers": 2, "owner": "Operations" }')
before = path.read_bytes()
self.assertTrue(update(path, {"workers": 4}))
self.assertEqual(path.read_bytes(), before)
def test_apply_preserves_unrelated(self):
with tempfile.TemporaryDirectory() as root:
path = Path(root) / "config.json"
path.write_text('{"workers":2,"owner":"Operations"}')
self.assertTrue(update(path, {"workers":4}, apply=True))
self.assertEqual(json.loads(path.read_text()), {"workers":4,"owner":"Operations"})
def test_no_change(self):
with tempfile.TemporaryDirectory() as root:
path = Path(root) / "config.json"
path.write_text('{ "workers": 2 }')
before = path.read_bytes()
self.assertFalse(update(path, {"workers":2}, apply=True))
self.assertEqual(path.read_bytes(), before)
if __name__ == "__main__":
suite = unittest.defaultTestLoader.loadTestsFromTestCase(ChangeTests)
unittest.TextTestRunner(verbosity=2).run(suite)
The suite checks the public promise of each mode, including filesystem effects. Mutation checks demonstrate that the assertions reject realistic regressions.