6 ms·
I've been looking for something like this for ages. I'm excited to try some of this stuff out, like spec'd Mocks. I'm curious if anyone else who has been drawn
by tcbasche 6y ago
I've been looking for something like this for ages. I'm excited to try some of this stuff out, like spec'd Mocks.
I'm curious if anyone else who has been drawn in by the allure of the Mock has some strategies to avoid the footguns associated with them? (Python specifically)
- animal_spirits 6y agoWhat footguns are you looking to avoid with mocks? I've been using them for about a year now and haven't run into much issues.
- AlexCoventry 6y agoIf the mock's model of the mocked-out component is inaccurate, it reduces the relevance of the test using the mock.
- deleted 6y ago[deleted]
- nerdponx 6y agoCheck out the talk by Edwin Jung, "Mocking and Patching pitfalls": https://www.youtube.com/watch?v=Ldlz4V-UCFw https://www.youtube.com/watch?v=Ldlz4V-UCFw
- UK-Al05 6y agoI've seen things like the mock returns null for an error. But the real thing throws an exception.
- globular-toast 6y agoJust don't use them unless you have to. The two main reasons to mock are for network requests and because something is too slow. Other than that, test things for real. Do not isolate parts of your code from other parts of your code by using mocks. If your code does side effects on your own machine, like writes to the file system, let it write to a temporary directory. I linked this excellent talk in another thread recently. I'll put it here again: https://www.youtube.com/watch?v=EZ05e7EMOLM https://www.youtube.com/watch?v=EZ05e7EMOLM
- scrollaway 6y agoIndeed. And if you're mocking eg. API calls in a client library, try to have tests for the real things as well. They don't have to be part of your normal test suite, they can run only if env vars are set with the API keys needed.
- tmarice 6y agoVCR.py (https://github.com/kevin1024/vcrpy https://github.com/kevin1024/vcrpy) is a great utility for mocking APIs. It will run each request once, save the responses to YAML files, and then replay the responses every time you re-run the tests. It's also very useful for caching API responses (e.g. you have a trial account with limited number of requests). Unfortunately, if used for testing, it will not cover the case when the original API changes its interface.
- mumblemumble 6y agoThat Ian Cooper talk is just fantastic. It's perhaps best contribution to the subject of TDD anyone has produced since Kent Beck popularized the idea in the first place.
- travisjungroth 6y agoI'd really recommend this video. I had seen it years ago and just came back to it. It's about changing your architecture, one of the effects of that being changing how you need/use mock. https://www.youtube.com/watch?v=DJtef410XaM&t https://www.youtube.com/watch?v=DJtef410XaM&t
- f00_ 6y agoassert on the call_count attribute of a mock instead of trying to use methods on it like .assert_called_once_with() "a mock’s job is to say, “You got it, boss” whenever anyone calls it. It will do real work, like raising an exception, when one of its convenience methods is called, like assert_called_once_with. But it won’t do real work when you call a method that only resembles a convenience method, such as assert_called_once (no _with!)." https://engineeringblog.yelp.com/2015/02/assert_called_once-threat-or-menace.html https://engineeringblog.yelp.com/2015/02/assert_called_once-...
- alasdairnicol 6y agoThis behaviour has changed in Python 3.5 [1], and it was also backported to the mock package. When unsafe=False (the default), accessing an attribute that begins with assert will raise an error. [1]: https://docs.python.org/3/library/unittest.mock.html#the-mock-class https://docs.python.org/3/library/unittest.mock.html#the-moc...
- f00_ 6y agodo you happen to know if this is true on mock==2.0?
- alasdairnicol 6y agoYes it looks as if that functionality is in 2.0, but the list of typos isn’t as extensive as in later versions. https://mock.readthedocs.io/en/latest/changelog.html#and-earlier https://mock.readthedocs.io/en/latest/changelog.html#and-ear...
- progval 6y ago> accessing an attribute that begins with assert Or any of the names in this surprising hardcoded list of typos https://github.com/python/cpython/blob/fdb9efce6ac211f973088eef508740c3fa2bd182/Lib/unittest/mock.py#L634 https://github.com/python/cpython/blob/fdb9efce6ac211f973088...