0

I am automating an api using pytest bdd.I need to implement @pytest.mark.xfail for one of my step definition. But after adding this decorator, its not working as expected.

Example >

example.feature
Scenario: Validate the API response where availableLicense should not be greater than the totalLicense.
Given Send valid input
Then validate the availableLicense count should not be greater than totalLicense.

test_example.py
@given('Send valid input')
def valid_data(context, url,token_url, api_key):
    context.response = api_req('get',url, token_url, api_key)

@then('validate the availableLicense count should not be greater than totalLicense.')
@pytest.mark.xfail
def validate_license_count(context):
      <some logic>
      assert avail_license <= total_license

Still my test case is showing as failed when the above assertion got failed. What should I do here?

anirbans
  • 19
  • 1
  • 1
  • 2

1 Answers1

0

You need to add @pytest.mark.xfail to the pytest-bdd scenario test function that has the @scenario decorator, instead of a certain pytest-bdd step implementation.

So for your example it would be something like the following:

Scenario: Validate the API response where available License should not be greater than the total License.
    Given Send valid input
    Then validate the available License count should not be greater than total License.
@pytest.mark.xfail
@scenario('Validate the API response where available License should not be greater than the total License.')
def test_scenario():

@given('Send valid input')
def valid_data(context, url,token_url, api_key):
    context.response = api_req('get',url, token_url, api_key)

@then('validate the availableLicense count should not be greater than totalLicense.')
@pytest.mark.xfail
def validate_license_count(context):
      <some logic>
      assert avail_license <= total_license

Note: By default xfail does not let the testsuite fail even when the decorated test succeeds. In order to make the testsuite fail in that case, you have to use pytest.mark.xfail(strict=True) (see xfail documentation).

mattefrank
  • 231
  • 1
  • 9