Super detailed from entry to proficiency, pytest automated test framework combat - advanced operation of fixture firmware (11)


foreword

Parameterized fixtures

Fixture has a params parameter that allows us to pass data.
Grammar format:

Pytest automated testing framework: https://www.bilibili.com/video/BV18K411m7FH/

# conftest.py文件

# fixture的params参数
# 取value1时,会把依赖此fixture的用例执行一遍。
# 取value2时,会把依赖此fixture的用例执行一遍。
# 取value3时,会把依赖此fixture的用例执行一遍。
# params有几个参数,就会将依赖此fixture的用例执行几遍。
@pytest.fixture(params=[value1, value2, value3..])
def fix_name():
	# do something

When we need to call the fixture multiple times, we can use the parameterization function of the fixture.

But it is not concurrent, it is executed serially.

For example, test objects have multiple configurations, so parameterization can help us execute use cases in multiple configurations.

Next, take web page automation as an example.

Requirements: To execute test cases under google and firefox browsers, the use case is to open Baidu search pytest.

First define fixture in conftest.py and set params=[“google”, “firefox”]

# conftest.py

# params设置为google和firefox
@pytest.fixture(params=["google", "firefox"])
def browser_fix(request):
    if request.param == "google":
        driver = webdriver.Chrome()
    elif request.param == "firefox":
        driver = webdriver.Firefox()
    else:
        driver = None
    yield driver
    if driver:
        driver.quit()

In the test case file test_baidu_action.py, write the test case and call browser_fix

# test_baidu_action.py

@pytest.mark.usefixtures("browser_fix")
def test_baidu(browser_fix):
    driver = browser_fix
    driver.get("https://www.baidu.com/")
    driver.find_element(By.ID, "kw").send_keys("pytest", Keys.ENTER)
    loc = (By.XPATH, '//h3')
    WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc))
    driver.find_element(*loc).click()

Running the above use cases will be executed in google browser and then in firefox browser. There are 2 test cases in total.

Please add a picture description

fixture factory

When we need to call fixture multiple times in a use case, we can use fixture factory

Using the decorator method

Inside the fixture, define a function. What fixture returns is a function.

@pytest.fixture
def make_customer_record():
    def _make_customer_record(name):
        return {
    
    "name": name, "orders": []}

    return _make_customer_record

# 用例内部,多次调用了fixture.
def test_customer_records(make_customer_record):
    customer_1 = make_customer_record("Lisa")  # 第1次调用
    customer_2 = make_customer_record("Mike")  # 第2次调用
    customer_3 = make_customer_record("Meredith")  # 第3次调用

If the data created by the factory needs to be managed, then the fixtue can be handled as follows:

@pytest.fixture
def make_customer_record():
  
    # 管理工厂的数据。在前置中创建。在后置中销毁
    created_records = []

    def _make_customer_record(name):
        record = models.Customer(name=name, orders=[])
        # 前置中添加数据
        created_records.append(record)
        return record

    yield _make_customer_record  # 返回内部函数
  
    # 销毁数据
    for record in created_records:
        record.destroy()

# 测试用例
def test_customer_records(make_customer_record):
    customer_1 = make_customer_record("Lisa")
    customer_2 = make_customer_record("Mike")
    customer_3 = make_customer_record("Meredith")

request this fixture

The built-in fixture named requests of pytest, the main function: Provide the information of the test case/test class of the request fixture.
After we define the fixture, it is usually a test case/test class to request the fixture.
The request fixture will record the test case/test class related information.

The request fixture is implemented through FixtureRequest, and the following attributes (enumerated part) can be used:

request.param: Get the params parameter value of the fixture
request.scope: Get the scope of the fixture
request.function: Get the name of the use case function that calls the fixture. If the fixture is function-level scoped.
request.cls: Get the test class from which the test case is collected.
request.module: Get the python module from which the test case/test class is collected.
request.config: Get configuration information related to the current request from the config file of pytest

Since requests are fixtures, the fixtures we define can directly use requests as function parameters.

Below, a simple case is used to demonstrate.

Define a fixture that takes requests as parameters.

import pytest

@pytest.fixture(params=[1,2])
def init(request):
    print("用例名称:", request.function)
    print("fix参数 ", request.param)
    print("fix的作用域 ", request.scope)
    print("用例所在的类 ", request.cls)

Define a test class that directly requests the fixture named init:

@pytest.mark.usefixtures("init")
class TestABC:

    def test_hello(self):
        print("-------------------------")

The execution results are as follows:

Please add a picture description

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)

As long as you have a dream in your heart, don't stop moving forward. Even if the road is bumpy, we must move forward bravely. Because only by constantly striving can dreams become reality, and this is the most exciting part of life.

Every effort is a gift to your future. Let us bravely face challenges, embrace changes, keep learning and improving, become a better version of ourselves, and create our own wonderful life!

Don't be fooled by the setbacks in front of you, and don't get stuck in the comfortable status quo. Only by facing challenges bravely and fighting hard can we create our own brilliance on the road to the future.

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/130132835