I'm trying to create a fixture that can take existing test files and execute them within a docker container. I just can't seem to wrap my head around it.
Non working code for reference.
test_foo.py
class TestClass:
def test_example(self):
a = 1
b = 2
assert a + b == 3
conftest.py
import docker as docker_py
import pytest
docker = docker_py.from_env()
@pytest.fixture(autouse=True, scope="class")
def generate_container(request):
""" Create a container for all tests within class to run inside of """
con = docker.containers.run(image="test_image", tty=True, detach=True)
request.cls.container = con
@pytest.fixture(autouse=True, scope="function")
def run_tests_in_container(request):
"""
Not sure how to get the contents of request.function
to execute inside of the docker container
"""
output = request.cls.container.exec_run( ??? )
exit_code = output[0]
assert exit_code == 0
Ideally I'd like the test to only run inside the container. I think with my current approach the test will run inside the container and then run again on the host (unless I add in pytest.skip but that seems wrong)