I have a python function that creates a Client object from the aiosnow module, which is an API wrapper for ServiceNow like this: Assuming I will not have internet connection during unit testing, how or what part am I supposed to make the unit test for, which part am I supposed to mock? Answer One approach is just to guarantee
Tag: unit-testing
Testing for None on a non Optional input parameter
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
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:
Parametrizing fixtures and functions without combinations
Here’s the thing: I have an Array2D class that receives parameters ‘shape’ and ‘val’. The constructor is as follows: I would like to perform tests on this class. To do this I have declared a variable ARRAY_CONFIG, which lists parameters for different two-dimensional arrays. Here is an example: I have also defined other lists that store the expected values for
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
Mock.patch returning MagicMock object causing AssertionError?
I have a function that I am trying to test in querySomething.py: And the test file: I am trying to mock the httpClient.get_request so that it gets the JSON file instead of reaching out to the API. We want to test an unauthorized response and a success response which explains the mock_response function. However, when I run the test, I
Load existing data catalog programmatically
I want to write pytest unit test in Kedro 0.17.5. They need to perform integrity checks on dataframes created by the pipeline. These dataframes are specified in the catalog.yml and already persisted successfully using kedro run. The catalog.yml is in conf/base. I have a test module test_my_dataframe.py in src/tests/pipelines/my_pipeline/. How can I load the data catalog based on my catalog.yml
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
Mocking sqlobject function call for test db
I am trying to mock sqlbuilder.func for test cases with pytest I successfully mocked sqlbuilder.func.TO_BASE64 with correct output but when I tried mocking sqlbuilder.func.FROM_UNIXTIME I didn’t get any error but the resulted output is incorrect with the generated query. Below is the minimal working example of the problem. models.py conftest.py test_ex1.py Current SQL: Expected SQL: Edit Example Answer By default,