Applying a moto mock to the test class as a whole will have no effect on classmethods like the python unittests setupClass method.
@mock_ssm
class SomeClassTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
…
I have the following test code to test Decorator.
@mock.patch('a.b.c.KafkaProducer')
def test_1(self, mocked):
decorator = Decorator(....)
@decorator()
def test():
return 42
test()
start_time = ???
v = {'type':…
I'm new to Python. My first unit test doesn't work.
Here is my telegram.py:
#!/usr/bin/python3
import socket
import sys
import urllib.parse
import certifi
import pycurl
from io import BytesIO
# telegram API key and chat id
TELEGRAM_API_KEY =…
As the doc "Where to patch" says, we need to patch where an object is looked up, (not where it's defined); so I understand that it's not possible to - let's say - create a reusable patch for a particular path
Imagine you have several modules…
I just ran into a strange interaction and I'm curious if one of you could explain what's happening:
I'm writing a test, and to start I just created a simple print statement to check if the signal was going off. Here's the test located in…
In a test suite I have some code organized as below, context is some persistent object that is deleted when exiting the with block:
class Test(TestCase):
def test_action1(self):
with create_context() as context:
…
I'm practicing TDD in python and currently, my project has the following structure:
App
A_folder
init__.py
core.py
rand.csv
B_folder
test
__init__.py
test_A_folder
__init__.py
…
I have a tonne of dialogs and wizards in a large QT/Python app but can't determine which child class triggers the parent's exec_ method. Is there anyway to do this using mock, or any other library? I can find it via debug of course but I want a…
I want to patch a function(func_to_be_mocked ) returning class object (ReturnValue) with my own mocked version (ReturnValueMock). The function I want to test(func_to_be_tested) is setting some values on the object. But my mocked object is not…
Some of my tests are taking long time, and I want to have an option to run only short tests or all tests. I have found option to disable some tests (Disable individual Python unit tests temporarily), but I didn't find option to, for example, set…
Is there a way to mock a function return that is called within another function call? For example:
def bar():
return "baz"
def foo():
return bar()
class Tests(unittest.TestCase):
def test_code(self):
# hijack bar() here to…
I am trying to access the response from an inner function inside my pytest fixture. I'm not sure if this is a python issue or something unique to how pytest is built. The snippet below is a dumb example, but it demonstrates the issue. I am getting…
I am using pytest to and would like to use a parameterized pytest fixture. If I don't use a class and run pytest, both tests pass, but when inside a test class, the tests both fail, but I cannot figure out what is wrong.
Working Code:
import…
I have a method (close()) that's part of my class (Img) that goes like this:
def close(self):
Image.Image.close(self.image)
I've been struggling to find a way to test this method with the unittest package because it doesn't return…
I have a custom exception:
class AError(Exception):
def __init__(self, a):
self.a = a
and a function that raises this exception:
def raise_a(self):
raise AError(1)
Using unittest, how do I test that raise_a raises AError with an a…