How to mock/set system date in pytest?

In some of my tests I am having a problem that they fail on Travis because of time and time zone problems, so I want to mock system time for my test. How can I do this?

patching a class yields "AttributeError: Mock object has no attribute" when accessing instance attributes

The Problem Using mock.patch with autospec=True to patch a class is not preserving attributes of instances of that class. The Details I am trying to test a class Bar that instantiates an instance...

Mocking a Python class static variable with Pytest

I am trying to properly create mocks for a class that has a dependency on a system library. Currently the code makes a connection to a socket for the library when being tested, and I am trying to...

pytest-mock: Mock an abstract class

I'm trying to make the following code work: from pytest_mock import mocker class TestClass(mocker): def setup_method(self): self.some_mock = SomeAbstractClass() self.testsubject =...

How to check if a function was called in a unit test using pytest-mock?

I have a unit test where I want to check if a function was called. How do I do this withpytest and pytest-mock libraries? For example, here is a unit test test_hello.py. In this test I call the...

mocking snowflake connection

I have a SnowflakeApi class in python which just works as a wrapper on top of the SnowflakeConnection class. My SnowflakeApi is import logging import os from snowflake.connector import connect ...

How to mock external requests using aiohttp?

I'm trying to mock a single request to an external URL but in the documentation exists just examples to internal request (starting with '/'), it's impossible to add routers who not start with '/'...

Invalid syntax in more-itertools when running pytest

I have the following minimal setup.py: import setuptools setuptools.setup( setup_requires=['pytest-runner'], tests_require=['mock', 'pytest'], test_suite='tests', ...

Mocking a RelatedManager in Django 2

This question is directly related to this question, but that one is now outdated it seems. I am trying to test a view without having to access the database. To do that I need to Mock a...

What's the best way to unit test functions that handle csv files?

I'm trying to unit-test a function that deals with csv files with Pytest. While my function works, I feel like there's a lot of code repetition when creating "sample" csv files in my project...

Can't install dependencies in docker container

I'm using poetry library to manage project dependencies, so when I use docker build --tag=helloworld . I got this error [AttributeError] 'NoneType' object has no attribute 'group' ...

"AssertionError: Torch not compiled with CUDA enabled" in spite upgrading to CUDA version

I figured out this is a popular question, but still I couldn't find a solution for that. I'm trying to run a simple repo Here which uses PyTorch. Although I just upgraded my Pytorch to the latest...

ERROR: You must give at least one requirement to install -- when running: pip install --upgrade --no-binary hdbscan

I am trying to install hdbscan in my PC which runs Windows 10 and has installed Python 3.6. My first attempt failed: (base) C:\WINDOWS\system32>pip install hdbscan --user Collecting hdbscan ...

Moto does not appear to be mocking aws interactions in a pytest

Say I want to mock the following: session = boto3.Session(profile_name=profile) resource = session.resource('iam') iam_users = resource.users.all() policies = resource.policies.filter(Scope='AWS',...

Why is caplog.text empty, even though the function I'm testing is logging?

I'm trying to use pytest to test if my function is logging the expected text, such as addressed this question (the pyunit equivalent would be assertLogs). Following the pytest logging...

Is it possible to install SpaCy to Raspberry Pi 4 Raspbian Buster

I have been stuck at installing SpaCy the entire day. sudo pip install -U spacy Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting spacy Using cached...

Mock thread calls inside pytest-mock

I have a simple code base like below : import threading import time class Demo: def __init__(self): self._run_thread = False def start(self): if not self._run_thread: ...

How to mock django settings attributes in pytest-django

When using Django default unittest it's trivial to patch settings attributes (using @override_settings decorator for instance.) I'd like to override several attributes of my settings for a test...

How to create a unittest for a function that uses http client library using pytest & mocks in python?

How do I mock test the following function using pytest? import http.client def get_response(req_type, host, sub_domain, payload=None, headers=None, body=None): conn =...

How to mock multiple urls in request mock

I have a method which is calling two different end points and validating there response. def foo_bar: status_1 = requests.post( "http://myapi/test/status1", {}, ...

Trouble installing turbodbc

I am attempting to install turbodbc on my Ubuntu 20.10 machine. My specs are as follows: pip 20.2.4, Python 3.8.5 , gcc (Ubuntu 10.2.0-13ubuntu1) 10.2.0 I have attempted the solutions provided in...

dockerpy not able to connect to docker daemon inside bit bucket container

I have the below simple script which is basically to test whether https://github.com/docker/docker-py is able to run commands inside bitbucket containers: import docker CLIENT =...

How to mock subprocess.run in pytest?

I have this class defined: class InternalProc: @staticmethod def get_data(): try: result = subprocess.run(['bridge-client', '--business-credentials'], ...

How to use pytest-mock to mock a function conditionally

I have a class in foo.py that I wish to test: import requests class Foo: def fooMethod(self, url): response = requests.get(url) return response I want to replace the...

TypeError: super() argument 1 must be type, not MagicMock - azure-devops python package

I am using the azure-devOps python module to access the azure APIs to retrieve some information. I have used the below code to set up the authentication required to make a connection. def...

ERROR: file not found when using pytest with VSCode (macOS)

I am trying to run a single unit test within VSCode by clicking Run Test (or Debug Test) above the unit test in the editor field, but I keep getting an ERROR: file not...

How to correctly mock a gcp client library call in python

How do I correctly write a unit test for a function that uses the GCP secret manager client library. I've been reading up on unit testing and mocking but I just can't seem to grasp what's going...

How to mock a tensorflow model?

I would like to test a function that takes a tf.keras model and performs some operations on it. Example function illustrating the problem: def func(model, x): with tf.GradientTape() as tape: ...

pytest/unittest: mock.patch function from module?

Given a folder structure like such: dags/ **/ code.py tests/ dags/ **/ test_code.py conftest.py Where dags serves as the root of the src files, with 'dags/a/b/c.py' imported...

"This plugin does not support propagateSizeHints()" / calling `exec` on QDialog subclass

NB OS is W10. The context here is pytest-qt. I found this answer and tried to use it (see later). I get this error when a test method calls an application method which creates a QFileDialog and...