ai

Software Automation Testing คือ —

software automation testing คอ
Software Automation Testing คือ —

Automation Testing

Software Automation Testing คือ —

Software Automation Testing Unit Integration E2E Performance Selenium Playwright Cypress Jest pytest CI/CD Pipeline Regression Smoke TDD BDD

Test TypeScopeSpeedCostเครื่องมือ
Unit TestFunctionเร็วมากต่ำJest pytest JUnit
IntegrationModuleปานกลางปานกลางpytest Supertest
E2Eทั้งระบบช้าสูงPlaywright Cypress
PerformanceLoad/Stressช้าสูงk6 JMeter Locust
APIEndpointเร็วต่ำPostman Newman

Unit Test และ Integration

=== Unit Test with pytest ===

pip install pytest pytest-cov

test_calculator.py

class Calculator:

return a + b

if b == 0:

raise ValueError("Cannot divide by zero")

return a / b

import pytest

class TestCalculator:

self.calc = Calculator()

assert self.calc.add(2, 3) == 5

assert self.calc.add(-1, 1) == 0

assert self.calc.add(0, 0) == 0

assert self.calc.divide(10, 2) == 5

assert self.calc.divide(7, 2) == 3.5

with pytest.raises(ValueError):

self.calc.divide(10, 0)

@pytest.mark.parametrize("a, b, expected", [

(1, 1, 2),

(2, 3, 5),

(-1, -1, -2),

(100, 200, 300),

])

assert self.calc.add(a, b) == expected

Run: pytest test_calculator.py -v --cov=. --cov-report=html

Integration Test — API

import httpx

import pytest

BASE_URL = "http://localhost:8000"

@pytest.fixture

async with httpx.AsyncClient(base_url=BASE_URL) as client:

yield client

@pytest.mark.asyncio

response = await client.post("/users", json={

"name": "Test User",

"email": "test@example.com"

})

assert response.status_code == 201

data = response.json()

assert data["name"] == "Test User"

@pytest.mark.asyncio

response = await client.get("/users")

assert response.status_code == 200

assert isinstance(response.json(), list)

from dataclasses import dataclass

@dataclass

class TestSuite:

name: str

type: str

tests: int

passed: int

failed: int

duration_sec: float

coverage_pct: float

suites = [

TestSuite("Unit Tests", "Unit", 250, 248, 2, 12.5, 85.3),

TestSuite("API Integration", "Integration", 80, 79, 1, 45.0, 72.1),

TestSuite("Database Tests", "Integration", 45, 45, 0, 30.2, 68.5),

TestSuite("E2E Web", "E2E", 35, 34, 1, 180.0, 0),

TestSuite("Performance", "Load", 10, 10, 0, 300.0, 0),

]

status = "PASS" if s.failed == 0 else "FAIL"

E2E Test

=== E2E Test with Playwright ===

pip install playwright

playwright install

test_login.py

import pytest

from playwright.sync_api import Page, expect

Software Automation Testing คือ —

page.goto("https://app.example.com/login")

page.fill("#email", "user@example.com")

page.fill("#password", "password123")

page.click("button[type=submit]")

expect(page).to_have_url("https://app.example.com/dashboard")

expect(page.locator("h1")).to_have_text("Dashboard")

page.goto("https://app.example.com/login")

page.fill("#email", "wrong@example.com")

page.fill("#password", "wrong")

page.click("button[type=submit]")

expect(page.locator(".error")).to_be_visible()

expect(page.locator(".error")).to_have_text("Invalid credentials")

page.goto("https://app.example.com/products")

page.click(".product-card:first-child .add-to-cart")

page.click("a[href='/cart']")

expect(page.locator(".cart-items")).to_have_count(1)

page.click("#checkout")

expect(page).to_have_url("/checkout")

Run: pytest test_login.py --headed # visible browser

Run: pytest test_login.py # headless (CI)

Playwright Config

pytest.ini:

[pytest]

addopts = --browser chromium --browser firefox

markers =

smoke: Smoke tests

regression: Full regression

@dataclass

class E2ETest:

name: str

browser: str

steps: int

duration_sec: float

screenshot: bool

status: str

e2e_tests = [

E2ETest("Login Flow", "Chromium", 5, 3.2, True, "Pass"),

E2ETest("Registration", "Chromium", 8, 5.1, True, "Pass"),

E2ETest("Product Search", "Chromium", 4, 2.8, True, "Pass"),

E2ETest("Add to Cart", "Firefox", 6, 4.5, True, "Pass"),

E2ETest("Checkout", "Chromium", 10, 8.3, True, "Pass"),

E2ETest("Payment", "Chromium", 7, 6.1, True, "Fail"),

E2ETest("Admin Dashboard", "Chromium", 5, 3.5, True, "Pass"),

]

CI/CD Pipeline

=== GitHub Actions CI/CD ===

.github/workflows/test.yml

name: Test Pipeline

on: [push, pull_request]

jobs:

unit-test:

runs-on: ubuntu-latest

steps:

  • uses: actions/checkout@v4
  • uses: actions/setup-python@v5

with: { python-version: '3.12' }

  • run: pip install -r requirements-test.txt
  • run: pytest tests/unit -v --cov --cov-report=xml
  • uses: codecov/codecov-action@v4

integration-test:

runs-on: ubuntu-latest

services:

postgres:

image: postgres:16

env: { POSTGRES_PASSWORD: test }

ports: ['5432:5432']

steps:

  • uses: actions/checkout@v4
  • run: pytest tests/integration -v

e2e-test:

runs-on: ubuntu-latest

needs: [unit-test, integration-test]

steps:

  • uses: actions/checkout@v4
  • run: pip install playwright pytest-playwright
  • run: playwright install --with-deps
  • run: pytest tests/e2e --headed=false

Test Strategy

test_strategy = {

"Unit Tests": "ทุก Push, ทุก PR — ต้อง Pass 100%",

"Integration Tests": "ทุก Push, ทุก PR — ต้อง Pass 100%",

"E2E Tests": "ทุก PR ไป main — ต้อง Pass 100%",

"Performance Tests": "Weekly Schedule — Alert ถ้า Regression",

"Security Tests": "Weekly + ก่อน Release",

"Visual Tests": "ทุก PR — Compare Screenshot",

"Code Coverage": "Target 80% — Block PR ถ้าต่ำกว่า",

}

for test, strategy in test_strategy.items():

Metrics

ci_metrics = {

"Avg Pipeline Duration": "8 minutes",

"Test Pass Rate (30d)": "98.5%",

"Code Coverage": "83%",

"Flaky Test Rate": "1.2%",

"PRs Blocked by Tests": "15/month",

"Bugs Caught by Tests": "28/month",

"Test Count Total": "420",

}

for k, v in ci_metrics.items():

เคล็ดลับ

  • Pyramid: Unit มากสุด Integration กลาง E2E น้อยสุด
  • CI: รัน Test ทุก Push อัตโนมัติ ไม่ข้าม
  • Coverage: ตั้งเป้า 80% Block PR ถ้าต่ำกว่า
  • Playwright: ใช้ Playwright สำหรับ E2E ดีกว่า Selenium
  • Flaky: แก้ Flaky Test ทันที ไม่ปล่อยผ่าน

Automation Testing คืออะไร

ทดสอบซอฟต์แวร์อัตโนมัติ ลดเวลา ลดข้อผิดพลาด ทำซ้ำ Regression Smoke Selenium Playwright Cypress Jest pytest CI/CD Pipeline

XM Legend · เทรดเดอร์ & ผู้สอน Forex 13 ปี

ผู้ก่อตั้ง SiamCafe ตั้งแต่ปี 1997 · เทรดเดอร์สาย Forex มากกว่า 13 ปี ได้รับการยกย่องเป็น XM Legend · แบ่งปันความรู้ Forex, ไอที, AI และการเทรด จากประสบการณ์จริงในตลาดจริง