full-stack-fastapi-template icon indicating copy to clipboard operation
full-stack-fastapi-template copied to clipboard

Idea: Run tests locally outside docker

Open skrchnavy opened this issue 6 years ago • 0 comments

related also to #24.

At first, the tests/api/* are not using fixture server_api

from app.tests.utils.utils import get_server_api

def test_celery_worker_test(superuser_token_headers):
    server_api = get_server_api()

shall be

def test_celery_worker_test(server_api, superuser_token_headers):

this would allow to improve pytest tests this way (as idea, further improvements possible):

from multiprocessing import Process

import pytest
import uvicorn

def run_server():
    uvicorn.run("app.main:app", port=8123)

@pytest.fixture(scope="module")
def server_api(server):
    proc = Process(target=run_server, args=(), daemon=True)
    proc.start()
    # maybe some sleep here to wait for server starts
    yield ('localhost', 8123)
    proc.kill()  # Cleanup after test

credits to: https://stackoverflow.com/questions/57412825/how-to-start-a-uvicorn-fastapi-in-background-when-testing-with-pytest

skrchnavy avatar Sep 11 '19 17:09 skrchnavy