Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
1
vote
1 answer

Applying moto on a classmethod fails

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: …
Vincent Claes
  • 3,960
  • 3
  • 44
  • 62
1
vote
1 answer

assert_called_once_with() with datetime.now() in the call parameter?

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':…
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
1
vote
1 answer

assertRaises(AttributeError, ...) not working. Module 'myModule' has no attribute 'main' (Python Unit Test)

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 =…
user3646958
1
vote
1 answer

Python: is it possible to wrap "@patch(path)" for re-use? (unittest)

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…
Manu Artero
  • 9,238
  • 6
  • 58
  • 73
1
vote
0 answers

Unused import gives me access to the stdout of a signal

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…
1
vote
2 answers

Replacing python with_statement by setUp and tearDown in Unittest

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: …
kriss
  • 23,497
  • 17
  • 97
  • 116
1
vote
0 answers

Python Project Structuring with unittest

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 …
rohan
  • 527
  • 1
  • 6
  • 19
1
vote
1 answer

How do I get the name of a child Python class when a mocked parent class method is called?

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…
1
vote
1 answer

python @patch custom patched object not updating

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…
maneet
  • 295
  • 2
  • 10
1
vote
0 answers

How to optionally skip long tests in Python Unittest?

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…
krzysztofs
  • 33
  • 5
1
vote
3 answers

Can I mock a function return that is called within another function call in a Python Test?

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…
1
vote
1 answer

Accessing values from inner function in pytest fixture

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…
user2242044
  • 8,803
  • 25
  • 97
  • 164
1
vote
1 answer

Parameterized Fixture Inside unittest.TestCase class

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…
user2242044
  • 8,803
  • 25
  • 97
  • 164
1
vote
1 answer

How do I test a method (using unittest) that closes a PIL image?

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…
1
vote
1 answer

How to specify specific instance of exception when using `assertRaises`?

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…
Mario Ishac
  • 5,060
  • 3
  • 21
  • 52