python-unittest

unittest directory structure – cannot import src code

unittest directory structure – cannot import src code Question: I have the following folder structure in my project: my-project src/ __init__.py script.py test/ __init__.py test_script.py Ideally I want to have a separate folder where all the unit tests go. My test_script.py looks something like this: from src.script import my_object class TestClass(unittest.TestCase): def test_script_object(self): # unit …

Total answers: 1

Python unittest case expected not matching with the actual

Python unittest case expected not matching with the actual Question: I am trying to mock the secrets manager client. Earlier the variables weren’t in the class so I was able to mock the client directly using a patch like below: @patch(‘my_repo.rc.client’) and now since I am using an instance method, I need to mock the …

Total answers: 1

MagicMock Mock'ed Function is not 'called'

MagicMock Mock'ed Function is not 'called' Question: I an trying to mock an api function send_message from stmplib.SMTP in Python using MagicMock. a simplified version of my function looks like #email_sender.py def sendMessage(): msg = EmailMessage() #Skipping code for populating msg with smtplib.SMTP("localhost") as server: server.send_message(msg) I want to mock server.send_message call for my unit …

Total answers: 1

Python unittests used in a project structure with multiple directories

Python unittests used in a project structure with multiple directories Question: I need to use unittest python library to execute tests about the 3 functions in src/arithmetics.py file. Here is my project structure. . ├── src │   └── arithmetics.py └── test └── lcm ├── __init__.py ├── test_lcm_exception.py └── test_lcm.py src/arithmetics.py def lcm(p, q): p, q …

Total answers: 1

Mocking os.environ with python unittests

Mocking os.environ with python unittests Question: I am trying to test a class that handles for me the working directory based on a given parameter. To do so, we are using a class variable to map them. When a specific value is passed, the path is retrieved from the environment variables (See baz in the …

Total answers: 2

Mock class in Python with decorator patch

Mock class in Python with decorator patch Question: I would like to patch a class in Python in unit testing. The main code is this (mymath.py): class MyMath: def my_add(self, a, b): return a + b def add_three_and_two(): my_math = MyMath() return my_math.my_add(3, 2) The test class is this: import unittest from unittest.mock import patch …

Total answers: 2

How can I make python unittest failures not print the entire test?

How can I make python unittest failures not print the entire test? Question: When using my python unit tests, sometimes my tests become somewhat long due to test-specific inputs, or other lengthy logic. If one of my tests fail, unittest will print out the entire test, from the top of the list down to the …

Total answers: 1

Unable to successfully patch functions of Azure ContainerClient

Unable to successfully patch functions of Azure ContainerClient Question: I have been trying to patch the list_blobs() function of ContainerClient, have not been able to do this successfully, this code outputs a MagicMock() function – but the function isn’t patched as I would expect it to be (Trying to patch with a list [‘Blob1’, ‘Blob2’]. …

Total answers: 2

Django unittest function with redirect based on mock return_value

Django unittest function with redirect based on mock return_value Question: I have a view function similar to def my_function(request): session = create_something(‘some_random_string’) return redirect(session.url, code=303) To test it import unittest from django.test import TestCase from unittest.mock import patch from my_app.views import my_function class TestMyFunction(TestCase): @patch(‘my_app.views.create_something’, return_value={ "url": "https://tiagoperes.eu/" }) def test_my_function(self, mock_create_something): response = self.client.get("/my-function/") …

Total answers: 1