pytest文檔59-運行未提交git的用例(pytest-picked)


前言

我們每天寫完自動化用例后都會提交到 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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM