I am using mock for testing in Python. I am trying to unit test a metaclass which overwrites the __new__ method and then calls type.__new__(cls) internally.
I don't want to actually call type.__new__, so I want to mock out type. Of course, I can't patch __builtin__.type because it breaks object construction within the test.
So, I really want to limit mocking type within the module under test. Is this possible?
Yes. You patch as close to where you're going to call your function as possible for just these sort of reasons. So, in your test case, only around the function (or whatever callable) you'll call that's under tests, you can patch type.
The documentation for patch has plenty of examples for doing this if you'd like to peruse them.
Cheers.
Related
I need to cover only code that is called directly from test function, every nested method call must be marked as missed. This must help me ensure that every unit/method has his own test.
Example: test function calls method A and method A calls method B inside. After that I want to have A method marked as covered and method B marked as missed, as it was not directly called from test function.
Does anybody know plugin or has any idea how to do that?
I have tried googling and reading coverage docs, the only thing slightly related is dynamic contexts, but they show which methods called the line. This differs from what I want, because in this case I must check every line caller method. I just want this lines(that are not called directly) to be marked red.
Coverage.py doesn't have a way to do this. There's discussion about marking which product functions are intended to be covered by each test as a possible future feature: https://github.com/nedbat/coveragepy/issues/696
I'm a bit new to Python's unittest library and I'm currently looking at setting up my Flask server before running any of my integration tests. I know that the unittest.TestCase class allows you to use setUp() before every test cases in the class. I also know that the same class has another method called setUpClass() that runs only once for the entire class.
What I'm actually interested is trying to figure out how to do something similar like setUpClass(), but done on an entire unittest.TestSuite. However, I'm having no luck at it.
Sure, I could set up the server for every TestCase, but I would like to avoid doing this.
There is an answer on a separate question that suggests that by overriding unittest.TestResult's startTestRun(), you could have a set up function that covers the entire test suite. However, I've tried to passed in the custom TestResult object into unittest. TextTestRunner with no success.
So, how exactly can I do a set up for an entire test suite?
This is not well documented, but I recently needed to do this as well.
The docs mention that TestResult.startTestRun is "Called once before any tests are executed."
As you can see, in the implementation, the method doesn't do anything.
I tried subclassing TestResult and all kinds of things. I couldn't make any of that work, so I ended up monkey patching the class.
In the __init__.py of my test package, I did the following:
import unittest
OLD_TEST_RUN = unittest.result.TestResult.startTestRun
def startTestRun(self):
# whatever custom code you want to run exactly once before
# any of your tests runs goes here:
...
# just in case future versions do something in this method
# we'll call the existing method
OLD_TEST_RUN(self)
unittest.result.TestResult.startTestRun = startTestRun
There is also a stopTestRun method if you need to run cleanup code after all tests have run.
Note that this does not make a separate version of TestResult. The existing one is used by the unittest module as usual. The only thing we've done is surgically graft on our custom implementation of startTestRun
So, the main question is on the title. I'm trying to find out if it is correct to say that python mock module uses dependency injection pattern for mocking object calls.
I'm not familiar with DI pattern but from what I've read and what I saw with debugger in mock module underhood look like mock() uses DI.
Am I right and mock() is DI or I'm missing something about this pattern and mocking?
The mock module does not use dependency injection.
mock replaces some objects with customized ones.
For dependency injection to be used, there would have to be some top level process, which would search for functions to be called, detected arguments to pass to them, instantiate them and finally made the call.
mock only modifies the object and the call to this objects is done as usual - direct call to that object.
If you want to find example of dependency injection, check pytest and it's fixtures - they use it a lot and it is fun to use that.
I suppose you are primarily concerned with mocking out an object's attributes with unittest.mock.patch.
patch is a function which does little more than return an instance of the class unittest.mock._patch. _patch is a context manager, which monkeypatches an attribute upon __enter__ and unpatches it upon __exit__.
What is the easiest way to record function calls for debugging in Python? I'm usually interested in particular functions or all functions from a given class. Or sometimes even all functions called on a particular object attribute. Seeing the call arguments would be useful, too.
I can imagine writing decorators for all that, but then I'd still have to modify the source code in different places. And writing a class decorator which modifies all methods isn't that straightforward.
Is there a solution where I don't have to modify my source code? Ideally something which doesn't slow down Python too much.
You ought to be able to implement something that does what you want using either sys.setprofile() or perhaps sys.settrace(). They both let you define a function to be called when specific "events" occur, like function calls, and pass additional information to which can be used to to determine the function/method being called and examine its arguments.
If you look around, there's probably sample usage code to use as a good starting point.
Except decorators, for Python >= 3.0 you could use new __getattribute__ method for a class, which will be called every time you call any method of the object.
You could look through Lutz "Learning Python" chapters 31, 37 about it.
Mox mocking library allows you to be either specific or agnostic about the class you are mocking.
mock = mox.CreateMock(Foo) or
mock = mox.CreateMockAnything()
Mox documentation suggests to use the first way (basically check the type of the mock) wherever it is possible. Python as a dynamic language is type agnostic. The two approaches look inconsistent to me.
So, which approach to mocking is more Pythonic?
They are not the same. From the documentation:
Some classes do not provide public interfaces; for example, they might
use __getattribute__ to dynamically create their interface. For these
classes, you can use a MockAnything. It does not enforce any
interface, so any call your heart desires is valid. It works in the
same record-replay-verify paradigm. Don't use this unless you
absolutely have to! You can create a MockAnything with the
CreateMockAnything method of your Mox instance like so:
In contrast, when creating a mock using CreateMock(Foo), you get an exception when an unknown method is called.