1

I tried to make a design for my API Python Project with pytest-bdd. I made "common, features, test" folders.

And in the feature file, I added my post, delete , get, put cases. I also seperated post.feature delete.feature etc.. due to there are lost of cases.

And then I generated my post_steps.py from the feature files. And There are some common steps in all steps pages. and so I decided to put them to commonsteps.py under the common folder.

Then,in the common.py, There some common methods as in the below asserts status code:

  @then('Status code of response should be 200')
    def status_code_of_response_should_be_200():
        assert status_code == 200
     

My tests starts with mypost.py and then starts to use this common methods but How can I pass response,status code to this page ? because I need to verify . In a nutshell, How Can I get response as a parameter from different steps page?enter image description here

enter image description here

Emily
  • 43
  • 6

1 Answers1

1

I'm not quite sure what you actually try to achieve... I assume you want to know how to pass the response object you receive in step When Execute PostAPI method with valid data to the step Then Status code of response should be 200.

There are multiple solutions, that use the same concept:

  1. Create a "context" fixture and use it as parameter in step When Execute PostAPI method with valid data and Then Status code of response should be 200 and store the received response in there.
  2. Create a fixture for the HTTP client you use in When Execute PostAPI method with valid data and pass that as parameter in step Then Status code of response should be 200. Then you can access the HTTP client there and presumably the response as well.

Examples:

Use a context object

@pytest.fixture
def step_context():
    return {'response': None}

@when("Execute PostAPI method with valid data")
def do_post(step_context):
    # Do HTTP request and retrieve response here ...
    step_context['response'] = response

@then("Status code of response should be 200")
def assert_status(step_context):
    assert step_context['response'].status_code == 200

HTTP client as fixture

@pytest.fixture
def http_client():
    return HTTPClient()

@when("Execute PostAPI method with valid data")
def do_post(http_client):
    # Do HTTP request and retrieve response here ...
    
@then("Status code of response should be 200")
def assert_status(http_client):
    assert http_client.status_code == 200

mattefrank
  • 231
  • 1
  • 9