andredias/codebox

Test Coverage doesn't work when code runs in a container

Closed this issue · 1 comments

Current tests make calls to REST API in a running container. So, test coverage doesn't work well and gives wrong statistics. To get better results, tests must run inside the container, following the pattern to call the app directly, as used in ordinary FastAPI tests.

As codebox doesn't have external dependencies, we might use something like:

docker run --rm -it --privileged --init -e ENV=TESTING --ipc=none \
      -v $(pwd)/tests:/codebox/tests \
      --name codebox codebox pytest -svx

The pieces of code used in tests must run inside a container due to security reasons. Current tests call a running container to do that but it prevents test coverage to gather correct results.

Another option is to run the tests from inside the container. conftest.py would use an ordinary FastAPI test strategy to create a client fixture:

@fixture(scope='session')
async def app() -> AsyncIterable[FastAPI]:
    """
    Create a FastAPI instance.
    """
    async with LifespanManager(_app):
        yield _app

@fixture
async def client(app: FastAPI) -> AsyncIterable[AsyncClient]:
    async with AsyncClient(
        app=app,
        base_url='http://testserver',
        headers={'Content-Type': 'application/json'},
    ) as client:
        yield client