Introduction to pytest

Adding tests for posts

Want more?

This lesson for enrolled students only. Join the course to unlock it!

You can see the code changes implemented in this lecture below.

If you have purchased the course in a different platform, you still have access to the code changes per lecture here on Teclado. The lecture video and lecture notes remain locked.
Join course for $30

Modified files

storeapi/tests/routers/test_posts.py
--- 
+++ 
@@ -13,3 +13,31 @@
 @pytest.fixture()
 async def created_post(async_client: AsyncClient):
     return await create_post("Test Post", async_client)
+
+
+@pytest.mark.anyio
+async def test_create_post(async_client: AsyncClient):
+    body = "Test Post"
+
+    response = await async_client.post(
+        "/post",
+        json={"body": body},
+    )
+    assert response.status_code == 201
+    assert {"id": 0, "body": body}.items() <= response.json().items()
+
+
+@pytest.mark.anyio
+async def test_create_post_missing_data(async_client: AsyncClient):
+    response = await async_client.post(
+        "/post",
+        json={},
+    )
+    assert response.status_code == 422
+
+
+@pytest.mark.anyio
+async def test_get_all_posts(async_client: AsyncClient, created_post: dict):
+    response = await async_client.get("/post")
+    assert response.status_code == 200
+    assert response.json() == [created_post]