unit-testing

How to parametrise unit tests for sklearn models

How to parametrise unit tests for sklearn models Question: I want to create some unit tests to make sure the correct NotImplemented exceptions are thrown for a module I am developing. Is there a way that I could use some dummy data, create and fit multiple sklearn models to feed into the unit tests? I’m …

Total answers: 1

Hard to understand return_value for Python Mock Class in the following example

Hard to understand return_value for Python Mock Class in the following example Question: I had a hard time understanding the choice of using return_value. See the following example # my_module.py def query(connection): cursor = connection.cursor() cursor.execute("SELECT name FROM users WHERE id = 1") return cursor.fetchone()[0] # test_query.py import pytest from unittest.mock import Mock # Import …

Total answers: 1

How to mock a function which gets executed during the import time?

How to mock a function which gets executed during the import time? Question: Here the ABC() and obj.print_1() get called during the import time and it prints "making object" and "printed 1" respectively. How can we mock all the three functions, __init__(), print_1(), and print_2()? xyz.py from abc import ABC obj = ABC() obj.print_1() def …

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

How to mock a class with nested properties and autospec?

How to mock a class with nested properties and autospec? Question: I’m wondering if it’s possible to mock a class which contains properties by using patch and autospec? The goal in the example below is to mock (recursively) ClassB. Example: # file: class_c.py class ClassC: def get_default(self) -> list[int]: return [1, 2, 3] def delete(self, …

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

Python unable to mock function call from test class

Python unable to mock function call from test class Question: I am trying to mock a bigtable call in my unit test by declaring fixtures like so: @pytest.fixture() def bigtableMock(): bigtableMock = Mock(spec=google.cloud.bigtable.table.Table) yield bigtableMock @pytest.fixture() def bigtableInstanceMock(bigtableMock): bigtableInstanceMock = Mock(spec=google.cloud.bigtable.instance.Instance) bigtableInstanceMockAttrs = {‘table’: bigtableMock} bigtableInstanceMock.configure_mock(**bigtableInstanceMockAttrs) yield bigtableInstanceMock @pytest.fixture() def myDao(bigtableInstanceMock): yield MyDao(bigtableInstanceMock) I mock …

Total answers: 1