🎯
零门槛上手
自然语言风格的DSL语法,测试人员无需编程基础即可编写自动化测试
pip install pytest-dsluv pip install pytest-dslpytest-dsl 提供独立的 VS Code 扩展,可在扩展商店搜索 pytest-dsl 或直接安装:
code --install-extension felix-1991.pytest-dsl-support在VS Code中按 Ctrl+Shift+X 打开扩展商店
搜索 "pytest-dsl" 并安装UI 测试能力由独立扩展包提供,需要额外安装:
pip install pytest-dsl-ui
#安装playwright依赖
playwright install创建文件 hello.dsl:
@name: "我的第一个测试"
@description: "学习pytest-dsl的第一步"
# 定义变量
message = "Hello, pytest-dsl!"
count = 3
# 打印欢迎消息
[打印], 内容: ${message}
# 简单循环
for i in range(1, ${count} + 1) do
[打印], 内容: "第 ${i} 次循环"
end
# 测试断言
[断言], 条件: "${count} == 3", 消息: "计数器应该等于3"
teardown do
[打印], 内容: "测试完成!"
end# 直接运行DSL文件
pytest-dsl hello.dsl
# 运行目录下所有DSL文件
pytest-dsl tests/# 像写文档一样编写测试
[HTTP请求], 客户端: "default", 配置: '''
method: GET
url: https://api.example.com/users/1
asserts:
- ["status", "eq", 200]
- ["jsonpath", "$.name", "exists"]
'''@remote: "http://remote-server:8270/" as remote_machine
# 在远程机器上执行关键字
remote_machine|[HTTP请求], 客户端: "default", 配置: '''
method: GET
url: https://internal-api.example.com/data
'''# test_runner.py
from pytest_dsl.core.auto_decorator import auto_dsl
@auto_dsl("./tests")
class TestDSL:
"""自动将DSL目录转换为pytest测试"""
pass
# 使用pytest运行
# pytest test_runner.py -q# 通过 pytest-dsl-ui 扩展包提供的 UI 关键字进行浏览器自动化测试
[打开浏览器], 浏览器类型: "chrome", 无头模式: false
[访问页面], URL: "https://example.com"
[点击元素], 选择器: "#login-button"
[输入文本], 选择器: "input[name='username']", 文本: "admin"
[断言元素文本], 选择器: ".welcome-message", 期望文本: "欢迎回来"
[关闭浏览器]# 定义可复用的关键字
function 用户登录 (用户名, 密码="默认密码") do
[HTTP请求], 客户端: "default", 配置: '''
method: POST
url: /api/login
request:
json:
username: "${用户名}"
password: "${密码}"
captures:
token: ["jsonpath", "$.token"]
'''
return ${token}
end
# 使用自定义关键字
token = [用户登录], 用户名: "admin"@data: "test_data.csv" using csv
# CSV中的每一行都会执行一次测试
[HTTP请求], 客户端: "default", 配置: '''
method: POST
url: /api/login
request:
json:
username: "${username}"
password: "${password}"
asserts:
- ["status", "eq", ${expected_status}]
'''以下能力来自独立的 VS Code 扩展,而非当前仓库主包直接内置:
.dsl - pytest-dsl测试文件.pytest-dsl - pytest-dsl测试文件(备选扩展名)完整的HTTP测试支持,包括请求发送、响应捕获、断言验证
浏览器自动化测试,支持元素交互、页面导航和界面验证
跨服务调用、服务间通信和分布式系统测试,支持复杂的微服务架构验证
数据驱动和批量执行,轻松处理大量回归测试用例
跨系统测试协调,支持复杂的集成测试场景
VS Code插件支持,提升团队开发效率和代码质量