Preventing pytest from creating .cache directories in Pycharm

There are two basic options: disable the caching altogether (the caching is done with the cacheprovider plugin): pytest -p no:cacheprovider -p is used to disable plugins. changing the cache location by tweaking the cache-dir configuration option (requires pytest 3.2+) Sets a directory where stores content of cache plugin. Default directory is .cache which is created … Read more

Mock exception raised in function using Pytest

You can mock error raising via side_effect parameter: Alternatively side_effect can be an exception class or instance. In this case the exception will be raised when the mock is called. In your case, this can be used like this (assuming call_api is defined in module foo): import pytest from unittest.mock import patch def test_api(): with … Read more

How to run specific code after all tests are executed?

You can use a autouse fixture with session scope: @pytest.fixture(scope=”session”, autouse=True) def db_conn(): # Will be executed before the first test conn = db.connect() yield conn # Will be executed after the last test conn.disconnect() You can then also use db_conn as an argument to a test function: def test_foo(db_conn): results = db_conn.execute(…)

how to share a variable across modules for all tests in py.test

Update: pytest-namespace hook is deprecated/removed. Do not use. See #3735 for details. You mention the obvious and least magical option: using a fixture. You can apply it to entire modules using pytestmark = pytest.mark.usefixtures(‘big_dict’) in your module, but then it won’t be in your namespace so explicitly requesting it might be best. Alternatively you can … Read more

Pytest – how to skip tests unless you declare an option/flag?

The pytest documentation offers a nice example on how to skip tests marked “slow” by default and only run them with a –runslow option: # conftest.py import pytest def pytest_addoption(parser): parser.addoption( “–runslow”, action=”store_true”, default=False, help=”run slow tests” ) def pytest_configure(config): config.addinivalue_line(“markers”, “slow: mark test as slow to run”) def pytest_collection_modifyitems(config, items): if config.getoption(“–runslow”): # –runslow … Read more

How to get PyTest working in Visual Studio

Support for Pytest in Visual Studio has been added on Visual Studio 2019 (16.3 Preview 2) You have to change your project’s test framework by right-clicking it and going to Properties -> Test You can add a pytest.ini to your project to configure pytest further. More info from MS themselves: https://devblogs.microsoft.com/python/whats-new-for-python-in-visual-studio-16-3-preview-2/

Returning multiple objects from a pytest fixture

Usually in order to avoid tuples and beautify your code, you can join them back together to one unit as a class, which has been done for you, using collections.namedtuple: import collections EventListener = collections.namedtuple(‘EventListener’, ‘event listener’) Now modify your fixture: @pytest.fixture def event_listener(): e = EventListener(EventEmitter(), Listener()) e.event.subscribe({‘event’ : [e.listener.operation]}) return e Now modify … Read more

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