前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest报错_git是什么

pytest报错_git是什么

作者头像
全栈程序员站长
发布2022-09-16 11:58:22
2540
发布2022-09-16 11:58:22
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

前言

我们每天写完自动化用例后都会提交到 git 仓库,随着用例的增多,为了保证仓库代码的干净,当有用例新增的时候,我们希望只运行新增的未提交 git 仓库的用例。pytest-picked 插件可以实现只运行未提交到git仓库的代码。

安装

代码语言:javascript
复制
pip3 install pytest-picked

使用示例

代码语言:javascript
复制
$ pytest --picked

$ pytest --picked=first

$ pytest --picked --mode=branch

$ pytest --picked --mode=unstaged  # default

$ pytest --picked --mode=branch --parent-branch=main # if your parent branch differs from "master"

–picked 参数

我们在已提交过 git 仓库的用例里面新增了 1 个文件 test_new.py cd到项目根目录,使用git status查看当前分支状态

代码语言:javascript
复制
> git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   case/test_new.py

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   case/test_new.py

可以看到有1个文件,使用 pytest –picked 运行用例

代码语言:javascript
复制
Changed test files... 1. ['case/test_new.py']
collecting ... 
 case/test_new.py ✓                                                                                                                                                                                      100% ██████████

Results (0.04s):
       1 passed

–picked=first

首先运行修改后的测试文件中的测试,然后运行所有未修改的测试

–mode=PICKED_MODE

–mode 有2个参数可选 unstaged, branch, 默认是–mode=unstaged

git 文件的2个状态

  • untrack 没加到git里面的新文件
  • unstaged staged:暂存状态, unstage就是未暂存状态,也就是没git add 过的文件 先弄清楚什么是 untrack 状态,当我们 pycharm 打开 git 项目,新增一个文件的时候,会弹出询问框:是否加到 git 文件
pytest报错_git是什么
pytest报错_git是什么

如果选择是,文件会变绿色,也就是 unstage 状态(没git add 过);选择否,那就是一个新文件,未被加到当前分支的 git 目录里面,文件颜色是棕色。 git status 查看当前分支的状态,此时会看到 case/test_new2.py 是 Untracked files

代码语言:javascript
复制
> git status

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   case/test_new.py

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   case/test_new.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        case/test_new2.py

运行 pytest –picked 会默认执行所有的 Untracked 文件和 not staged 文件,默认是–mode=unstaged。

代码语言:javascript
复制
collecting ... 
 case/test_new2.py::test_2 ✓                                                                                                                                                                              50% █████     
 case/test_new.py::test_1 ✓                                                                                                                                                                              100% ██████████

如果我们只需运行当前分支上已经被暂存,但尚未提交的文件(不包含 Untracked files) 运行 pytest --picked --mode=branch, 运行分支上已经被暂存但尚未提交的代码

代码语言:javascript
复制
(pytest_env) ➜  apitest git:(master) ✗ pytest --picked --mode=branch

Changed test files... 1. ['case/test_new.py']
Changed test folders... 0. []
collecting ... 
 case/test_new.py::test_1 ✓                                                                                                                                                                              100% ██████████

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/166263.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 安装
  • 使用示例
  • –picked 参数
  • –picked=first
  • –mode=PICKED_MODE
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档