What to do when a py.test hangs silently?

I ran into the same SQLite/Postgres problem with Flask and SQLAlchemy, similar to Gordon Fierce. However, my solution was different. Postgres is strict about table locks and connections, so explicitly closing the session connection on teardown solved the problem for me. My working code: @pytest.yield_fixture(scope=”function”) def db(app): # app is an instance of a flask … Read more

Creating a temporary directory in PyTest

UPDATE: Use tmp_path instead of tmpdir. tmp_path is a pathlib.Path/pathlib2.Path. tmpdir is a py.path (Actually LocalPath), which has offered syntax very similar to pathlib.Path. See pytest issue. Usage of py.path is no longer recommended by the developers. Syntax is similar, eg: def test_something_else(tmp_path): #create a file “myfile” in “mydir” in temp directory f1 = tmp_path … Read more

Can I run line_profiler over a pytest test?

Run pytest like this: python3 -m cProfile -o profile -m pytest You can even pass in optional arguments: python3 -m cProfile -o profile -m pytest tests/worker/test_tasks.py -s campaigns This will create a binary file called profile in your current directory. This can be analyzed with pstats: import pstats p = pstats.Stats(‘profile’) p.strip_dirs() p.sort_stats(‘cumtime’) p.print_stats(50) This … Read more

“ indirect fixture” error using pytest. What is wrong?

TL;DR – The problem is with the line @pytest.mark.parametrize(“entrada”,”esperado”,[ … ]) It should be written as a comma-separated string: @pytest.mark.parametrize(“entrada, esperado”,[ … ]) You got the indirect fixture because pytest couldn’t unpack the given argvalues since it got a wrong argnames parameter. You need to make sure all parameters are written as one string. Please … Read more

How to mock requests using pytest? [duplicate]

You can use requests-mock (PyPI), there is a fixture for a pytest usage. For your example: from correct.package import __BASE_URL from requests import HTTPError def test_get_employee(requests_mock): test_id = ‘random-id’ requests_mock.get(f'{__BASE_URL}/employee/{test_id}’, json= {‘name’: ‘awesome-mock’}) resp = get_employee(‘random-id’) assert resp == {‘name’: ‘awesome-mock’} def test_absent_employee(requests_mock): test_id = ‘does_not_exist’ requests_mock.get(f'{__BASE_URL}/employee/{test_id}’, status_code=404) with pytest.raises(HTTPError): resp = get_employee(test_id)

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