Python Pytest: show full variables values on error (no minimization)

Using –showlocals If you just need the full variables printing, you can use the –showlocals option, combined with the verbose output. Example: $ pytest -vvl ==================================== test session starts ==================================== platform darwin — Python 3.6.6, pytest-3.9.1, py-1.5.4, pluggy-0.7.1 cachedir: .pytest_cache rootdir: /Users/hoefling/projects/private/stackoverflow, inifile: collected 1 item test_spam.py::test_in FAILED [100%] ========================================= FAILURES ========================================== __________________________________________ test_in __________________________________________ … Read more

pytest assert message customization with variable introspection

you could use Python built-in capability to show custom exception message: assert response.status_code == 200, f”My custom msg: actual status code {response.status_code}” Or you can built a helper assert functions: def assert_status(response, status=200): # you can assert other status codes too assert response.status_code == status, \ f”Expected {status}. Actual status {response.status_code}. Response text {response.text}” # … Read more

How to mock/set system date in pytest?

@Brian-Kruger’s answer is the best one. I’ve voted to undelete it. In the meantime… Use freezegun (repo). From the README: from freezegun import freeze_time @freeze_time(“2012-01-14”) def test(): assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)

How to share object from fixture to all tests using pytest?

My recommendation would to add the fixture to conftest.py and make sure to return the object you want to produce from the fixture. As noted, this makes “autouse” kind of useless. In the root directory for your tests, add the fixture to a file named conftest.py: @pytest.fixture(scope=”session”, autouse=True) def someobj(request): return SomeObj() Any test file … Read more

pytest: How to get a list of all failed tests at the end of the session? (and while using xdist)

Run pytest with -rf to get it to print a list of failed tests at the end. From py.test –help: -r chars show extra test summary info as specified by chars (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed, (p)passed, (P)passed with output, (a)all except pP. Warnings are displayed at all times except when –disable-warnings is set Here’s … Read more

pytest fixtures in a separate directory

Please add the following in your conftest.py import pytest pytest_plugins = [ “fixtures.conftest”, “fixtures.fixture_cifs”, “fixtures.fixture_ftp”, “fixtures.fixture_service” ] This ensures that all fixtures declared under fixtures/ will be found by pytest As a note that the respective directories referred to in fixtures.conftest” need to have __init__.py files for the plugins to be loaded by pytest A … Read more

Why did Flask start failing with “ImportError: cannot import name ‘url_quote’ from ‘werkzeug.urls'”?

I had the same problem. It is because Werkzeug 3.0.0 was released and Flask doesn’t specify the dependency correctly (requirements says Werkzeug>=2.2.0). This is why, Werkzeug 3.0.0 is still installed and Flask 2.2.2 isn’t made for Werkzeug 3.0.0. Solution: Just set a fix version for Werkzeug such as Werkzeug==2.2.2 in your requirements.txt and it should … Read more