Category : pytest-mock

I’m trying to mock a class method with pytest-mock. I have the code below in a single file, and when the test is run I get ModuleNotFoundError: No module named ‘RealClass’ in the patch function. How to make this work? class RealClass: def some_function(): return ‘real’ def function_to_test(): x = RealClass() return x.some_function() def test_the_function(mocker): ..

Read more

I’m having a real hard time understanding how to mock a class that has a property and setter fixtures that access a "private" attribute. import pytest from pytest_mock import MockFixture from unittest.mock import MagicMock from typing import List class MyEntity: _my_list: List[str] = [] def __init__(self, list_vals = []): self._my_list = list_vals @property def my_list(self) ..

Read more

I am using python 3.8.1 with pytest My project structure is metadb.py # main is here lib MetaQ.py __init__.py tests test_meta_q.py __init__.py My code looks like this class MetaQ: message = None database = None def __init__(self, message): self.message = message self.parse_connection_info() def parse_connection_info(self): # do some stuff self.database = … def get_info(self): x = ..

Read more

I have a method with an external API call. The external API call that takes two parameters (a XML string and number) and if it is successful it creates something in the external system. def create_stuff(my_number: int,name: str): try: new_value = my_number + 5 stuff_xml = ”'<?xml version=’1.0′ encoding=’UTF-8′?> <description>”’ + name + ”'</description> <properties/>”’ ..

Read more

In the below files I have InternalDogWebhookResource which calls VisitOrchestrator.fetch_visit. I am attempting to write a test for InternalDogWebhookResource but mock VisitOrchestrator.fetch_visit since it is a network call. I have tried the mock paths: api.dog.handlers.internal.VisitOrchestrator.fetch_visit api.dog.handlers.internal.InternalDogWebhookResource.VisitOrchestrator.fetch_visit api.dog.handlers.internal.InternalDogWebhookResource.fetch_visit and many others, but I am always getting AssertionError: assert None I can confirm that the client.post in ..

Read more

This is a very stripped back version of what I want to do. It may not seem totally useful, but that is because I have stripped away all elements not pertinent to this question; dummy.py from another_mod import some_val class Dummy: A_DICT = { ‘a’: some_val*5, ‘b’: some_val*10, } def some_func(self, x): return x*Dummy.A_DICT[‘a’] For ..

Read more

I have a sample class which reads a saved Tensorflow model and runs predictions class Sample(): ## all it does is creates a new column with predictions def __init__(self, tf_model): self.tf_model = tf_model def tf_process(self, x): ##some other preprocessing x["tf_predictions"] = self.tf_model.predict(x) return x def predict(self, x): predictions = self.tf_process(x) return predictions Code for Unittesting ..

Read more

I want to write pytest cases for mysql python function by using mocker databases for below enter code here code can you help me to write pytest enter code here " def check_for_db(): db_name = os.environ.get(‘db_name’) mysql_hostname = os.environ.get(‘mysql_hostname’) mysql_username = os.environ.get(‘mysql_username’) mysql_pw =os.environ.get(‘mysql_pw’) conn = mysql.connector.connect(host=mysql_hostname,user=mysql_username,password=mysql_pw) cur = conn.cursor(buffered=True) sql_query = ‘SHOW DATABASES;’ cur.execute(sql_query) ..

Read more