Steven D'Aprano wrote: > Wait... are you saying that importing test_mymodule monkey-patches the > current library? And doesn't un-patch it afterwards? That's horrible.
There's something in the library, unittest.mock that makes this relatively safe -- if not painless with mock.patch("random.randint", side_effect=[42]) as randint: self.assertEqual(my_module.my_thing(), 42) randint.assert_called_once_with(1, 6) and sometimes monkey-patching may be a necessary evil to verify that a portion of the code that is buried a bit too deep is called as expected. However, in this case it tests that the code is what it is rather than what it does. Good tests would allow for replacing random.randint() with random.randrange() or random.SystemRandom().randrange() and still succeed. -- https://mail.python.org/mailman/listinfo/python-list