A full demo project for presenting smarter login testing with:
- Hypothesis for logic / unit testing
- Schemathesis for API contract testing
- Playwright for UI / browser testing
- FastAPI as the demo app
- pytest as the shared test runner
modern-python-testing-login-full/
├─ app/
│ ├─ auth.py
│ ├─ main.py
│ └─ templates/
│ └─ login.html
├─ tests/
│ ├─ conftest.py
│ ├─ test_logic_hypothesis.py
│ ├─ test_api_basic.py
│ ├─ test_api_schemathesis.py
│ └─ test_ui_playwright.py
├─ .vscode/settings.json
├─ pytest.ini
├─ requirements.txt
├─ .gitignore
└─ scripts/
Open the project folder directly in VS Code.
python -m venv .venv
.venv\Scripts\Activate.ps1python -m pip install --upgrade pip
pip install -r requirements.txt
python -m playwright installuvicorn app.main:app --reloadOpen these URLs:
- App: http://127.0.0.1:8000/
- Swagger UI: http://127.0.0.1:8000/docs
- OpenAPI JSON: http://127.0.0.1:8000/openapi.json
Demo login account:
- username:
admin - password:
secret123
pytest -vpytest -v tests/test_logic_hypothesis.pypytest -v tests/test_api_basic.pypytest -v tests/test_api_schemathesis.pypytest -v tests/test_ui_playwright.py- Install the Python extension.
- Open the Testing view.
- Choose Configure Python Tests.
- Select pytest.
- VS Code should discover the files inside
tests/.
- Show the login page in the browser.
- Show
test_logic_hypothesis.pyand explain that Hypothesis generates many inputs automatically. - Show
test_api_basic.pyand explain normal API tests. - Show
test_api_schemathesis.pyand explain schema-driven API testing. - Show
test_ui_playwright.pyand run the browser test live.
- The HTML routes (
/and/login) are hidden from OpenAPI so Schemathesis focuses on the API routes. - The Playwright tests use a fixture in
tests/conftest.pythat starts the server automatically. - This project is designed for teaching/demo purposes, not production authentication.
- Hypothesis คือ Python Library สำหรับการทำ Testing ที่เปลี่ยนจากการสุ่มมั่วๆ มาเป็นการสุ่มแบบมีกลยุทธ์ เพื่อหาจุดบกพร่องที่เรานึกไม่ถึง สรุปคือ มันคือเครื่องมือที่ช่วยให้เราเขียน Test น้อยลง แต่ได้ความมั่นใจในคุณภาพ Software มากขึ้น
- TestClient คือ เครื่องมือที่ช่วยให้เรา "จำลองการใช้งานจริง" (Terminal) โดยไม่ต้องเปิดหน้าเว็บจริงและรวดเร็วกว่าการเทสด้วยมือหลายเท่า
- Schemathesis คิอ เครื่องมือทดสอบ API แบบอัตโนมัติขั้นสูง ที่ฉลาดกว่าการเขียน Unit Test ทั่วไป เหมือนเรา "จ้างบริษัทตรวจบ้าน" เอาเครื่องมือมาสแกนทุกซอกทุกมุมและทำเช็คลิสต์ตรวจสอบความเรียบร้อยแบบเป็นระบบ
- Playwright คือเครื่องมือ Open-source สำหรับการทำ Automated Testing บนเว็บเบราว์เซอร์ (เช่น Chrome, Firefox, Safari) พัฒนาโดย Microsoft จุดเด่นคือทำงานได้รวดเร็วและสามารถโต้ตอบกับหน้าเว็บได้เหมือนที่มนุษย์ทำจริงๆ (คลิก, พิมพ์, รอข้อความปรากฏ)