py.test logging control
Installing and using the pytest-capturelog plugin could satisfy most of your pytest/logging needs. If something is missing you should be able to implement it relatively easily.
Installing and using the pytest-capturelog plugin could satisfy most of your pytest/logging needs. If something is missing you should be able to implement it relatively easily.
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
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
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(…)
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
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
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/
I found I must add __init__.py in test and projroot. __init__.py in src is not necessary. And pytest could run correctly in 3 folders: # parent folder of project root C:\>pytest projroot # project root C:\>cd projroot C:\projroot>pytest # test folder C:\projroot>cd test C:\projroot\test>pytest
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
It appears that all of your test output is going stdout, so you simply need to “redirect” your python invocation’s output there: python test_out.py >myoutput.log You can also “tee” the output to multiple places. E.g., you might want to log to the file yet also see the output on your console. The above example then … Read more