Python automated testing: pytest implements keyword drive

In the last article, I wrote a very simple keyword-driven program, but this program just ran through the function, and there are still many places that can be optimized. In this article, I want to use pytest to simplify the writing of automated test cases, using relatively basic pytest functions. In the next article, I will write a complex version, directly executing the yaml file from the bottom layer as a use case.

Use Case Before Optimization

Before optimization, if you want to add a use case, you first need to write a yaml file, and then write a python automated test case, the code of the use case is as follows:

def test_keyword(driver):
    """获取 yaml 文件"""
    with open('signin.yaml', encoding='utf-8') as f:
        steps = yaml.safe_load(f)
        page = Page(driver)

    for step in steps:
        action_name = step.get('action')
        params = step.get('params')
        action = getattr(page, action_name)
        action(**params)

Although this program is relatively simple to use, if you want to create other use cases, you only need to copy this function once, modify the signin.yaml file name, and other codes do not need to be moved, but it is not yet to the point where you don’t need to use your brain. Copying so many repeated codes every time is not good-looking.

Optimized Use Case

import pytest

@pytest.mark.yaml_case('signin.yaml')
def test_keyword():
	pass

The optimized use case is significantly simpler, not even a single line of code in the function body. The configuration of the yaml file is configured above the test function in the form of a decorator, which is clearer and easy to find when modifying.

Implementation

The implementation method actually only uses two knowledge points of pytest: mark and fixture, first look at the code:

# conftest.py
import pytest
import yaml
from selenium import webdriver
from keyworks import Page

@pytest.fixture
def driver():
    d = webdriver.Chrome()
    d.implicitly_wait(8)
    d.maximize_window()
    yield d
    d.quit()

@pytest.fixture
def page(driver):
    """获取page"""
    return Page(driver)

@pytest.fixture(autouse=True)
def yaml_case(page, request):
    """yaml 测试步骤"""
    yaml_marker = request.node.get_closest_marker('yaml_case')
    yaml_file, *_ = yaml_marker.args
    with open(yaml_file, encoding='utf-8') as f:
        steps = yaml.safe_load(f)
        for step in steps:
            action = getattr(page, step['action'])
            action(**step['params'])

The focus is on the last fixture. First of all, I set the yaml_case fixture to be used automatically, so that I don't need to manually call it in the test function, so I don't need to pass in any parameters in the use case function.

In the yaml_case fixture, the first line of code request.node.get_closest_marker('yaml_case')gets the mark of yaml_case, and the second line of code yaml_marker.argsgets the parameter in the mark, which is the path of the file signin.yaml. Next, read the test steps in this file and call the specific execution operation. The calling code has been mentioned in the previous article. If you have any doubts, you can go back and have a look.

@pytest.mark.yaml_case('signin.yaml')
def test_keyword():
	pass

Summarize

The implementation of this code mainly uses pytest's flexible mark mechanism and fixture management. As long as pytest is used proficiently, it is not difficult to implement. If you have any questions or suggestions, welcome to private message me to discuss together.

If the article is helpful to you, remember to like, bookmark, and add attention. I will share some dry goods from time to time...

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

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/myh919/article/details/131810903