I have a problem in writing integration test for some function that calls other function which is decorated. Suppose the following definitions: I need to write test to ensure that any exception raised inside problematic_func will not be propagated to func_under_test. For this reason, I used mocking like below: Problem is that I can’t pass this test. Patching problematic_func caused
Tag: unit-testing
Twisted unittest “Reactor Unclean” when using TimeoutMixin
I’m implementing a protocol which may be instantiated with different timeouts, so I use TimeoutMixin. The idea can be represented with this dummy class: my_protocol.py To test it with the Twisted Trial a made this unittest roughly following the official tutorial: test_my_protocol.py I get the following error when running the test: This is what official docs says on the topic:
AssertEqual failing when comparing two seemingly identical IndexErrors
I have a DynamicArray class shown below. (I have only included relevant methods. The rest can be viewed from https://www.geeksforgeeks.org/implementation-of-dynamic-array-in-python/) Then I have another unit test file down below When I run the test I expect self.a.__getitem__(0) to throw IndexError(‘0 is out of bounds’) and I can’t see why the assertion fails? The only difference is that self.a.__getitem__(0) will yield
Mocking FastText model for utest
I am using fasttext models in my python library (from the official fasttext library). To run my u-tests, I need at some point a model (fasttext.FastText._FastText object), as light as possible so that I can version it in my repo. I have tried to create a fake text dataset with 5 lines “fake.txt” and a few words and called It
How to generate a test report in python using selenium and unittest?
I was trying to test a simple login form with different cases. I used python,selenium,python’s unittest library for testing and i am able to get test done by unittest library but how can i generate test reports for this ? I tried with HTMLTestRunner but it has bugs and i found it was written for python 2x version. A lot
mocking/patching the value of a computed attribute from a classmethod
I am attempting to write a test for a function which calls an object’s classmethod — this classmethod goes on to return a new instance of that class. There are plenty of examples of patching class attributes both here on stackoverflow and elsewhere but I am having difficulty understanding how to patch attribute/value such that I can test my function.
Mock property method on class
I have the following dataclass: I’d like to change the behavior of qux during testing. I’m aware of PropertyMock, and could write something like: I’d instead like to substitute the property method (undecorated), something like: I’ve tried various combinations of new_callable, new, etc. when creating the patch object, but I’m seeing: Is there a way I can mock out a
Querying the database in a Django unit test
I am creating a web application which has a POST endpoint, that does two things: Saves the POSTed data (a university review) in the database. Redirects the user to an overview page. Here is the code for it: I haven’t yet implemented passing the user data to the endpoint, and that’s why I’m saving everything under the user with pk=1.
Run / Debug a Django application’s UnitTests from the mouse right click context menu in PyCharm Community Edition?
I must emphasize on PyCharm Community Edition which does not have any Django integration (v2016.3.2 at question time). I’ve Googled my problem and (surprisingly,) I did not get any answers, (of course I don’t exclude the possibility that there might be some, be but I just missed them). The question is simple: in PyCharm, one can Run (Debug) an Unit
How to patch a constant in Python using a mock as function parameter
I’m trying to understand the different ways to patch a constant in Python using mock.patch. My goal is to be able to use a variable defined in my Test class as the patching value for my constant. I’ve found this question which explains how to patch a constant: How to patch a constant in python And this question which explains