Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

需求: 对齐pytest的fixture作为函数参数 #1160

Open
zhengtong0898 opened this issue Feb 7, 2022 · 0 comments
Open

需求: 对齐pytest的fixture作为函数参数 #1160

zhengtong0898 opened this issue Feb 7, 2022 · 0 comments
Assignees
Labels
feature-pytest feature related to pytest
Milestone

Comments

@zhengtong0898
Copy link

zhengtong0898 commented Feb 7, 2022

背景描述

pytest 提供了 fixture 的特性,
并且允许在测试用例的入口函数处声明使用该fixture作为参数,
这是一个高价值特性, 需要被支持, pytest-fixture

期望的功能特性

期望可以将 fixture 当做为变量传递给实体函数.

可以替代 httprunner 的 setup_hooks 和 teardown_hooks 功能.

示例描述(可选)

conftest.py

@pytest.fixture(scope="session", autouse=True)
def session_fixture(request):                                                                                    
    """setup and teardown each task"""
    total_testcases_num = request.node.testscollected
    testcases = []
    for item in request.node.items:
        testcase = {
            "name": item.cls.config.name,
            "path": item.cls.config.path,
            "node_id": item.nodeid,
        }
        testcases.append(testcase)

    logger.debug(f"collected {total_testcases_num} testcases: {testcases}")

    yield testcases                                                                                                         # 返回fixture

    logger.debug(f"teardown task fixture")

    # teardown task
    # TODO: upload task summary

request_with_fixtures.yml

config:
    name: "request with fixture: session fixture"
    base_url: "https://postman-echo.com"
    usefixtures: ["session_fixture"]

teststeps:
-
    name: get with params
    request:
        method: GET
        url: /get
        params:
            info: "ip"
        headers:
            User-Agent: HttpRunner/${get_httprunner_version()}/${get_session_fixture($session_fixture)}

request_with_fixtures_test.py

# NOTE: Generated By HttpRunner v3.1.6
# FROM: request_methods/request_with_fixtures.yml


import debugtalk
import pytest


import allure


from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase


class TestCaseRequestWithFixtures(HttpRunner):
    @allure.title("request teststep: with error retry")
    def test_start(self, session_fixture):
        scope, fixtures = locals(), ["session_fixture"]
        values = map(lambda x: scope.get(x), fixtures)
        super().test_start(fixtures=dict(zip(fixtures, values)))

    config = Config("request teststep: with error retry").base_url(
        "https://postman-echo.com"
    )

    teststeps = [
        Step(
            RunRequest("get with params")
            .get("/get")
            .with_params(**{"info": "ip"})
            .with_headers(
                **{
                    "User-Agent": "HttpRunner/${get_httprunner_version()}/${get_session_fixture($session_fixture)}"
                }
            )
        ),
    ]


if __name__ == "__main__":
    TestCaseRequestWithFixtures().test_start()
@zhengtong0898 zhengtong0898 added the pending pending for confirmation label Feb 7, 2022
zhengtong0898 added a commit to zhengtong0898/httprunner that referenced this issue Feb 7, 2022
zhengtong0898 added a commit to zhengtong0898/httprunner that referenced this issue Feb 9, 2022
zhengtong0898 added a commit to zhengtong0898/httprunner that referenced this issue Feb 9, 2022
@debugtalk debugtalk added feature-pytest feature related to pytest and removed pending pending for confirmation labels Mar 24, 2022
@debugtalk debugtalk added this to the v4.1 milestone May 3, 2022
@debugtalk debugtalk modified the milestones: v4.1, v4.2 May 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-pytest feature related to pytest
Projects
Status: Todo
Development

No branches or pull requests

2 participants