Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest: Custom marker for GitHub #572

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions receptorctl/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time
import json
import yaml
import urllib.request
from click.testing import CliRunner

from lib import create_certificate
Expand Down Expand Up @@ -389,3 +390,23 @@ def f_invoke_as_json(command, args: list = []):
return result, json_output

return f_invoke_as_json


def pytest_configure(config):
config.addinivalue_line(
"markers",
"github(): this mark skips the tests for the given open GitHub issues",
)


def pytest_runtest_setup(item):
gh_markers = [mark.args[0] for mark in item.iter_markers(name="github")]
if gh_markers:
issue_url = gh_markers[0]
with urllib.request.urlopen(issue_url) as f:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this require using the API endpoint for an Issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. You will need to specify the marker like this -

import pytest

@pytest.mark.github('https://api.github.com/repos/ansible/receptor/issues/441')
def test_c():
    print("This is Test C")

I choose this routine in order to avoid additional dependency on third-party library.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can simplify the logic further to specify only the issue number or pull request number. But for starters, I think this OK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is pretty difficult to use. Normally you'd just visit an issue in your browser. We should be able to copy / paste that URL instead of having to construct this API endpoint. We use https://github.com/jlaska/pytest-github in other projects, so we're not maintaining anything extra by also using it here.

try:
status = json.loads(f.read())["state"]
if status == "open":
pytest.skip("Test skipped because GitHub issue is open")
except (json.decoder.JSONDecodeError, IndexError):
pass