unit-testing

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

How to mock stdin when using fileinput module?

How to mock stdin when using fileinput module? Question: I have a program that uses the Python fileinput module, and I am trying to write unittests for the main() function. They work find when using an actual file, but raise OSError: reading from stdin while output is captured when I try to pass data via …

Total answers: 2

assertEqual vs assertSetEqulal in unittest

assertEqual vs assertSetEqulal in unittest Question: Is there a difference between assertEquals and assertSetEqual in the python unittest.TestCase for assertion of sets or frozensets? And if there is not, why are there assertSetEqual? also for this situation we can use assertCountEqual and assertSequenceEqual! . . . self.assertEqual({1, 2, 3}, {1, 2, 3}) self.assertSetEqual({1, 2, 3}, {1, 2, 3}) . . …

Total answers: 1

How do i mock an external libraries' classes/functions such as yaml.load() or Box() in python

How do i mock an external libraries' classes/functions such as yaml.load() or Box() in python Question: How would i go about testing the following class and its functions? import yaml from box import Box from yaml import SafeLoader class Config: def set_config_path(self): self.path = r"./config/datasets.yaml" return self.path def create_config(self): with open(r"./config/datasets.yaml") as f: self.config = …

Total answers: 1