How to get coverage reporting when testing a pytest plugin?
Instead of using the pytest-cov plugin, use coverage to run pytest: coverage run -m pytest …. That way, coverage will be started before pytest.
Instead of using the pytest-cov plugin, use coverage to run pytest: coverage run -m pytest …. That way, coverage will be started before pytest.
According to docs, pytest_namespace has been removed in version 4.0: One can use pytest_configure to share global variables. Example: import pytest def pytest_configure(): pytest.my_symbol = MySymbol()
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
To properly handle this you need to register the custom marker. Create a pytest.ini file and place the following inside of it. [pytest] markers = webtest: mark a test as a webtest. Next time you run the tests, the warning about the unregistered marker will not be there.
Monkey patching socket ought to do it: import socket def guard(*args, **kwargs): raise Exception(“I told you not to use the Internet!”) socket.socket = guard Make sure this runs before any other import.
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
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
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
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)
py.test will import conftest.py and all Python files that match the python_files pattern, by default test_*.py. If you have a test fixture, you need to include or import it from conftest.py or from the test files that depend on it: from sonoftest import pytest_addoption, cmdopt