Skip to content

nonemania/modern-python-testing-login-full

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

From Logic to Browser: Modern Python Testing in VS Code

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

Project structure

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/

1) Open the project in VS Code

Open the project folder directly in VS Code.

2) Create a virtual environment

Windows PowerShell

python -m venv .venv
.venv\Scripts\Activate.ps1

3) Install dependencies

python -m pip install --upgrade pip
pip install -r requirements.txt
python -m playwright install

4) Run the FastAPI app

uvicorn app.main:app --reload

Open these URLs:

Demo login account:

  • username: admin
  • password: secret123

5) Run all tests

pytest -v

6) Run each test group separately

Hypothesis (logic / unit)

pytest -v tests/test_logic_hypothesis.py

Basic API tests

pytest -v tests/test_api_basic.py

Schemathesis API contract tests

pytest -v tests/test_api_schemathesis.py

Playwright UI tests

pytest -v tests/test_ui_playwright.py

7) VS Code Test Explorer

  1. Install the Python extension.
  2. Open the Testing view.
  3. Choose Configure Python Tests.
  4. Select pytest.
  5. VS Code should discover the files inside tests/.

Presentation flow idea

  1. Show the login page in the browser.
  2. Show test_logic_hypothesis.py and explain that Hypothesis generates many inputs automatically.
  3. Show test_api_basic.py and explain normal API tests.
  4. Show test_api_schemathesis.py and explain schema-driven API testing.
  5. Show test_ui_playwright.py and run the browser test live.

Notes

  • 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.py that starts the server automatically.
  • This project is designed for teaching/demo purposes, not production authentication.

Compare and Summary Tools

  • Hypothesis คือ Python Library สำหรับการทำ Testing ที่เปลี่ยนจากการสุ่มมั่วๆ มาเป็นการสุ่มแบบมีกลยุทธ์ เพื่อหาจุดบกพร่องที่เรานึกไม่ถึง สรุปคือ มันคือเครื่องมือที่ช่วยให้เราเขียน Test น้อยลง แต่ได้ความมั่นใจในคุณภาพ Software มากขึ้น
  • TestClient คือ เครื่องมือที่ช่วยให้เรา "จำลองการใช้งานจริง" (Terminal) โดยไม่ต้องเปิดหน้าเว็บจริงและรวดเร็วกว่าการเทสด้วยมือหลายเท่า
  • Schemathesis คิอ เครื่องมือทดสอบ API แบบอัตโนมัติขั้นสูง ที่ฉลาดกว่าการเขียน Unit Test ทั่วไป เหมือนเรา "จ้างบริษัทตรวจบ้าน" เอาเครื่องมือมาสแกนทุกซอกทุกมุมและทำเช็คลิสต์ตรวจสอบความเรียบร้อยแบบเป็นระบบ
  • Playwright คือเครื่องมือ Open-source สำหรับการทำ Automated Testing บนเว็บเบราว์เซอร์ (เช่น Chrome, Firefox, Safari) พัฒนาโดย Microsoft จุดเด่นคือทำงานได้รวดเร็วและสามารถโต้ตอบกับหน้าเว็บได้เหมือนที่มนุษย์ทำจริงๆ (คลิก, พิมพ์, รอข้อความปรากฏ)

About

unittest

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors