How to monkeypatch python’s datetime.datetime.now with py.test?

You need to monkeypatch datetime.now function. In example below, I’m creating fixture which I can re-use later in other tests: import datetime import pytest FAKE_TIME = datetime.datetime(2020, 12, 25, 17, 5, 55) @pytest.fixture def patch_datetime_now(monkeypatch): class mydatetime: @classmethod def now(cls): return FAKE_TIME monkeypatch.setattr(datetime, ‘datetime’, mydatetime) def test_patch_datetime(patch_datetime_now): assert datetime.datetime.now() == FAKE_TIME

Pytest and Python 3

I found a workaround: Installed python3-pip using aptitude, which created /usr/bin/pip-3.2. Next pip-3.2 install pytest which re-installed pytest, but under a python3.2 path. Then I was able to use python3 -m pytest somedir/sometest.py. Not as convenient as running py.test directly, but workable.

@Patch decorator is not compatible with pytest fixture

When using pytest fixture with mock.patch, test parameter order is crucial. If you place a fixture parameter before a mocked one: from unittest import mock @mock.patch(‘my.module.my.class’) def test_my_code(my_fixture, mocked_class): then the mock object will be in my_fixture and mocked_class will be search as a fixture: fixture ‘mocked_class’ not found But, if you reverse the order, … Read more

How to keep Unit tests and Integrations tests separate in pytest

Yes, you can mark tests with the pytest.mark decorator. Example: def unit_test_1(): # assert here def unit_test_2(): # assert here @pytest.mark.integtest def integration_test(): # assert here Now, from the command line, you can run pytest -m “not integtest” for only the unit tests, pytest -m integtest for only the integration test and plain pytest for … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)