Category : python-unittest

I’m trying to log within my unit tests, but I’m not seeing any output. I looked at https://docs.pytest.org/en/6.2.x/logging.html and it seems like it should work with my setup, but it’s not, so I’m a bit confused. Here’s the pytest console output [email protected]:/opt/app$ pytest tests/x.py /usr/local/lib/python3.7/site-packages/flask_restplus/fields.py:17: DeprecationWarning: The import ‘werkzeug.cache d_property’ is deprecated and will be ..

Read more

I’m having trouble while dealing with patching. I’m using mock from unittest library. While testing check_codes() view I would like to set another values to db.find_one() api.utils.py from pymongo import MongoClient import os def get_share_code_collection(): client = MongoClient(os.getenv("DB_HOST")) db_handle = client[os.getenv("DB_NAME")] return db_handle["share_codes"] views.py def check_codes(self, request): db = get_share_code_collection() data = db.find_one({"specialist_id": {"$exists": True}}) ..

Read more

A_script.py from uuid import uuid4 def get_unique_identifier(env, customer_id): return env + ‘-‘ + customer_id + ‘-‘ + str(uuid4())[0:8] test_A_script.py import unittest from unittest.mock import patch import src.A_script as a_script class MyTestCase(unittest.TestCase): @patch(‘uuid.uuid4’) def test_get_unique_identifier(self, mock_uuid4): mock_uuid4.return_value = ‘abcd1234’ expected = ‘test_env-test_cust-abcd1234’ unique_identifier = a_script.get_unique_identifier(‘test_env’, ‘test_cust’) self.assertEqual(expected, unique_identifier) How can I make uuid4 return ‘abcd1234’? Source: ..

Read more

I have a case similar to the following code where I am trying to patch a fucntion that is imported using the from statement: from module1 import function1 def function2(): function1_result = function1() return 2*function1_result and then the testing code is: from unittest import patch def test_function2(): func1_value = 5 with patch("module1.function1", return_value=func1_value) as patched_func1: ..

Read more

I have the following function I want to test: def rmdir_and_files_with_exceptions(folder: str, exceptions: "list[str]") -> None: """[removes subfolders and files of a given folder with a list as parameter to prevent deletion] Args: folder (str): [folder where subfolders should be removed in] exceptions (list[str]): [list with folders NOT to delete] """ for dir_or_file in os.listdir(folder): ..

Read more

I want to test that only valid files are added to a count of valid files, like so: self.n_valid_files = 0 for file in self.list_of_files: n_paras = self.count_paras(file) if n_paras != None: self.n_valid_files += 1 where count_paras returns None if something goes wrong. Is there a way to patch-and-test that: i.e. supply a multiple given ..

Read more