Stop pytest right at the start if condition not met

Try using the pytest_configure initialization hook.

In your global conftest.py:

import requests
import pytest

def pytest_configure(config):
    try:
        requests.get(f'http://localhost:9200')
    except requests.exceptions.ConnectionError:
        msg = 'FATAL. Connection refused: ES does not appear to be installed as a service (localhost port 9200)' 
        pytest.exit(msg)

Updates:

  1. Note that the single argument of pytest_configure has to be named config!
  2. Using pytest.exit makes it look nicer.