0

我有一个使用 API 的类:

import requests


class APIClient:

    def __init__(self, base_url=None, refresh_token=None, client_id=None):
        self.base_url = base_url
        self.refresh_token = refresh_token
        self.client_id = client_id
        self.response = None

    def make_request(self):
        relative_url = f'{self.client_id}/session/confirm'
        params = {
            "refresh_token": f"{self.refresh_token}"
        }
        self.response = requests.post(self.base_url + relative_url, json=params, verify=False)

我有conftest.py

import pytest
from Project.tests.steps.api import APIClient


@pytest.fixture(scope="module")
def api_client():
    client = APIClient()
    yield client

我有 pytest-bdd 固定装置:

from pytest_bdd import given, then, when, parsers, scenario


@scenario('../features/api.feature', 'Get an access token')
def test_some():
    pass


@given(parsers.cfparse('client ID "{client_id:String}" and URL "{url:String}" and refresh token "{token:String}"',
                       extra_types=dict(String=str)))
def test_setup_method(api_client, client_id, url, token):
    api_client.client_id = client_id
    api_client.refresh_token = token
    api_client.base_url = url


@when("call request to get an access token")
def test_call_request(api_client):
    **api_client.make_request**()


@then("check than response result is OK 200 and contains an access token")
def test_check_token(api_client):
    assert api_client.response.status_code == 200, f"error code:{api_client.response.json()}"

结果,当我运行 pytest 时,我看到api_client.make_request调用了两次。

是什么原因,我应该怎么做才能避免它并且只调用一次api_client.make_request

4

0 回答 0