From 1932f7a72b968b406917f904cdaf1f38107fa1ca Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Fri, 14 Apr 2023 16:54:11 +0100 Subject: [PATCH 1/2] Use correct pytest cli --- README.md | 20 ++++++++++---------- pyproject.toml | 2 +- tests/test_params.py | 26 ++++++++++++-------------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 78fa97d1..6c80c1bb 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![License](https://img.shields.io/pypi/l/pytest-ansible.svg)](https://pypi.python.org/pypi/pytest-ansible/) [![Supported Python Versions](https://img.shields.io/pypi/pyversions/pytest-ansible.svg)](https://pypi.python.org/pypi/pytest-ansible/) -This repository contains a plugin for `py.test` which adds several fixtures +This repository contains a plugin for `pytest` which adds several fixtures for running `ansible` modules, or inspecting `ansible_facts`. While one can simply call out to `ansible` using the `subprocess` module, having to parse stdout to determine the outcome of the operation is unpleasant and prone @@ -23,10 +23,10 @@ pip install pytest-ansible ## Usage -Once installed, the following `py.test` command-line parameters are available: +Once installed, the following `pytest` command-line parameters are available: ```bash -py.test \ +pytest \ [--inventory ] \ [--extra-inventory ] \ [--host-pattern ] \ @@ -60,19 +60,19 @@ JSON](http://docs.ansible.com/ansible/latest/intro_dynamic_inventory.html). For example, ```bash -py.test --inventory my_inventory.ini --host-pattern all +pytest --inventory my_inventory.ini --host-pattern all ``` or ```bash -py.test --inventory path/to/my/script.py --host-pattern webservers +pytest --inventory path/to/my/script.py --host-pattern webservers ``` or ```bash -py.test --inventory one.example.com,two.example.com --host-pattern all +pytest --inventory one.example.com,two.example.com --host-pattern all ``` In the above examples, the inventory provided at runtime will be used in all @@ -91,18 +91,18 @@ we'll use the `ansible_adhoc` fixture. For example, ```bash -py.test --inventory my_inventory.ini --extra-inventory my_second_inventory.ini --host-pattern host_in_second_inventory +pytest --inventory my_inventory.ini --extra-inventory my_second_inventory.ini --host-pattern host_in_second_inventory ``` ### Fixture `ansible_adhoc` The `ansible_adhoc` fixture returns a function used to initialize a `HostManager` object. The `ansible_adhoc` fixture will default to parameters -supplied to the `py.test` command-line, but also allows one to provide keyword +supplied to the `pytest` command-line, but also allows one to provide keyword arguments used to initialize the inventory. The example below demonstrates basic usage with options supplied at run-time to -`py.test`. +`pytest`. ```python def test_all_the_pings(ansible_adhoc): @@ -180,7 +180,7 @@ def test_do_something_cloudy(localhost, ansible_adhoc): The `ansible_module` fixture allows tests and fixtures to call [ansible modules](http://docs.ansible.com/modules.html). Unlike the `ansible_adhoc` -fixture, this fixture only uses the options supplied to `py.test` at run time. +fixture, this fixture only uses the options supplied to `pytest` at run time. A very basic example demonstrating the ansible [`ping` module](http://docs.ansible.com/ping_module.html): diff --git a/pyproject.toml b/pyproject.toml index d57c727d..06070c11 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta" requires-python = ">=3.9" dynamic = ["version", "dependencies", "optional-dependencies"] name = "pytest-ansible" -description = "Plugin for py.test to simplify calling ansible modules from tests or fixtures" +description = "Plugin for pytest to simplify calling ansible modules from tests or fixtures" readme = "README.md" authors = [{ "name" = "Ansible by Red Hat", "email" = "info@ansible.com" }] maintainers = [{ "name" = "Ansible by Red Hat", "email" = "info@ansible.com" }] diff --git a/tests/test_params.py b/tests/test_params.py index 1c6f8462..4169b685 100644 --- a/tests/test_params.py +++ b/tests/test_params.py @@ -1,23 +1,21 @@ import re import sys - from unittest import mock import ansible import pytest - from pkg_resources import parse_version - from pytest_ansible.has_version import has_ansible_v28 - # pylint: disable=unused-import try: - from _pytest.main import EXIT_INTERRUPTED # type: ignore[attr-defined] - from _pytest.main import EXIT_NOTESTSCOLLECTED # type: ignore[attr-defined] - from _pytest.main import EXIT_OK # type: ignore[attr-defined] - from _pytest.main import EXIT_TESTSFAILED # type: ignore[attr-defined] - from _pytest.main import EXIT_USAGEERROR # type: ignore[attr-defined] + from _pytest.main import ( + EXIT_INTERRUPTED, # type: ignore[attr-defined] + EXIT_NOTESTSCOLLECTED, # type: ignore[attr-defined] + EXIT_OK, # type: ignore[attr-defined] + EXIT_TESTSFAILED, # type: ignore[attr-defined] + EXIT_USAGEERROR, # type: ignore[attr-defined] + ) except ImportError: from _pytest.main import ExitCode @@ -35,7 +33,7 @@ def test_plugin_help(testdir): - """Verifies expected output from of py.test --help""" + """Verifies expected output from of pytest --help""" result = testdir.runpytest("--help") result.stdout.fnmatch_lines( @@ -60,7 +58,7 @@ def test_plugin_help(testdir): def test_plugin_markers(testdir): - """Verifies expected output from of py.test --markers""" + """Verifies expected output from of pytest --markers""" result = testdir.runpytest("--markers") result.stdout.fnmatch_lines( @@ -177,7 +175,7 @@ def test_func(ansible_module): *["--ansible-inventory", "bogus", "--ansible-host-pattern", "all"] ) - # Assert py.test exit code + # Assert pytest exit code assert result.ret == EXIT_TESTSFAILED # Assert expected error output @@ -218,7 +216,7 @@ def test_func(ansible_module): ] ) - # Assert py.test exit code + # Assert pytest exit code assert result.ret == EXIT_OK # Assert mock open called on provided file @@ -245,7 +243,7 @@ def test_func(ansible_module): *["-vvvvvs", "--ansible-inventory", "bogus", "--ansible-host-pattern", "all"] ) - # Assert py.test exit code + # Assert pytest exit code assert result.ret == EXIT_OK # There appear to be '\n' newline characters within the output. Using the join on errlines flattens the string for From b259ba263ecff05465f9cacea6b3744caca621c3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Apr 2023 15:54:45 +0000 Subject: [PATCH 2/2] chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_params.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/test_params.py b/tests/test_params.py index 4169b685..1bb4c3a0 100644 --- a/tests/test_params.py +++ b/tests/test_params.py @@ -1,21 +1,23 @@ import re import sys + from unittest import mock import ansible import pytest + from pkg_resources import parse_version + from pytest_ansible.has_version import has_ansible_v28 + # pylint: disable=unused-import try: - from _pytest.main import ( - EXIT_INTERRUPTED, # type: ignore[attr-defined] - EXIT_NOTESTSCOLLECTED, # type: ignore[attr-defined] - EXIT_OK, # type: ignore[attr-defined] - EXIT_TESTSFAILED, # type: ignore[attr-defined] - EXIT_USAGEERROR, # type: ignore[attr-defined] - ) + from _pytest.main import EXIT_INTERRUPTED # type: ignore[attr-defined] + from _pytest.main import EXIT_NOTESTSCOLLECTED # type: ignore[attr-defined] + from _pytest.main import EXIT_OK # type: ignore[attr-defined] + from _pytest.main import EXIT_TESTSFAILED # type: ignore[attr-defined] + from _pytest.main import EXIT_USAGEERROR # type: ignore[attr-defined] except ImportError: from _pytest.main import ExitCode