前言

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

pytest-picked 插件可以实现只运行未提交到git仓库的代码。

pytest-picked

使用命令行安装

pip install pytest-picked

可使用参数

  1. picked:
  2. --picked=[{only,first}]
  3. Run the tests related to the changed files either on their own, or first
  4. --mode=PICKED_MODE Options: unstaged, branch

使用示例:

  1. $ pytest --picked
  2. $ pytest --picked=first
  3. $ pytest --picked --mode=branch
  4. $ 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 查看当前分支状态

  1. # 作者-上海悠悠 QQ交流群:717225969
  2. # blog地址 https://www.cnblogs.com/yoyoketang/
  3. >git status
  4. On branch master
  5. Your branch is up-to-date with 'origin/master'.
  6. Changes to be committed:
  7. (use "git reset HEAD <file>..." to unstage)
  8. new file: pytest_demo/test_new.py
  9. new file: pytest_demo/test_new_2.py
  10. Changes not staged for commit:
  11. (use "git add <file>..." to update what will be committed)
  12. (use "git checkout -- <file>..." to discard changes in working directory)
  13. modified: pytest_demo/test_new.py
  14. modified: pytest_demo/test_new_2.py

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

  1. >pytest --picked
  2. Changed test files... 2. ['pytest_demo/test_new.py', 'pytest_demo/test_new_2.py']
  3. Changed test folders... 0. []
  4. ================================================= test session starts =================================================
  5. platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
  6. Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
  7. rootdir: D:\demo\code\xuexi_pytest
  8. collected 4 items
  9. pytest_demo\test_new.py .. [ 50%]
  10. pytest_demo\test_new_2.py .. [100%]
  11. ================================================== 4 passed in 0.20s ==================================================

所有测试都将从已修改但尚未提交的文件和文件夹中运行。

--picked=first

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

  1. >pytest --picked=first
  2. ================================================= test session starts =================================================
  3. platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
  4. rootdir: D:\demo\code\xuexi_pytest
  5. collected 11 items
  6. pytest_demo\test_new.py .. [ 18%]
  7. pytest_demo\test_new_2.py .. [ 36%]
  8. pytest_demo\test_b.py ...... [ 90%]
  9. pytest_demo\test_c.py . [100%]
  10. ================================================= 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

  1. # 作者-上海悠悠 QQ交流群:717225969
  2. # blog地址 https://www.cnblogs.com/yoyoketang/
  3. >git status
  4. On branch master
  5. Your branch is up-to-date with 'origin/master'.
  6. Changes to be committed:
  7. (use "git reset HEAD <file>..." to unstage)
  8. new file: pytest_demo/test_new.py
  9. new file: pytest_demo/test_new_2.py
  10. Changes not staged for commit:
  11. (use "git add <file>..." to update what will be committed)
  12. (use "git checkout -- <file>..." to discard changes in working directory)
  13. modified: pytest_demo/test_new.py
  14. modified: pytest_demo/test_new_2.py
  15. Untracked files:
  16. (use "git add <file>..." to include in what will be committed)
  17. .idea/
  18. pytest_demo/__pycache__/
  19. pytest_demo/test_3.py

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

  1. >pytest --picked
  2. Changed test files... 3. ['pytest_demo/test_new.py', 'pytest_demo/test_new_2.py', 'pytest_demo/test_3.py']
  3. Changed test folders... 2. ['.idea/', 'pytest_demo/__pycache__/']
  4. ================================================= test session starts =================================================
  5. platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
  6. collected 5 items
  7. pytest_demo\test_new.py .. [ 40%]
  8. pytest_demo\test_new_2.py .. [ 80%]
  9. pytest_demo\test_3.py . [100%]
  10. ================================================== 5 passed in 0.06s ==================================================

如果我们只需运行当前分支上已经被暂存,但尚未提交的文件(不包含 Untracked files),使用 git diff 查看分支代码的差异

  1. >git diff --name-only master
  2. pytest_demo/test_new.py
  3. pytest_demo/test_new_2.py

运行 pytest --picked --mode=branch, 运行分支上已经被暂存但尚未提交的代码

  1. >pytest --picked --mode=branch
  2. Changed test files... 2. ['pytest_demo/test_new.py', 'pytest_demo/test_new_2.py']
  3. Changed test folders... 0. []
  4. ================================================= test session starts =================================================
  5. platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
  6. collected 4 items
  7. pytest_demo\test_new.py .. [ 50%]
  8. pytest_demo\test_new_2.py .. [100%]
  9. ================================================== 4 passed in 0.04s ==================================================

作者-上海悠悠 QQ交流群:717225969

blog地址 https://www.cnblogs.com/yoyoketang/

pytest文档59-运行未提交git的用例(pytest-picked)的更多相关文章

  1. Pytest(17)运行未提交的git(pytest-picked)

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

  2. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  3. pytest文档50-命令行参数--durations统计用例运行时间

    前言 写完一个项目的自动化用例之后,发现有些用例运行较慢,影响整体的用例运行速度,于是领导说找出运行慢的那几个用例优化下. --durations 参数可以统计出每个用例运行的时间,对用例的时间做个排 ...

  4. pytest文档2-用例运行规则

    用例设计原则 文件名以test_*.py文件和*_test.py 以test_开头的函数 以Test开头的类 以test_开头的方法 所有的包pakege必须要有__init__.py文件 help帮 ...

  5. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  6. Error-Javascript:错误:页面文档类型(DOCTYPE)未声明!

    ylbtech-Error-Javascript:错误:页面文档类型(DOCTYPE)未声明! 1.返回顶部 1. HTML1300: 进行了导航.文件: TransferNote.aspxHTML1 ...

  7. pytest文档55-plugins插件开发

    前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...

  8. 魔改——MFC MDI程序 定制 文档模板 运行时全部打开 禁用关闭按钮

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  9. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

随机推荐

  1. latex 封面

    latex 封面 代码: \begin{titlepage} \heiti \vspace*{64pt} \begin{center} \fontsize{48pt}{0} XX大学\\ \vspac ...

  2. Effective Objective-C 的读书笔记

    本文主要是摘录了 <Effective Objective-C 2.0>一书中提到的编写高质量iOS 代码的几个方法. 1 熟悉Objective -C 1.1 OC 起源 OC 为C语言 ...

  3. First-blog:解决mybatis 用mysql进行模糊搜索时,查不了中文问题

    如图:点击小字 按搜索时,出现乱码搜索不了 解决办法:出现乱码问题,一般无非两种 1.是数据库问题 2.是服务器问题 我在MySQL命令行搜索时,中文可以实现,说明时服务器问题 通过修改 tomcat ...

  4. python之类方法和静态方法

    在类中定义的函数称为方法,主要有三种:实例方法.类方法.静态方法. class MyTest(): # 普通实例函数 def func1(self, arg1, arg2): pass # 类函数 @ ...

  5. MySQL的事务机制和锁(InnoDB引擎、MVCC多版本并发控制技术)

    一.事务(数据库的事务都通用的定义) 1.1 事务定义 事务是由一步或几步数据库操作序列组成逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行.事务通常以 BEGIN TRANSACTION 开始 ...

  6. 一道JavaScript的二维数组求平均数的题

    JavaScript中只支持一维数组,但是可以在数组中嵌套数组来创建二维以至于多维的数组.今天下午在看书时候,发现一道感觉比较有意思的题,就是js中如何求二维数组的列之和和行之和,现在就给大家分享下, ...

  7. 虚拟机栈(Java Stack)基础知识

    虚拟机栈即是程序运行时的单位,而堆是数据存储的单位.换句话说,栈解决的是程序运行的问题,即程序如何执行,如何处理数据,而堆是解决数据的存储问题,数据存在哪,放在哪 虚拟机栈细节如下图所示,其中当前栈帧 ...

  8. Java多线程--CAS

    在Java多线程并发的情况下同时对一个变量进行操作会出现线程安全的问题,假如我们现在使用20个线程对一个变量不停累加1,代码如下: 1 public class ThreadDemo implemen ...

  9. 安装MySQL和出现的问题解决

    在Windows下安装mysql,注意自己的Windows是32位还是64位. MySQL官网下载地址:https://dev.mysql.com/downloads/mysql/ 下载完之后,解压放 ...

  10. Oracle添加键值对盲注

    前言 遇到一种注入点,存在于POST参数中,却不能用sqlmap扫出: 分析 request参数格式: %24Q_value1=test1&orderCol=&order=+ASC+& ...