Sublime Text 本身不内置单元测试或集成测试功能,但可通过配置构建系统(如 pytest.sublime-build、jest.sublime-build)并安装插件(如 SublimeJEDI、ESLint-Formatter),配合 pytest 或 Jest 实现一键运行测试,需遵循框架规范编写用例并注意环境路径一致性。

Sublime Text 本身不内置单元测试或集成测试功能,但它可以作为编辑器高效配合 Jest(JavaScript)或 pytest(Python)运行测试。关键在于配置构建系统、安装必要插件,并遵循测试框架规范编写用例。
配置 Sublime 的测试运行环境
让 Sublime 能一键运行测试,需设置构建系统:
-
Pytest:在 Sublime 中选择 Tools → Build System → New Build System,粘贴以下内容并保存为
pytest.sublime-build:
{
"cmd": ["pytest", "$file", "-v"],
"selector": "source.python",
"working_dir": "${file_path}",
"variants": [
{
"name": "pytest with coverage",
"cmd": ["pytest", "$file", "--cov=.", "-v"]
}
]
}登录后复制
保存后,打开 Python 测试文件(如 test_example.py),按 Ctrl+B(Windows/Linux)或 Cmd+B(macOS)即可运行。
-
Jest:类似地新建
jest.sublime-build,内容如下(确保项目根目录含package.json和jest.config.js):
{
"cmd": ["npx", "jest", "$file", "--verbose"],
"selector": "source.js, source.jsx, source.ts, source.tsx",
"working_dir": "${project_path:${folder}}"
}登录后复制
推荐安装插件 SublimeJEDI(Python 智能提示)或 ESLint-Formatter(JS 代码校验),提升测试代码编写体验。
编写符合规范的 pytest 测试用例
pytest 要求测试文件以 test_*.py 或 *_test.py 命名,函数以 test_ 开头:
import pytest <p>def test_addition(): assert 1 + 1 == 2</p><p>def test_pide_by_zero(): with pytest.raises(ZeroDivisionError): 1 / 0</p><p>class TestCalculator: def test_multiply(self): assert 3 * 4 == 12
登录后复制
常用技巧:
标签: linux javascript python java sublime js json windows npm 编码
还木有评论哦,快来抢沙发吧~