Pytest learning ---- pytest operation mode and pre post packaging of interface automation test

1, Pytest benefits awareness:

1. It can combine all automatic test tools
2. Skip the failed case and rerun the failed case
3. Combined with allure production report
4. Continuous integration with Jenkins
5. Many powerful plug-ins

 pytest-html: production html Test report
 pytest-xdist: Multithreading
 pytest-ordering: Change the execution sequence of use cases
 pytest-rerunfailures: Failed case re crawl
 allure-pytest:Aesthetic test report

In general projects, we will use requests Textdocument saves the plug-in name for batch one-time installation

pip install -r requerments.txt

2, Operation mode:

1. Operation mode of main function: main method
2. Command operation mode
pytest -vs
-v: More details
-s: Debugging information
-n = processing: multithreaded operation
--Reruns = number: failed case reruns
--Reruns = number: failed case reruns
--html=./report.html: generate HTML report

Use case grouping operation

1. Use case grouping:
2. Annotate the use case:

#@pytest.mark. The group name is as follows:
@pytest.mark.smoke

[pytest]
##Run the command, for example: - vs -m "smoke" group execution name is fixed
addopts = -vs 
#Test case file directory
testpaths = ./testcases
python_files = test_*.py
python_classes = Test*
python_functions = test_*
##grouping
markers =
    smoke:maoyan
    case:gongneng

3, Front and rear fixture

1. Simple distinction: it is troublesome to call methods directly, but there are too many interfaces

    def setup(self):
        print("Each use case is executed once before execution")
    def teardown(self):
        print("After each use case is executed, it is executed again")

2. Implement partial prepositioning: if you only want to prepose one use case, for example, you need to connect to the database when logging in.

The following devices are required:

Parameter introduction:

@pytest.fixture(scope = "scope", params = "data driven", autouse = "auto execute", ids = "custom parameter", name = "Rename")
Scope: functions, classes, modules, packages, session s

usage method:

1. Label the device on the function that needs to be preceded
2. Call the device between other methods and functions

As follows: partial pre wake-up is performed in a file

import time

import pytest
import requests
#Realize the device label pre labeling, label, and yieid wake-up return
@pytest.fixture(scope="function")
def conn_getbase():
    print("Successfully connected to database")
    yield
    print("Database closed successfully")

class TestSendRequsets:
    #When there are too many interfaces, it is troublesome and redundant
    # def setup(self):
    #     print("before each use case is executed")
    #
    # def teardown(self):
    #     print("after each use case is executed")

    def test_getImgCode(self):
        # Interface url
        t = time.time()
        timess = str(int(round(t * 1000)))
        times = str(int(t))
        url = "http://124.71.230.185:9002/jeecg-boot/sys/randomImage/" + "" + timess
        # parameter
        data = {
            "_t": times,
        }
        # # get request
        rep = requests.request('get', url, params=data)
        print(rep.text)

    # Group use cases labeled smoke
    @pytest.mark.smoke
    def test_Login(self,conn_getbase):
        # post request
        url = "http://124.71.230.185:9002/jeecg-boot/sys/login"
        # parameter
        data = {
            "captcha": "Gkak!@#2021",
            "checkKey": 1637811815838,
            "password": "123456",
            "remember_me": 1,
            "username": "admin"
        }
        rep = requests.request('post', url, json=data)
        statues = rep.json()["success"]
        message = rep.json()["message"]
        if statues:
            print("")
        else:
            # raise Exception(message)
            print(message)


if __name__ == '__main__':
    pytest.main();

3. Encapsulation and flexible call

In general: @ pytest Fixture() will conftest.py Files are used together

conftest. The PY name is fixed, and its functions are as follows:

1. It is used to share pre configuration among multiple py files.
2. When calling the methods inside, they do not need to be imported and can be called between
3. You can have multiple confists Py files can also have different levels

conftest. See the next chapter for details of Py file

realization:

1. Create a confitest.exe between directories Py file
2. Paste the above code into conf test Py file

# Pre function
import pytest

@pytest.fixture(scope="function")
def conn_getbase():
    print("Database connection succeeded")
    yield
    print("Database closed successfully")

The following is the supporting materials. For friends who do [software testing], it should be the most comprehensive and complete war preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

Finally, it can be in the official account: programmer Hao! Get a 216 page interview document of Software Test Engineer for free. And the corresponding video learning tutorials for free!, It includes basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, test advanced Python programming, Web automation test, APP automation test, interface automation test, advanced continuous integration of test, test architecture, development test framework, performance test, security test, etc.

If my blog is helpful to you and you like my blog content, please click "like", "comment" and "collect" for three times! Friends who like software testing can join our testing technology exchange group: 779450660 (there are various software testing resources and technical discussions)

Keywords: Python software testing pytest

Added by lakilevi on Sat, 12 Feb 2022 01:08:32 +0200