pytest-docker
pytest-docker copied to clipboard
Fixture `docker_ip` not found
Hello,
I'm creating an http_service
fixture like the one featured in the readme. When I use the fixture, I'm getting fixture 'docker_ip' not found
.
conftest.py
import json
import os
import pytest
import requests
import pathlib
from .utils import is_responsive_404
@pytest.fixture
def tests_dir():
return pathlib.Path(__file__).resolve().parent
@pytest.fixture(scope="session")
def docker_compose_file(pytestconfig):
return pathlib.Path(__file__).resolve().parent / "test-docker-compose.yml"
@pytest.fixture(scope="session")
def http_service(docker_ip, docker_services):
"""
Ensure that Django service is up and responsive.
"""
# `port_for` takes a container port and returns the corresponding host port
port = docker_services.port_for("django", 8000)
url = "http://{}:{}".format(docker_ip, port)
url404 = f"{url}/missing"
docker_services.wait_until_responsive(
timeout=2400.0, pause=0.1, check=lambda: is_responsive_404(url404)
)
return url
tests file
import json
import pytest
import requests
test_endpoint = "api/"
test_headers = {"Content-Type": "application/json", "Accept": "application/json"}
status = 200
def test_item_list_up(http_service):
"""
Test item list is up
"""
response = requests.get(f"{http_service}/{test_endpoint}item/", headers=test_headers)
resp_data = response.json()
assert response.status_code == status
assert resp_data.get('results', None) != None
assert resp_data.get('pagination', {}).get('totalResults', -1) != -1
When I run pytest -vv
I am getting:
ERROR at setup of test_item_list_up
file .../test_item_api.py, line 24
def test_item_list_up(http_service):
file .../tests/conftest.py, line 18
@pytest.fixture(scope="session")
def http_service(docker_ip, docker_services):
E fixture 'docker_ip' not found
> available fixtures: anyio_backend, anyio_backend_name, anyio_backend_options, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, class_mocker, docker_compose_file, doctest_namespace, http_service, mocker, module_mocker, monkeypatch, package_mocker, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, session_mocker, tests_dir, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
Current pip venv:
Package Version
------------------ ---------
attrs 21.4.0
bcrypt 4.0.1
certifi 2022.12.7
cffi 1.15.1
charset-normalizer 2.0.12
cryptography 40.0.1
distro 1.8.0
docker 6.0.1
docker-compose 1.29.2
dockerpty 0.4.1
docopt 0.6.2
exceptiongroup 1.1.1
idna 3.4
iniconfig 2.0.0
jsonschema 3.2.0
packaging 23.0
paramiko 3.1.0
pip 21.2.3
pluggy 1.0.0
py 1.11.0
pycparser 2.21
PyNaCl 1.5.0
pyrsistent 0.19.3
pytest 6.2.5
pytest-docker 1.0.1
python-dotenv 0.21.1
PyYAML 5.4.1
requests 2.26.0
setuptools 57.4.0
six 1.16.0
texttable 1.6.7
toml 0.10.2
tomli 2.0.1
urllib3 1.26.15
websocket-client 0.59.0
Any thoughts @Luminaar ?
Looks like the pytest-docker plugin is simply not being loaded. Only the docker_compose_file
shows in your error message because it is overridden in your conftest.py. If you run the pytest --fixtures [testpath]
you were suggested, you should see a section like
------------------------------------------------ fixtures defined from pytest_docker.plugin ------------------------------------------------
docker_compose_command [session scope] -- .venv/lib/python3.10/site-packages/pytest_docker/plugin.py:133
Docker Compose command to use, it could be either `docker-compose`
for Docker Compose v1 or `docker compose` for Docker Compose
v2.
docker_compose_file [session scope] -- .venv/lib/python3.10/site-packages/pytest_docker/plugin.py:142
Get an absolute path to the `docker-compose.yml` file. Override this
fixture in your tests if you need a custom location.
docker_compose_project_name [session scope] -- .venv/lib/python3.10/site-packages/pytest_docker/plugin.py:150
Generate a project name using the current process PID. Override this
fixture in your tests if you need a particular project name.
docker_cleanup [session scope] -- .venv/lib/python3.10/site-packages/pytest_docker/plugin.py:163
Get the docker_compose command to be executed for test clean-up actions.
Override this fixture in your tests if you need to change clean-up actions.
Returning anything that would evaluate to False will skip this command.
docker_setup [session scope] -- .venv/lib/python3.10/site-packages/pytest_docker/plugin.py:177
Get the docker_compose command to be executed for test setup actions.
Override this fixture in your tests if you need to change setup actions.
Returning anything that would evaluate to False will skip this command.
docker_services [session scope] -- .venv/lib/python3.10/site-packages/pytest_docker/plugin.py:211
Start all services from a docker compose file (`docker-compose up`).
After test are finished, shutdown all services (`docker-compose down`).
docker_ip [session scope] -- .venv/lib/python3.10/site-packages/pytest_docker/plugin.py:46
Determine the IP address for TCP connections to Docker containers.
If you don't: check your pytest configuration as some option might be disabling the plugin autoload.
I had this message when the Docker daemon was unavailable. There was a previous message in the exception stack that gave me the hint. Once the daemon was running, the pytest --fixtures [testpath]
command showed the previously missing fixtures and all was well running the tests.
Maybe this insight helps someone in the future? Does it help you @ColeDCrawford ??
I had a similar issue just now actually, and the solution was as simple, as I was stupid!
Make sure, if you are using a venv
, that you are using the pytest
from the venv
in which you installed pytest-docker
!
I had a similar issue just now actually, and the solution was as simple, as I was stupid!
Make sure, if you are using a
venv
, that you are using thepytest
from thevenv
in which you installedpytest-docker
!
I have same solution))