On 14.09.2017 15:49, Paul Hammant wrote: > Would I retain the previous value of os.system and set it back after > any assertions (pass or fail) so that following tests would not affected? > Or does it und itself naturally when Pytest recurses in the next test > in its suite ?
It wouldn't undo itself unless PyTest runs a separate instance of the interpreter for each test case, which I don't believe it does. The best way for restoring the original function in the module would be by using an appropriately crafted context manager in the scope of your test case. Either that, or add the mock/revert to the per-testcase setup and teardown methods. > As I hinted I couldn't find such an example online, with an emphatic > 'best practice' claim attached. There's no reason to expect to find any kind of best practice for anything online. :) In the case of Python, the semantics of how module imports work is the clue to the obvious way to mock functions (and classes, etc) from modules you don't otherwise control. > What do you intend to convey with "Well well well"? That I just made an emphatic best practice available online. :D -- Brane