Fixture not clearing database before each test

I am writing tests for a FastAPI application using httpx and pytest-asyncio. I want to ensure that the MongoDB database is cleared before each test method. However, despite setting up the fixture with scope="function" and autouse=True, it seems that the database is not being cleared before each test.

Here is the code for the tests and the fixture:

import pytest
from httpx import ASGITransport, AsyncClient
from motor.motor_asyncio import AsyncIOMotorClient
from app.main import app  # Ensure your FastAPI app is correctly imported

@pytest.fixture(scope="function", autouse=True)
async def clear_db():
    print("Clearing the database")
    client = AsyncIOMotorClient("mongodb://localhost:27017")
    await client["markdown_db"]["markdown_files"].delete_many({})
    client.close()

@pytest.mark.asyncio
async def test_create_markdown():
    transport = ASGITransport(app)
    async with AsyncClient(transport=transport, base_url="http://testserver") as client:
        response = await client.post("/markdowns/", json={"content": "Company: TestCompanynContent goes here."})
        assert response.status_code == 200, response.text

@pytest.mark.asyncio
async def test_read_markdown():
    transport = ASGITransport(app)
    async with AsyncClient(transport=transport, base_url="http://testserver") as client:
        create_response = await client.post("/markdowns/", json={"content": "Company: TestCompanynContent goes here."})
        assert create_response.status_code == 200
        markdown_id = create_response.json()["id"]
        read_response = await client.get(f"/markdowns/{markdown_id}")
        assert read_response.status_code == 200
        data = read_response.json()
        assert data["company"] == "TestCompany"

@pytest.mark.asyncio
async def test_update_markdown():
    transport = ASGITransport(app)
    async with AsyncClient(transport=transport, base_url="http://testserver") as client:
        create_response = await client.post("/markdowns/", json={"content": "Company: TestCompanynContent goes here."})
        assert create_response.status_code == 200
        markdown_id = create_response.json()["id"]
        update_response = await client.put(f"/markdowns/{markdown_id}", json={"content": "Updated content."})
        assert update_response.status_code == 200
        data = update_response.json()
        assert data["content"] == "Updated content."

@pytest.mark.asyncio
async def test_read_all_markdowns():
    transport = ASGITransport(app)
    async with AsyncClient(transport=transport, base_url="http://testserver") as client:
        await client.post("/markdowns/", json={"content": "Company: TestCompany1nContent goes here."})
        await client.post("/markdowns/", json={"content": "Company: TestCompany2nContent goes here."})
        read_response = await client.get("/markdowns/")
        assert read_response.status_code == 200
        data = read_response.json()
        assert len(data) >= 2

Why might the clear_db fixture not be executing as expected before each test? Are there any best practices or corrections needed in the fixture or the test setup to ensure the database is cleared before each test method?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật