Skip to main content

Plugin skips (xfail) tests if unresolved Jira issue(s) linked

Project description

pytest-jira-xfail

Tests

Plugin skips (xfail) tests linked to unresolved Jira issue(s)

1. Generate your Jira API token

You should have Jira user with API token generated

2. Add PytestJiraHelper to your pytest hook:

import pytest

from pytest_jira_xfail.jira_helper import PytestJiraHelper


@pytest.hookimpl(tryfirst=True)
def pytest_collection_modifyitems(items):
    jira = PytestJiraHelper(
        jira_url="https://company.atlassian.net",
        jira_username="my_jira_user@company.com",
        jira_api_token="my_jira_user_api_token",
    )
    jira.process_linked_jira_issues(items)

3. Link bugs to your tests

from pytest_jira_xfail.annotations import bug


@bug("MP-123")
def test_my_test_fails():
    assert False


@bug("MP-124", IndexError)
def test_my_test_broken():
    db_records = []
    assert db_records[0]


@bug("MP-124")
@bug("MP-124", IndexError)
def test_multiple_exceptions():
    db_records = []
    assert db_records[0][0] == 'active'

Skip the test entirely (never run it)

By default a test linked to an open bug is still executed and reported as XFAIL (so an unexpected pass shows up as XPASS). If the bug makes the test unsafe or pointless to run (e.g. it hangs, corrupts data, or blocks the suite), pass run=False to skip execution completely while the issue is open. The test is reported as XFAIL [NOTRUN] and its body is never executed:

from pytest_jira_xfail.annotations import bug


@bug("MP-123", run=False)
def test_not_executed_until_fixed():
    assert False  # never runs while MP-123 is open

Once the linked issue is resolved, the test runs normally again.

Match the error message, not only the error type

By default a test is treated as XFAIL whenever it raises the expected error type (raises). If the same error type can be raised for unrelated reasons, use error_contains to xfail the test only when the raised error message contains an expected substring (or one of several substrings). If the type matches but the message does not, the test is reported as a real failure so a different problem is not silently hidden:

from pytest_jira_xfail.annotations import bug


# XFAIL only if an IndexError with this exact message is raised
@bug("MP-123", IndexError, error_contains="list index out of range")
def test_single_substring():
    records = []
    assert records[0]


# XFAIL if a KeyError message contains any of the listed substrings
@bug("MP-124", KeyError, error_contains=["'user_id'", "'account_id'"])
def test_multiple_substrings():
    payload = {}
    assert payload["user_id"]

Matching is case-sensitive by default. Pass case_sensitive=False for case-insensitive matching:

# Case-sensitive (default): the case must match exactly
@bug("MP-125", ValueError, error_contains="Invalid token")
def test_case_sensitive():
    raise ValueError("invalid token")  # does NOT match -> reported as a failure


# Case-insensitive: matches regardless of case
@bug("MP-126", ValueError, error_contains="invalid token", case_sensitive=False)
def test_case_insensitive():
    raise ValueError("INVALID TOKEN")  # matches -> XFAIL

error_contains can be combined with run=False and with multiple @bug markers (each marker keeps its own expected type, substrings and case option).

XFAIL message format:

XFAIL The test is skipped because of open bugs:
https://company.atlassian.net/browse/MP-123

4. [Optional] Set custom resolved statuses

By default, only issues with the status "Done" and "Closed" are considered as resolved.
But you can override this and add more statuses, as following:

import pytest

from pytest_jira_xfail.jira_helper import PytestJiraHelper


@pytest.hookimpl(tryfirst=True)
def pytest_collection_modifyitems(items):
    jira = PytestJiraHelper(
        jira_url="https://company.atlassian.net",
        jira_username="my_jira_user@company.com",
        jira_api_token="my_jira_user_api_token",
        resolved_statuses=["Done", "Closed", "Released", "Declined"]
    )
    jira.process_linked_jira_issues(items)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_jira_xfail-1.4.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_jira_xfail-1.4.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_jira_xfail-1.4.0.tar.gz.

File metadata

  • Download URL: pytest_jira_xfail-1.4.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pytest_jira_xfail-1.4.0.tar.gz
Algorithm Hash digest
SHA256 cf4b8dd2aaecc8b3012b01ff3addd65ac58da629475b8100710464a4e60e9adb
MD5 c9e8a24e39eaed2c192f4f12844a03bb
BLAKE2b-256 f3d79232561de6c54f7aa7ef4c837b965c93f9620c1bde57e92a053226759b3e

See more details on using hashes here.

File details

Details for the file pytest_jira_xfail-1.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_jira_xfail-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66647dad8d69249750e219bef3d5bb074650c8cce54e0d9feeed6a2c52dfd593
MD5 86de2376f5a14fd85b4258f6346a70be
BLAKE2b-256 a9b3dc853d13d045616be4d678fe0b2b2e8a23e52a406af84df6b65bd5fde509

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page