网站首页 > 精选文章 正文
前言
我们每天写完自动化用例后都会提交到 git 仓库,随着用例的增多,为了保证仓库代码的干净,当有用例新增的时候,我们希望只运行新增的未提交 git 仓库的用例。
pytest-picked 插件可以实现只运行未提交到git仓库的代码。
pytest-picked
使用命令行安装
pip install pytest-picked
可使用参数
picked:
--picked=[{only,first}]
Run the tests related to the changed files either on their own, or first
--mode=PICKED_MODE Options: unstaged, branch
使用示例:
$ pytest --picked
$ pytest --picked=first
$ pytest --picked --mode=branch
$ pytest --picked --mode=unstaged # default
github仓库地址https://github.com/anapaulagomes/pytest-picked
--picked 参数
我们在已提交过 git 仓库的用例里面新增了 2 个文件 test_new.py 和 test_new_2.py
cd到项目根目录,使用 git status 查看当前分支状态
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: pytest_demo/test_new.py
new file: pytest_demo/test_new_2.py
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: pytest_demo/test_new.py
modified: pytest_demo/test_new_2.py
可以看到有2个文件,使用 pytest --picked 运行用例
>pytest --picked
Changed test files... 2. ['pytest_demo/test_new.py', 'pytest_demo/test_new_2.py']
Changed test folders... 0. []
================================================= test session starts =================================================
platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
rootdir: D:\demo\code\xuexi_pytest
collected 4 items
pytest_demo\test_new.py .. [ 50%]
pytest_demo\test_new_2.py .. [100%]
================================================== 4 passed in 0.20s ==================================================
所有测试都将从已修改但尚未提交的文件和文件夹中运行。
--picked=first
首先运行修改后的测试文件中的测试,然后运行所有未修改的测试
>pytest --picked=first
================================================= test session starts =================================================
platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\demo\code\xuexi_pytest
collected 11 items
pytest_demo\test_new.py .. [ 18%]
pytest_demo\test_new_2.py .. [ 36%]
pytest_demo\test_b.py ...... [ 90%]
pytest_demo\test_c.py . [100%]
================================================= 11 passed in 0.10s ==================================================
--mode=PICKED_MODE
--mode 有2个参数可选 unstaged, branch, 默认是--mode=unstaged
git 文件的2个状态
- untrack 没加到git里面的新文件
- unstaged staged:暂存状态, unstage就是未暂存状态,也就是没git add 过的文件
先弄清楚什么是 untrack 状态,当我们 pycharm 打开 git 项目,新增一个文件的时候,会弹出询问框:是否加到 git 文件
如果选择是,文件会变绿色,也就是 unstage 状态(没git add 过);选择否,那就是一个新文件,未被加到当前分支的 git 目录里面,文件颜色是棕色。
git status 查看当前分支的状态,此时会看到 pytest_demo/test_3.py 是 Untracked files
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: pytest_demo/test_new.py
new file: pytest_demo/test_new_2.py
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: pytest_demo/test_new.py
modified: pytest_demo/test_new_2.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
.idea/
pytest_demo/__pycache__/
pytest_demo/test_3.py
运行 pytest --picked 会默认执行所有的 Untracked 文件和 not staged 文件,默认是--mode=unstaged。
>pytest --picked
Changed test files... 3. ['pytest_demo/test_new.py', 'pytest_demo/test_new_2.py', 'pytest_demo/test_3.py']
Changed test folders... 2. ['.idea/', 'pytest_demo/__pycache__/']
================================================= test session starts =================================================
platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
collected 5 items
pytest_demo\test_new.py .. [ 40%]
pytest_demo\test_new_2.py .. [ 80%]
pytest_demo\test_3.py . [100%]
================================================== 5 passed in 0.06s ==================================================
如果我们只需运行当前分支上已经被暂存,但尚未提交的文件(不包含 Untracked files),使用 git diff 查看分支代码的差异
>git diff --name-only master
pytest_demo/test_new.py
pytest_demo/test_new_2.py
运行 pytest --picked --mode=branch, 运行分支上已经被暂存但尚未提交的代码
>pytest --picked --mode=branch
Changed test files... 2. ['pytest_demo/test_new.py', 'pytest_demo/test_new_2.py']
Changed test folders... 0. []
================================================= test session starts =================================================
platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
collected 4 items
pytest_demo\test_new.py .. [ 50%]
pytest_demo\test_new_2.py .. [100%]
================================================== 4 passed in 0.04s ==================================================
猜你喜欢
- 2025-02-03 IDEA图文使用教程-汇总(idea详细使用教程)
- 2025-02-03 gitlab上面新建工程的操作指引(gitlab新建project)
- 2025-02-03 基于Docker构建安装Git/GitLab,以及制作springboot工程镜像
- 2025-02-03 IDEA 2021首个大版本发布,Java开发者感动哭了(附新亮点演示)
- 2025-02-03 IntelliJ IDEA中的神仙插件 写代码必备
- 2025-02-03 善用Git的stash和unstash,让你的idea工作的更加丝滑起来
- 2025-02-03 java开发工具IntelliJ IDEA中使用 Git Blame 进行注释教程
- 2025-02-03 如何规范你的Git commit?(如何规范你的工作并且确定有时间休息和放松)
- 2025-02-03 idea提交的信息有误,想撤销怎么办?
- 2025-02-03 idea中使用git提示输入密码的解决办法
- 最近发表
- 标签列表
-
- 向日葵无法连接服务器 (32)
- git.exe (33)
- vscode更新 (34)
- dev c (33)
- git ignore命令 (32)
- gitlab提交代码步骤 (37)
- java update (36)
- vue debug (34)
- vue blur (32)
- vscode导入vue项目 (33)
- vue chart (32)
- vue cms (32)
- 大雅数据库 (34)
- 技术迭代 (37)
- 同一局域网 (33)
- github拒绝连接 (33)
- vscode php插件 (32)
- vue注释快捷键 (32)
- linux ssr (33)
- 微端服务器 (35)
- 导航猫 (32)
- 获取当前时间年月日 (33)
- stp软件 (33)
- http下载文件 (33)
- linux bt下载 (33)