前言

我们每天写完自动化用例后都会提交到 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 ==================================================

作者-上海悠悠 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. shell 设置进程数运行

    问题描述 在服务器上提交任务时,需要限制运行的核的数目.程序本身是单线程的,但是不同的输入参数需要跑很多组,粗暴的方法是开多个终端,不断地去提交任务.但这比较麻烦,可以用 shell 实现. 基础 首 ...

  2. Qt Qgis 二次开发——鼠标点击识别矢量要素

    Qt Qgis 二次开发--鼠标点击识别矢量要素 介绍: 识别矢量要素需要用到QGis的一个工具类:QgsMapToolIdentifyFeature 一个QgsMapTool的子类的子类,官方文档描 ...

  3. [LeetCode]678. 有效的括号字符串、20. 有效的括号(栈)

    题目 678. 有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何 ...

  4. [LeetCode]Sql系列2

    题目 1205. 每月交易II Transactions 记录表 +----------------+---------+ | Column Name | Type | +-------------- ...

  5. 使用阿里云OSS的服务端签名后直传功能

    网站一般都会有上传功能,而对象存储服务oss是一个很好的选择.可以快速的搭建起自己的上传文件功能. 该文章以使用阿里云的OSS功能为例,记录如何在客户端使用阿里云的对象存储服务. 服务端签名后直传 背 ...

  6. SpringMVC-11-文件上传和下载

    11. 文件上传和下载 准备工作 ​ springMVC可以很好的支持文件上传,但是SpringMVC上下文默认没有装配MultipartResolver,因此默认情况下不能处理文件上传工作.如果想实 ...

  7. 云计算openstack核心组件——horizon Web管理界面(10)

    一.horizon 介绍: 理解 horizon Horizon 为 Openstack 提供一个 WEB 前端的管理界面 (UI 服务 )通过 Horizone 所提供的 DashBoard 服务 ...

  8. Python列出指定目录下的子目录/文件或者递归列出

    1.python只列出当前目录(或者指定目录)下的文件或者目录条目 import os files,dirs=[],[] for item in os.listdir(): if os.path.is ...

  9. 针对python字符串有哪些操作呢?本文详解!

    1.1 字符串字符串就是一系列字符.在Python中,用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号.1.2 一些对字符串的操作(1)使用方法修改字符串的大小写name = " ...

  10. Java环境变量配置 新手必备

    第一步:安装JDK,无脑下一步 建议修改安装路径 这里以jdk1.7为例子(之前帮机房安装软件,五六十台电脑都要用1.7); 2.安装完了之后右击此电脑,打开属性 打开系统高级设置 打开环境变量 这里 ...