快速构建vscode pytest 开发测试环境

如果不想用 heavy 的pycharm

vscode 也是1个很好的选择

安装python SDK

复制代码
pacman -S python
[gateman@manjaro-x13 tmp]$ pacman -Q python
python 3.11.8-1

安装Vscode

很多中方法

bash 复制代码
yay -S visual-studio-code-bin
[gateman@manjaro-x13 tmp]$ pacman -Q | grep -i visual
visual-studio-code-bin 1.88.1-1

安装微软python 插件

一个microsoft python 就够来, 另外两个是依赖, 关联安装

新建1个空项目文件夹

我们就命名为python1 吧

bash 复制代码
[gateman@manjaro-x13 tmp]$ mkdir -p python1
[gateman@manjaro-x13 tmp]$ ls
python1

用vscode 打开这个文件夹

在welcome 页面新建 项目基本的虚拟环境

在项目内创建test folder

并在里面创建1个test类, 命名规则 test_xxx.py

配置vscode pytest 环境

配置之后可能会有error, 重新打开vscode, 就会见到pytest 已经配好

配置pytest configuration

在项目根目录创建1个pytest.ini

bash 复制代码
[pytest]
addopts = -s

这样pytest 就可以输出console里的输出

测试pytest

可以见到有hello world 输出

构建requirement.txt

执行

bash 复制代码
pip freeze > requirement.txt
bash 复制代码
(.venv) [gateman@manjaro-x13 python1]$ pip freeze > requirement.txt
(.venv) [gateman@manjaro-x13 python1]$ ls
main.py  pytest.ini  requirement.txt  test
(.venv) [gateman@manjaro-x13 python1]$ ls -l
total 12
-rw-r--r-- 1 gateman gateman    0 May  4 01:44 main.py
-rw-r--r-- 1 gateman gateman   21 May  4 01:53 pytest.ini
-rw-r--r-- 1 gateman gateman   61 May  4 01:56 requirement.txt
drwxr-xr-x 3 gateman gateman 4096 May  4 01:49 test
(.venv) [gateman@manjaro-x13 python1]$ cat requirement.txt 
iniconfig==2.0.0
packaging==24.0
pluggy==1.5.0
pytest==8.2.0

好了到一步配置基本完成, 下一步可以配置log 框架的什么的了, 建议这里先checkin 代码。

相关推荐
合作小小程序员小小店13 小时前
web网页,在线%食谱推荐系统%分析系统demo,基于vscode,uniapp,vue,java,jdk,springboot,mysql数据库
vue.js·spring boot·vscode·spring·uni-app
牧天白衣.14 小时前
快捷键——VsCode
vscode
蜕变的土豆15 小时前
Visual Studio 旧版软件下载教程
ide·visual studio
思则变18 小时前
[Pytest][Part 4]多种测试运行方式
pytest
金玉满堂@bj19 小时前
PyCharm 中 Python 解释器的添加选项及作用
ide·python·pycharm
程序员三藏19 小时前
如何使用Pytest进行测试?
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
通信小小昕20 小时前
ubuntu18.04.1无法安装vscode(安装依赖无效)
ide·vscode·编辑器
七月初七淮水竹亭~1 天前
Pycharm 报错 Environment location directory is not empty 如何解决
ide·python·pycharm
等不到来世2 天前
vscode设置默认终端terminal为git bash
vscode·terminal
green5+12 天前
yolo11n环境配置+实例运行【wsl+pycharm+conda】
ide·pycharm·conda