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).

Soft assertions (pytest-check)

Tests that fail via soft-assertion libraries such as pytest-check (check.equal(...), @check.check_func, ...) are handled correctly: a soft-assertion failure on a test with an open issue is reported as XFAIL deterministically, regardless of environment (IDE vs CI) or pytest-xdist.

error_contains is enforced for soft-assertion failures too. The XFAIL is kept only when every collected soft failure message contains an expected substring; if any soft failure is unrelated to the bug, the test is reported as a real failure so the regression is not hidden. (A bare @bug with no error_contains keeps the deterministic XFAIL for any soft assertion.)

The XFAIL <reason> message is also shown in Allure for soft-assertion skips. By default pytest-check reports a soft XFAIL without an exception object, so allure-pytest would otherwise render an empty status message; the plugin surfaces the open-issue reason (plus the soft-failure detail) without changing the XFAIL outcome.

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.2.tar.gz (19.5 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.2-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_jira_xfail-1.4.2.tar.gz
  • Upload date:
  • Size: 19.5 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.2.tar.gz
Algorithm Hash digest
SHA256 9ae10eeb23d0bf91c0fe5afb4155879f075bfd0e018f44d03433fcab9fc6821c
MD5 e52075ab52bb6cc56c881d45ec2764b9
BLAKE2b-256 845eac50837a0e0ed48ba4831d868ebec054f8686e04c4fe99ac9267274df062

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_jira_xfail-1.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c1d7c74c9413c169529faa73357a7aa411446909f32fb65ae2ab1116beabff1f
MD5 608aa4d3255044fe011533f96685b4d2
BLAKE2b-256 979243bc37a81b107c7d06b318117c454664d5d1acfae06509fd18dd492d67df

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