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

Add basic fixture cli #13101

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pytest_plugins/fspath_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ def pytest_collection_modifyitems(session, items, config):
if item.nodeid.startswith('tests/robottelo/') or item.nodeid.startswith('tests/upgrades/'):
continue

endpoint = endpoint_regex.findall(item.location[0])[0]
item.user_properties.append(('endpoint', endpoint))
if endpoints := endpoint_regex.findall(item.location[0]):
item.user_properties.append(('endpoint', endpoints[0]))
4 changes: 3 additions & 1 deletion pytest_plugins/issue_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ def generate_issue_collection(items, config): # pragma: no cover
filepath, lineno, testcase = item.location
# Component and importance marks are determined by testimony tokens
# Testimony.yaml as of writing has both as required, so any
component_mark = item.get_closest_marker('component').args[0]
if not (components := item.get_closest_marker('component')):
continue
component_mark = components.args[0]
component_slug = slugify_component(component_mark, False)
importance_mark = item.get_closest_marker('importance').args[0]
for marker in item.iter_markers():
Expand Down
46 changes: 46 additions & 0 deletions scripts/fixture_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from pathlib import Path

import click
import pytest


@click.command()
@click.argument("fixtures", nargs=-1, required=True)
@click.option(
"--from-file",
"-f",
type=click.File("w"),
help="Run the fixtures from within a file, inheriting the file's context.",
)
def run_fixtures(fixtures, from_file):
"""Create a temporary test that depends on each fixture, then run it.

You can also run the fixtures from the context of a file, which is useful when testing fixtures
that don't live at a global scope.

Examples:
python scripts/fixture_cli.py module_published_cv module_subscribe_satellite
python scripts/fixture_cli.py module_lce --from-file tests/foreman/api/test_activationkey.py
"""
fixture_string = ", ".join(filter(None, fixtures))
test_template = f"def test_fake({fixture_string}):\n assert True"
if from_file:
from_file = Path(from_file.name)
# inject the test at the end of the file
with from_file.open("a") as f:
eof_pos = f.tell()
f.write(f"\n\n{test_template}")
pytest.main(["-qq", str(from_file.resolve()), "-k", "test_fake"])
# remove the test from the file
with from_file.open("r+") as f:
f.seek(eof_pos)
f.truncate()
else:
temp_file = Path("test_DELETEME.py")
temp_file.write_text(test_template)
pytest.main(["-qq", str(temp_file)])
temp_file.unlink()


if __name__ == "__main__":
run_fixtures()
Loading