Practical dry goods, pytest automated testing - test case operation in Git (detailed)


foreword

After we finish writing automation use cases every day, we will submit them to the git warehouse. With the increase of use cases, in order to ensure the cleanness of the warehouse code, when new use cases are added, we hope to only run the newly added use cases that have not been submitted to the git warehouse.

The pytest-picked plugin can only run code that has not been submitted to the git repository.

pytest-picked

Install using the command line

pip install pytest-picked

Available parameters

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

Example usage:

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

--picked parameter

We have added two new files test_new.py and test_new_2.py in the use case that has been submitted to the git warehouse

cd to the project root directory, use git status to view the current branch status

>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

You can see that there are 2 files, use pytest --picked to run the use case

Changed test files
>pytest --picked

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

All tests will be run from files and folders that have been modified but not yet committed.

–picked=first

Tests in the modified test file are run first, followed by all unmodified tests

>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 has 2 parameters optional unstaged, branch, the default is –mode=unstaged

2 states of git files

Untrack is not added to the new file in git
unstaged staged: staged state, unstage is the unstaged state, that is, the file that has not been added by git.
First figure out what is the untrack state. When we open the git project in pycharm, add a new file When , a query box will pop up: whether to add to the git file

If you choose Yes, the file will turn green, that is, the unstage state (not git added); if you choose No, it is a new file that has not been added to the git directory of the current branch, and the file color is brown.
git status View the status of the current branch, you will see that pytest_demo/test_3.py is Untracked files

>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

Running pytest --picked will execute all Untracked files and not staged files by default, the default is --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 ==================================================

If we only need to run the files that have been staged on the current branch but have not yet been submitted (excluding Untracked files), use git diff to view the difference of the branch code

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

Run pytest --picked --mode=branch to run the code that has been staged but not yet submitted on the 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 ==================================================
The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Every effort is worth remembering, and every persistence will become the cornerstone of victory. As long as you maintain the courage and confidence to move forward, you will be able to surpass yourself and realize even greater dreams. No matter where you are, you must dare to explore the unknown and meet challenges. Resolutely move towards the road of success, let hard work and struggle become the background color of your life!

There is a long way to go, only continuous struggle; there are many challenges, seize the day and night; success depends on strength, and hard work can achieve dreams; down-to-earth, go forward bravely; any difficulty can be overcome, as long as you have a dream in your heart!

Everyone has their own life path. Although the future is uncertain, as long as you have a dream in your heart and keep working hard, you can overcome obstacles and move towards the other side of success. On the road of pursuing our dreams, we need to strengthen our beliefs and move forward courageously in order to realize our self-worth and ideals in life.

Guess you like

Origin blog.csdn.net/x2waiwai/article/details/131130150