diff --git a/README.rst b/README.rst index 1c2bacb..eb6ee05 100644 --- a/README.rst +++ b/README.rst @@ -97,6 +97,25 @@ If you specify version, open issues will be **unresolved** only if they also aff Even when the issue is closed, but your version was affected and it was not fixed for your version, the issue will be considered **unresolved**. +Fixture usage +------------- + +Besides a test marker, you can also use the added ``jira_issue`` fixture. This enables examining issue status mid test +and not just at the beginning of a test. The fixture return a boolean representing the state of the issue. +If the issue isn't found, or the jira plugin isn't loaded, it returns ``None``. + +.. code:: python + + NICE_ANIMALS = ["bird", "cat", "dog"] + + def test_stuff(jira_issue): + animals = ["dog", "cat"] + for animal in animals: + if animal == "dog" and jira_issue("ORG-1382") is True: + print("Issue is still open, cannot check for dogs!") + continue + assert animal in NICE_ANIMALS + Requires ======== diff --git a/pytest_jira.py b/pytest_jira.py index fa24faf..acd860f 100644 --- a/pytest_jira.py +++ b/pytest_jira.py @@ -222,8 +222,8 @@ def get_url(self): class JiraMarkerReporter(object): issue_re = r"([A-Z]+-[0-9]+)" - def __init__(self, strategy, docs, patern): - self.issue_pattern = re.compile(patern or self.issue_re) + def __init__(self, strategy, docs, pattern): + self.issue_pattern = re.compile(pattern or self.issue_re) self.docs = docs self.strategy = strategy.lower() @@ -441,5 +441,16 @@ def pytest_configure(config): resolved_statuses, config.getvalue('jira_run_test_case'), ) + config._jira = jira_plugin ok = config.pluginmanager.register(jira_plugin, "jira_plugin") assert ok + + +@pytest.fixture +def jira_issue(request): + def wrapper_jira_issue(issue_id): + jira_plugin = getattr(request.config, '_jira') + if jira_plugin and jira_plugin.conn.is_connected(): + return jira_plugin.is_issue_resolved(issue_id) + + return wrapper_jira_issue diff --git a/tests/test_jira.py b/tests/test_jira.py index 8738cb5..a5a087f 100644 --- a/tests/test_jira.py +++ b/tests/test_jira.py @@ -704,3 +704,27 @@ def test_fail(): """) result = testdir.runpytest(*PLUGIN_ARGS) assert_outcomes(result, passed=0, skipped=0, failed=0, error=0, xfailed=1) + + +def test_jira_fixture_run_positive(testdir): + testdir.makeconftest(CONFTEST) + testdir.makepyfile(""" + import pytest + + def test_pass(jira_issue): + assert not jira_issue("ORG-1382") + """) + result = testdir.runpytest(*PLUGIN_ARGS) + result.assert_outcomes(1, 0, 0) + + +def test_jira_fixture_run_negative(testdir): + testdir.makeconftest(CONFTEST) + testdir.makepyfile(""" + import pytest + + def test_pass(jira_issue): + assert jira_issue("ORG-1382") + """) + result = testdir.runpytest(*PLUGIN_ARGS) + result.assert_outcomes(0, 0, 1)