Skip to content

Testing

For Testing

Test always is a part of developer when create featute follow to TDD progress.

FastAPI intergrate testing with pytest, httpx and some fake data generate tools like Faker

ref: FastAPI Testing

ref: Faker to create fake data

ref: Factoy Boy

Progress

Export the variable TEST_DATABASE_URI that is the connection string to Test Database. The database for testing purpose can be the database service in local development database in Docker.

export TEST_DATABASE_URI=postgresql+pg8000://postgres:postgres@127.0.0.1:5432/postgres

Prepair working directory and the environment

cd backend && source venv/Scripts/activate

then write your tests cases, with syntax of prefix test_*

then, execute the test

python3 -m pytest

https://labs.flinters.vn/technote/thiet-ke-restful-api-nhu-the-nao/

Concept of Fixture of Pytest

ref: https://docs.pytest.org/en/6.2.x/fixture.html