Let’s say I have a python module with the following function: For that function, I have the unit test that follows: Given the type hints in the function, the input parameter should always be a defined string. But type hints are… hints. Nothing prevents the user from passing whatever it wants, and None in particular is a quite common option
Tag: python-unittest
How to unittest a datetime function
file.py test.py I get an Error: TypeError: ‘>’ not supported between instances of ‘MagicMock’ and ‘int’ Is there anyway to test this function? Probably the problem is with this part of the last line: (utc_date – namespace_created_date).days Answer I could reproduce and fix. Here is a working unittest file (assuming file.py is accessible): If I use date_now.datetime.utcnow.date.return_value = datetime.datetime(2022,1,1) I
Test (unittest, mock) if a global variable list is updated
file.py: test.py: I always get an AssertionError. from the line assert x in GLOB_VAR Let me say that I DO need a global variable Answer It turned out that I shouldn’t have patched the global variable as I need to assert against the file’s global variable and not against the a mock’s instance global variable. This does does the trick:
Mock return value of a function for one case input, otherwise return original value
apps.my_module.py I want to mock my_func in tests.py the mocked function return value for a is name is AA not name is a, but the return value for b and else must stay the same I want to do this using unittest, how to do this? Answer The solution goes as the following: in tests.py
Running unittest with modules that must import other modules
Our Python 3.10 unit tests are breaking when the modules being tested need to import other modules. When we use the packaging techniques recommended by other posts and articles, either the unit tests fail to import modules, or the direct calls to run the app fail to import modules. The other posts and articles we have read do not show
unit test of function in different directory gives AttributeError: module has no attribute
What specific syntax must be changed below in order for a unit test running in one part of a file system to successfully test a function in a class that is located in a completely different part of a file system? The test file is located at C:pathtosome-test-classestest_an_example.py The class being tested is located at C:\completely\different\path\an_example.py The problem might be
Python unittest AssertionError: 0 != []
I’m new to Python unittest and I’m trying to access this list: to do this test: I’ve created this function: But I keep getting this error: AssertionError: 0 != [] Can someone help explain what I’m doing wrong in the function please? Answer Let’s take a look at this part, the get_customer_pet_count function: First, you’re not passing it a “customer
Using unittest.mock to mock a DRF response
My example is pretty basic: What I’m trying to do is mock the request to the /core/ URI so it returns a mocked response and not the response from the database. For example, considering unit testing in a CI pipeline when the database isn’t available. The below is what I have, but print(response.data) returns the actual response and not the
TypeError: object is not callable when instantiating during unit test
I have a class that throws an exception when instantiated with wrong values and I would like to write a unit test raising an exception when given wrong parameters. Instantiating an object outside of the self.assertRaises() doesn’t seem to cause any error and I fail to understand why it causes an error when I try to instantiate it within the
Python Unittest Allow Multiple Possible Return Values
I’m writing a unittest that will be run on my class’s code. For one of the functions they must write, there are two possible return values that they could return and either one is okay for my purposes. I’ve been using But this doesn’t work for accepting one of two valid return values, so I’ve changed it to: Is there