full-stack-fastapi-template
full-stack-fastapi-template copied to clipboard
Fresh project no changes gitlab ci test fail
gitlab ci test fail, local tests running fine.
`============================= test session starts ============================== platform linux -- Python 3.7.7, pytest-5.4.3, py-1.8.2, pluggy-0.13.1 rootdir: /app plugins: celery-4.4.5, cov-2.10.0 collected 25 items app/tests/api/api_v1/test_celery.py E [ 4%] app/tests/api/api_v1/test_items.py EE [ 12%] app/tests/api/api_v1/test_login.py FE [ 20%] app/tests/api/api_v1/test_users.py E.EEE.E [ 48%] app/tests/crud/test_item.py .... [ 64%] app/tests/crud/test_user.py ......... [100%] ==================================== ERRORS ==================================== __________________ ERROR at setup of test_celery_worker_test ___________________ client = <starlette.testclient.TestClient object at 0x7ff6a58a0b50> @pytest.fixture(scope="module") def superuser_token_headers(client: TestClient) -> Dict[str, str]:
return get_superuser_token_headers(client)
app/tests/conftest.py:27:
client = <starlette.testclient.TestClient object at 0x7ff6a58a0b50> def get_superuser_token_headers(client: TestClient) -> Dict[str, str]: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
a_token = tokens["access_token"]
E KeyError: 'access_token' app/tests/utils/utils.py:25: KeyError ______________________ ERROR at setup of test_create_item ______________________ client = <starlette.testclient.TestClient object at 0x7ff6a59bde50> @pytest.fixture(scope="module") def superuser_token_headers(client: TestClient) -> Dict[str, str]:
return get_superuser_token_headers(client)
app/tests/conftest.py:27:
client = <starlette.testclient.TestClient object at 0x7ff6a59bde50> def get_superuser_token_headers(client: TestClient) -> Dict[str, str]: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
a_token = tokens["access_token"]
E KeyError: 'access_token' app/tests/utils/utils.py:25: KeyError _______________________ ERROR at setup of test_read_item _______________________ client = <starlette.testclient.TestClient object at 0x7ff6a59bde50> @pytest.fixture(scope="module") def superuser_token_headers(client: TestClient) -> Dict[str, str]:
return get_superuser_token_headers(client)
app/tests/conftest.py:27:
client = <starlette.testclient.TestClient object at 0x7ff6a59bde50> def get_superuser_token_headers(client: TestClient) -> Dict[str, str]: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
a_token = tokens["access_token"]
E KeyError: 'access_token' app/tests/utils/utils.py:25: KeyError ___________________ ERROR at setup of test_use_access_token ____________________ client = <starlette.testclient.TestClient object at 0x7ff6a599c250> @pytest.fixture(scope="module") def superuser_token_headers(client: TestClient) -> Dict[str, str]:
return get_superuser_token_headers(client)
app/tests/conftest.py:27:
client = <starlette.testclient.TestClient object at 0x7ff6a599c250> def get_superuser_token_headers(client: TestClient) -> Dict[str, str]: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
a_token = tokens["access_token"]
E KeyError: 'access_token' app/tests/utils/utils.py:25: KeyError ________________ ERROR at setup of test_get_users_superuser_me _________________ client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> @pytest.fixture(scope="module") def superuser_token_headers(client: TestClient) -> Dict[str, str]:
return get_superuser_token_headers(client)
app/tests/conftest.py:27:
client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> def get_superuser_token_headers(client: TestClient) -> Dict[str, str]: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
a_token = tokens["access_token"]
E KeyError: 'access_token' app/tests/utils/utils.py:25: KeyError _________________ ERROR at setup of test_create_user_new_email _________________ client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> @pytest.fixture(scope="module") def superuser_token_headers(client: TestClient) -> Dict[str, str]:
return get_superuser_token_headers(client)
app/tests/conftest.py:27:
client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> def get_superuser_token_headers(client: TestClient) -> Dict[str, str]: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
a_token = tokens["access_token"]
E KeyError: 'access_token' app/tests/utils/utils.py:25: KeyError ___________________ ERROR at setup of test_get_existing_user ___________________ client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> @pytest.fixture(scope="module") def superuser_token_headers(client: TestClient) -> Dict[str, str]:
return get_superuser_token_headers(client)
app/tests/conftest.py:27:
client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> def get_superuser_token_headers(client: TestClient) -> Dict[str, str]: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
a_token = tokens["access_token"]
E KeyError: 'access_token' app/tests/utils/utils.py:25: KeyError _____________ ERROR at setup of test_create_user_existing_username _____________ client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> @pytest.fixture(scope="module") def superuser_token_headers(client: TestClient) -> Dict[str, str]:
return get_superuser_token_headers(client)
app/tests/conftest.py:27:
client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> def get_superuser_token_headers(client: TestClient) -> Dict[str, str]: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
a_token = tokens["access_token"]
E KeyError: 'access_token' app/tests/utils/utils.py:25: KeyError ____________________ ERROR at setup of test_retrieve_users _____________________ client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> @pytest.fixture(scope="module") def superuser_token_headers(client: TestClient) -> Dict[str, str]:
return get_superuser_token_headers(client)
app/tests/conftest.py:27:
client = <starlette.testclient.TestClient object at 0x7ff6a5940e50> def get_superuser_token_headers(client: TestClient) -> Dict[str, str]: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
a_token = tokens["access_token"]
E KeyError: 'access_token' app/tests/utils/utils.py:25: KeyError =================================== FAILURES =================================== ____________________________ test_get_access_token _____________________________ client = <starlette.testclient.TestClient object at 0x7ff6a599c250> def test_get_access_token(client: TestClient) -> None: login_data = { "username": settings.FIRST_SUPERUSER, "password": settings.FIRST_SUPERUSER_PASSWORD, } r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json()
assert r.status_code == 200
E assert 400 == 200 E + where 400 = <Response [400]>.status_code app/tests/api/api_v1/test_login.py:15: AssertionError ----------- coverage: platform linux, python 3.7.7-final-0 ----------- Name Stmts Miss Cover Missing
app/init.py 0 0 100% app/api/init.py 0 0 100% app/api/api_v1/init.py 0 0 100% app/api/api_v1/api.py 7 0 100% app/api/api_v1/endpoints/init.py 0 0 100% app/api/api_v1/endpoints/items.py 42 26 38% 22-28, 41-42, 56-62, 75-80, 93-99 app/api/api_v1/endpoints/login.py 47 21 55% 35, 50, 58-69, 81-96 app/api/api_v1/endpoints/users.py 63 38 40% 26-27, 40-51, 66-75, 100-113, 125-132, 146-153 app/api/api_v1/endpoints/utils.py 16 4 75% 22-23, 34-35 app/api/deps.py 34 5 85% 35-36, 42, 50, 61 app/backend_pre_start.py 21 21 0% 1-37 app/celeryworker_pre_start.py 21 21 0% 1-37 app/core/init.py 0 0 100% app/core/celery_app.py 3 0 100% app/core/config.py 59 5 92% 22, 25, 34, 45, 66 app/core/security.py 18 1 94% 21 app/crud/init.py 2 0 100% app/crud/base.py 39 7 82% 32, 35-40 app/crud/crud_item.py 17 1 94% 25 app/crud/crud_user.py 36 2 94% 31, 45 app/db/init.py 0 0 100% app/db/base.py 3 3 0% 3-5 app/db/base_class.py 8 0 100% app/db/init_db.py 9 9 0% 1-25 app/db/session.py 5 0 100% app/initial_data.py 14 14 0% 1-22 app/main.py 8 0 100% app/models/init.py 2 0 100% app/models/item.py 12 1 92% 9 app/models/user.py 14 1 93% 9 app/schemas/init.py 4 0 100% app/schemas/item.py 19 0 100% app/schemas/msg.py 3 0 100% app/schemas/token.py 7 0 100% app/schemas/user.py 20 0 100% app/tests/init.py 0 0 100% app/tests/api/init.py 0 0 100% app/tests/api/api_v1/init.py 0 0 100% app/tests/api/api_v1/test_celery.py 8 4 50% 11-18 app/tests/api/api_v1/test_items.py 22 16 27% 11-20, 26-35 app/tests/api/api_v1/test_login.py 15 6 60% 16-17, 23-28 app/tests/api/api_v1/test_users.py 73 48 34% 15-20, 37-47, 53-65, 71-82, 100-115 app/tests/conftest.py 22 0 100% app/tests/crud/init.py 0 0 100% app/tests/crud/test_item.py 52 0 100% app/tests/crud/test_user.py 75 0 100% app/tests/utils/init.py 0 0 100% app/tests/utils/item.py 14 7 50% 12-18 app/tests/utils/user.py 30 2 93% 47-48 app/tests/utils/utils.py 16 2 88% 26-27 app/tests_pre_start.py 21 21 0% 1-37 app/utils.py 54 40 26% 19-33, 37-41, 50-56, 71-76, 91-98, 102-106 app/worker.py 7 7 0% 1-11
TOTAL 962 333 65% =========================== short test summary info ============================ FAILED app/tests/api/api_v1/test_login.py::test_get_access_token - assert 400... ERROR app/tests/api/api_v1/test_celery.py::test_celery_worker_test - KeyError... ERROR app/tests/api/api_v1/test_items.py::test_create_item - KeyError: 'acces... ERROR app/tests/api/api_v1/test_items.py::test_read_item - KeyError: 'access_... ERROR app/tests/api/api_v1/test_login.py::test_use_access_token - KeyError: '... ERROR app/tests/api/api_v1/test_users.py::test_get_users_superuser_me - KeyEr... ERROR app/tests/api/api_v1/test_users.py::test_create_user_new_email - KeyErr... ERROR app/tests/api/api_v1/test_users.py::test_get_existing_user - KeyError: ... ERROR app/tests/api/api_v1/test_users.py::test_create_user_existing_username ERROR app/tests/api/api_v1/test_users.py::test_retrieve_users - KeyError: 'ac... ==================== 1 failed, 15 passed, 9 errors in 4.54s ==================== Running after_script 00:02 Uploading artifacts for failed job 00:03 ERROR: Job failed: exit code 1`
same issue
@rasenx this happens if you are running the tests and the builds on the same gitlab-runner instance
Same here
For me this occurs during tests when you are hitting a database that does not yet have the superuser created in it. Normally, that first superuser is created by a call to init_db()
in backend/app/app/db/init_db.py
. For tests, you can add a fixture that uses your db fixture to make sure the superuser is created prior to test runs:
#backend/app/app/tests/conftest.py
from app.db.init_db import init_db
....
@pytest.fixture(scope="session", autouse=True)
def init_test_db(db: Session) -> None:
init_db(db)
Thanks all! I just moved the CI to GitHub Actions, the GitLab CI is no longer there. It should work fine with GitHub Actions. But you are also free to use that as a boilerplate to update your own CI (with GitLab or anything else). :nerd_face:
FYI
I get the same error when running the tests locally, still not sure how to fix this