unit-testing

Assertion Error: Custom Exception not raised

Assertion Error: Custom Exception not raised Question: I am trying to write unittests for a function I wrote that includes a try/except block. I have raised a custom exception if my string contains less than 3 characters. I wanted to test that the error gets raised when inputting a string less than 3 characters long. …

Total answers: 2

Accessing Calls to Mocked Class functions

Accessing Calls to Mocked Class functions Question: I have written a custom class to mock a general API client in a codebase so that I can centrally and easily mock all of the class methods for unit testing. This is working great so far, however I am looking for a way to track individual calls …

Total answers: 1

How to make custom Hypothesis strategy to supply custom objects?

How to make custom Hypothesis strategy to supply custom objects? Question: Suppose I have a class Thing class Thing: def __init__(self, x, y): … And suppose I have a function which acts on a list of things. def do_stuff(list_of_things): … I would like to write unit tests for do_stuff involving different instances of lists of …

Total answers: 2

Timeout error on selecting the first item in unittest Python

Timeout error on selecting the first item in unittest Python Question: I’m trying to create a unittest using Selenium Webdriver on Python. This test aims to search for a given item, search the first result, add it to the cart and check that there is one product. However, I cannot get this done. Below is …

Total answers: 1

python unittest: relative paths to files – pycharm vs cli

python unittest: relative paths to files – pycharm vs cli Question: I have unittests that require access to files. this is due to the nature of the project that generates files as output and I want to compare to the expected output. Currently my directory structure is: project root/tests/files In the test setup I have …

Total answers: 1

Mock Python-On-Whales

Mock Python-On-Whales Question: With a Pyhton script I want to determine, if a docker container is running. For this, I use python-on-whales. Something this way: check_container.py from python_on_whales import docker class Container: def __init__(self, name): self.name = name def is_running(self): container_found = self.search_container() if container_found: for container in self.get_container_state(): if container.state.status == "running": return True …

Total answers: 1

Test for instantiation of a mocked class in Python?

Test for instantiation of a mocked class in Python? Question: I want to make my tests "real" unittests in a strict meaning. There shouldn’t be any dependencies. All dependencies should be mocked. The class Bar in module bar need to be tested. But in some situations it will have a member of type Foo from …

Total answers: 1

Python unittest that at least one exception is raised

Python unittest that at least one exception is raised Question: Is there a way to get unittest standard library to check for multiple exceptions? Obviously assertRaises works for a single exception: How do you test that a Python function throws an exception? But I want to test whether at least one error is raised. This …

Total answers: 1

Python mock function that returns dictionary multiple times

Python mock function that returns dictionary multiple times Question: Background I have a function my_func that gets a dictionary from another function get_dict and modifies it. On failure, my_func gets retried until it succeeds or called a specified number of times. What complicates this is that the dictionary must have exactly one key-value pair, otherwise …

Total answers: 1