一、Web自动化框架:Python+unittest+selenium

        组成说明:

        ● Python:基础编程语言,提供丰富的库支持。

        ● unittest:Python自带的测试框架,用于组织用例(TestCase类、setUp/tearDown初始化/清理)、执行测试。

        ● Selenium:Web自动化核心工具,通过WebDriver(如ChromeDriver)驱动浏览器,模拟用户操作(点击、输入、跳转等)。

        简单使用方法(登录示例):

import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By

class LoginTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://example.com/login")  # 打开登录页

    def test_login_success(self):
        self.driver.find_element(By.ID, "username").send_keys("testuser")
        self.driver.find_element(By.ID, "password").send_keys("testpass")
        self.driver.find_element(By.XPATH, "//button[text()='登录']").click()
        self.assertEqual(self.driver.title, "首页")  # 断言登录成功

    def tearDown(self):
        self.driver.quit()

        优点:成熟稳定、支持多浏览器、社区活跃;
        缺点:动态页面需处理等待、执行速度较慢;
        适用场景:Web应用功能测试、跨浏览器兼容性测试、回归测试。

二、接口自动化框架:Python+unittest+requests

        组成说明:

        ● Python:简洁易读的基础语言;

        ● unittest:组织用例,支持前置/后置操作;

        ● requests:发送HTTP请求,处理响应数据(JSON/XML)。

        简单使用方法(获取用户信息示例):

import unittest
import requests

class UserInfoTest(unittest.TestCase):
    def setUp(self):
        self.base_url = "https://example.com/api"
        self.headers = {"Authorization": "Bearer your_token"}  # 身份验证

    def test_get_user_info_success(self):
        response = requests.get(f"{self.base_url}/user/1", headers=self.headers)
        self.assertEqual(response.status_code, 200)  # 断言状态码
        self.assertEqual(response.json()["username"], "testuser")  # 断言用户名

    def tearDown(self):
        pass

        优点:上手容易、执行效率高、覆盖多种协议;
        缺点:测试报告基础、需手动处理接口依赖;
        适用场景:RESTful接口功能测试、回归测试、性能测试(结合locust)。

三、App自动化框架:Python+unittest+pytest+Appium

        组成说明:

        ● Python:基础语言;

        ● unittest/pytest:组织用例(pytest更灵活,支持参数化、fixtures);

        ● Appium:跨平台App驱动,支持iOS/Android。

        简单使用方法(App登录示例):

import pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

@pytest.fixture(scope="module")
def driver():
    desired_caps = {
        "platformName": "Android",
        "deviceName": "emulator-5554",
        "appPackage": "com.example.app",
        "appActivity": ".MainActivity"
    }
    driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
    yield driver
    driver.quit()

def test_app_login_success(driver):
    driver.find_element(AppiumBy.ID, "com.example.app:id/username_input").send_keys("testuser")
    driver.find_element(AppiumBy.ID, "com.example.app:id/password_input").send_keys("testpass")
    driver.find_element(AppiumBy.ID, "com.example.app:id/login_button").click()
    nickname = driver.find_element(AppiumBy.ID, "com.example.app:id/nickname_text").text
    assert nickname == "Test User"  # 断言昵称

        优点:跨平台支持、功能丰富(滑动/手势)、插件扩展(allure-pytest生成报告);
        缺点:配置复杂、设备兼容性问题、执行速度慢;
        适用场景:移动App功能测试、跨平台兼容性测试、回归测试。

四、差异性对比

维度

Web自动化

接口自动化

App自动化

测试对象

Web网页(浏览器)

HTTP接口(后端)

移动App(iOS/Android)

核心工具

Selenium+unittest

requests+unittest

Appium+pytest

复杂度

中等(需配置浏览器)

低(只需安装requests)

高(需配置Appium/SDK/设备)

执行速度

中等(比接口慢,比App快)

高(最快)

低(最慢)

主要优势

模拟真实用户操作

执行效率高

跨平台支持

适用场景

Web功能/兼容性测试

接口功能/回归测试

移动App功能/兼容性测试

💡 总结:

        三个框架分别针对不同的测试层级(前端Web、后端接口、移动App),选择时需根据测试目标(如验证前端交互 vs 后端逻辑)、技术门槛(如App自动化需配置设备)和效率要求(如接口测试更快)来决定。实际项目中,通常会结合使用这三个框架,覆盖从后端到前端的全流程测试。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐