本文目录导读:

Python 代码执行
直接运行脚本
# 终端运行 python test.py python3 test.py # 带参数运行 python test.py --input data.txt
使用交互模式
# 进入交互环境
python
>>> print("Hello")
# 或执行单行代码
python -c "print('hello')"
JavaScript 代码执行
Node.js 环境
# 运行JS文件
node app.js
node --experimental-modules app.js
# 交互模式
node
> console.log("Hello")
浏览器控制台
<!-- 在HTML中嵌入 -->
<script>
console.log('测试代码');
// 你的测试代码
</script>
使用代码编辑器/IDE
VS Code
# 安装 Code Runner 插件 # 快捷键运行: - Windows: Ctrl+Alt+N - Mac: Ctrl+Option+N
常用配置
{
"code-runner.runInTerminal": true,
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.executorMap": {
"python": "python -u",
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}
}
Jupyter Notebook 测试
# 安装 pip install jupyter # 启动 jupyter notebook # 或 jupyter lab # 在单元格中逐段测试代码
在线测试转为本地
# 1. 创建测试文件
mkdir my_test
cd my_test
# 2. 初始化项目
npm init -y # Node项目
pip init # Python项目
# 3. 安装测试框架
npm install --save-dev jest # Node.js
pip install pytest # Python
# 4. 编写测试
# test_example.py
def test_function():
assert add(1, 2) == 3
# 5. 运行测试
npm test # Node.js
pytest # Python
实用技巧
快速测试文件
# test.py
if __name__ == "__main__":
# 这里的代码只在直接运行时执行
print("测试代码")
result = your_function()
print(result)
热重载开发
# nodemon 自动重启 npm install -g nodemon nodemon app.py # watch 模式 python -m watchdog app.py
使用虚拟环境
# 创建隔离环境 python -m venv venv source venv/bin/activate # Mac/Linux venv\Scripts\activate # Windows pip install -r requirements.txt
推荐实践
- 小型测试:使用 VS Code + Code Runner 插件
- 大型项目:使用 IDE(PyCharm/WebStorm)
- 数据处理:使用 Jupyter Notebook
- 单元测试:使用 pytest/jest 框架
- 快速调试:使用 pdb 或 node inspect
需要我详细演示某种语言的测试方法吗?
标签: 代码测试
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。