pytest自动化测试-Git中的测试用例运行

前言

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

–picked 参数

我们在已提交过 git 仓库的用例里面新增了 2 个文件 test_new.py 和 test_new_2.py

cd到项目根目录,使用 git status 查看当前分支状态

  1. >git status

  2. On branch master

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

  4. Changes to be committed:

  5. (use "git reset HEAD <file>..." to unstage)

  6. new file: pytest_demo/test_new.py

  7. new file: pytest_demo/test_new_2.py

  8. Changes not staged for commit:

  9. (use "git add <file>..." to update what will be committed)

  10. (use "git checkout -- <file>..." to discard changes in working directory)

  11. modified: pytest_demo/test_new.py

  12. modified: pytest_demo/test_new_2.py

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

  1. Changed test files

  2. >pytest --picked

  3. ... 2. ['pytest_demo/test_new.py', 'pytest_demo/test_new_2.py']

  4. Changed test folders... 0. []

  5. ================================================= test session starts =================================================

  6. platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1

  7. Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>

  8. rootdir: D:\demo\code\xuexi_pytest

  9. collected 4 items

  10. pytest_demo\test_new.py .. [ 50%]

  11. pytest_demo\test_new_2.py .. [100%]

  12. ================================================== 4 passed in 0.20s ==================================================

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

–picked=first

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

–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. >git status

  2. On branch master

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

  4. Changes to be committed:

  5. (use "git reset HEAD <file>..." to unstage)

  6. new file: pytest_demo/test_new.py

  7. new file: pytest_demo/test_new_2.py

  8. Changes not staged for commit:

  9. (use "git add <file>..." to update what will be committed)

  10. (use "git checkout -- <file>..." to discard changes in working directory)

  11. modified: pytest_demo/test_new.py

  12. modified: pytest_demo/test_new_2.py

  13. Untracked files:

  14. (use "git add <file>..." to include in what will be committed)

  15. .idea/

  16. pytest_demo/__pycache__/

  17. 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 ==================================================

总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

 

          视频文档获取方式:
这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方小卡片即可自行领取。

  • 16
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值