Skip to content

Commit

Permalink
Improve naming of tests
Browse files Browse the repository at this point in the history
- avoid duplicate test names
- improve test id names
- enable pytest-plus to detect regressions
  • Loading branch information
ssbarnea committed Oct 20, 2023
1 parent 8e31a8f commit 9345151
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 46 deletions.
1 change: 1 addition & 0 deletions .config/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ansible-core
coverage
molecule
pytest-plus>=0.6.0; python_version>='3.9'
1 change: 1 addition & 0 deletions .config/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pycparser==2.21
pygments==2.16.1
pymdown-extensions==10.3
pytest==7.4.2
pytest-plus==0.6.0 ; python_version >= "3.9"
python-dateutil==2.8.2
python-slugify==8.0.1
pyyaml==6.0.1
Expand Down
6 changes: 0 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ repos:
- id: check-executables-have-shebangs
- id: debug-statements
language_version: python3
- repo: https://github.com/asottile/add-trailing-comma.git
rev: v3.1.0
hooks:
- id: add-trailing-comma
args:
- --py36-plus
- repo: https://github.com/Lucas-C/pre-commit-hooks.git
rev: v1.5.4
hooks:
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ classifiers = [
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12'
'Programming Language :: Python :: 3.12',
]
keywords = ["ansible", "testing", "pytest"]

Expand Down Expand Up @@ -124,7 +124,8 @@ disable = [

[tool.pytest.ini_options]
minversion = 6.0
addopts = ["-v", "--tb=native"]
# do not add verbosity level here
addopts = ["--tb=native"]
markers = [
"old",
"unit",
Expand Down
7 changes: 5 additions & 2 deletions src/pytest_ansible/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


logger = logging.getLogger(__name__)
counter = 0


def molecule_pytest_configure(config):
Expand Down Expand Up @@ -100,10 +101,12 @@ class MoleculeFile(pytest.File):

def collect(self):
"""Test generator."""
global counter
if hasattr(MoleculeItem, "from_parent"):
yield MoleculeItem.from_parent(name="test", parent=self)
yield MoleculeItem.from_parent(name=f"test{counter}", parent=self)
else:
yield MoleculeItem("test", self)
yield MoleculeItem(f"test{counter}", self)
counter += 1

def __str__(self) -> str:
"""Return test name string representation."""
Expand Down
45 changes: 23 additions & 22 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,40 @@
ALL_HOSTS = ["another_host", "localhost", "yet_another_host"]

POSITIVE_HOST_PATTERNS = [
("all", 3),
("*", 3),
("localhost", 1),
("local*", 1),
("local*,&*host", 1),
("!localhost", 2),
("all[0]", 1),
("all[-1]", 1),
pytest.param("*[0-1]", 1, marks=pytest.mark.requires_ansible_v1()),
pytest.param("*[0-1]", 2, marks=pytest.mark.requires_ansible_v2()),
pytest.param("all", 3, id="0"),
pytest.param("*", 3, id="1"),
pytest.param("localhost", 1, id="2"),
pytest.param("local*", 1, id="3"),
pytest.param("local*,&*host", 1, id="4"),
pytest.param("!localhost", 2, id="5"),
pytest.param("all[0]", 1, id="6"),
pytest.param("all[-1]", 1, id="7"),
pytest.param("*[0-1]", 1, marks=pytest.mark.requires_ansible_v1(), id="8"),
pytest.param("*[0-1]", 2, marks=pytest.mark.requires_ansible_v2(), id="9"),
pytest.param(
"*[0:1]",
2,
marks=pytest.mark.requires_ansible_v2(),
id="10",
), # this is confusing, but how host slicing works on v2
pytest.param("*[0:]", 3, marks=pytest.mark.requires_ansible_v2()),
pytest.param("*[0:]", 3, marks=pytest.mark.requires_ansible_v2(), id="11"),
]

NEGATIVE_HOST_PATTERNS = [
("none", 0),
("all[8:]", 0),
pytest.param("none", 0, id="0"),
pytest.param("all[8:]", 0, id="1"),
]

POSITIVE_HOST_SLICES = [
(slice(0, 0), 1),
pytest.param(slice(0, 1), 1, marks=pytest.mark.requires_ansible_v1()),
pytest.param(slice(0, 1), 2, marks=pytest.mark.requires_ansible_v2()),
pytest.param(slice(0, 2), 2, marks=pytest.mark.requires_ansible_v1()),
pytest.param(slice(0, 2), 3, marks=pytest.mark.requires_ansible_v2()),
(slice(0), 1),
(slice(1), 1),
(slice(2), 1),
(slice(3), 1),
pytest.param(slice(0, 0), 1, id="0"),
pytest.param(slice(0, 1), 1, marks=pytest.mark.requires_ansible_v1(), id="1"),
pytest.param(slice(0, 1), 2, marks=pytest.mark.requires_ansible_v2(), id="2"),
pytest.param(slice(0, 2), 2, marks=pytest.mark.requires_ansible_v1(), id="3"),
pytest.param(slice(0, 2), 3, marks=pytest.mark.requires_ansible_v2(), id="4"),
pytest.param(slice(0), 1, id="5"),
pytest.param(slice(1), 1, id="6"),
pytest.param(slice(2), 1, id="7"),
pytest.param(slice(3), 1, id="8"),
]

NEGATIVE_HOST_SLICES = [
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_molecule_collect() -> None:
pytest.fail(exc.stderr)

assert proc.returncode == 0
assert "test[default]" in proc.stdout
assert "test1[default]" in proc.stdout


def test_molecule_disabled() -> None:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_adhoc_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from pytest_ansible.results import ModuleResult


invalid_hosts = ["none", "all", "*", "local*"]
invalid_hosts = [
pytest.param("none"),
pytest.param("all"),
pytest.param("*", id="glob"),
pytest.param("local*", id="glob2"),
]


@pytest.fixture()
Expand Down
18 changes: 9 additions & 9 deletions tests/test_host_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,44 @@
]


def test_len(hosts):
def test_host_manager_len(hosts):
assert len(hosts) == len(ALL_HOSTS)


def test_keys(hosts):
def test_host_manager_keys(hosts):
sorted_keys = hosts.keys()
sorted_keys.sort()
assert sorted_keys == ALL_HOSTS


@pytest.mark.parametrize(("host_pattern", "num_hosts"), POSITIVE_HOST_PATTERNS)
def test_contains(host_pattern, num_hosts, hosts):
def test_host_manager_contains(host_pattern, num_hosts, hosts):
assert host_pattern in hosts, f"{host_pattern} not in hosts"


@pytest.mark.parametrize(("host_pattern", "num_hosts"), NEGATIVE_HOST_PATTERNS)
def test_not_contains(host_pattern, num_hosts, hosts):
def test_host_manager_not_contains(host_pattern, num_hosts, hosts):
assert host_pattern not in hosts


@pytest.mark.parametrize(("host_pattern", "num_hosts"), POSITIVE_HOST_PATTERNS)
def test_getitem(host_pattern, num_hosts, hosts):
def test_host_manager_getitem(host_pattern, num_hosts, hosts):
assert hosts[host_pattern]


@pytest.mark.parametrize(("host_pattern", "num_hosts"), NEGATIVE_HOST_PATTERNS)
def test_not_getitem(host_pattern, num_hosts, hosts):
def test_host_manager_not_getitem(host_pattern, num_hosts, hosts):
with pytest.raises(KeyError):
assert hosts[host_pattern]


@pytest.mark.parametrize(("host_pattern", "num_hosts"), POSITIVE_HOST_PATTERNS)
def test_getattr(host_pattern, num_hosts, hosts):
def test_host_manager_getattr(host_pattern, num_hosts, hosts):
assert hasattr(hosts, host_pattern)


@pytest.mark.parametrize(("host_slice", "num_hosts"), POSITIVE_HOST_SLICES)
def test_slice(host_slice, num_hosts, hosts):
def test_host_manager_slice(host_slice, num_hosts, hosts):
assert (
len(hosts[host_slice]) == num_hosts
), f"{len(hosts[host_slice])} != {num_hosts} for {host_slice}"
Expand All @@ -66,7 +66,7 @@ def test_not_slice(host_slice, hosts):


@pytest.mark.parametrize(("host_pattern", "num_hosts"), NEGATIVE_HOST_PATTERNS)
def test_not_getattr(host_pattern, num_hosts, hosts):
def test_host_manager_not_getattr(host_pattern, num_hosts, hosts):
assert not hasattr(hosts, host_pattern)
with pytest.raises(AttributeError):
getattr(hosts, host_pattern)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_module_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ def test_importerror_requires_v1():


@pytest.mark.parametrize(("host_pattern", "num_hosts"), POSITIVE_HOST_PATTERNS)
def test_len(host_pattern, num_hosts, hosts):
def test_dispatcher_len(host_pattern, num_hosts, hosts):
assert len(getattr(hosts, host_pattern)) == num_hosts


@pytest.mark.parametrize(("host_pattern", "num_hosts"), POSITIVE_HOST_PATTERNS)
def test_contains(host_pattern, num_hosts, hosts):
def test_dispatcher_contains(host_pattern, num_hosts, hosts):
assert host_pattern in hosts.all
assert host_pattern in hosts["all"]


@pytest.mark.parametrize(("host_pattern", "num_hosts"), NEGATIVE_HOST_PATTERNS)
def test_not_contains(host_pattern, num_hosts, hosts):
def test_dispatcher_not_contains(host_pattern, num_hosts, hosts):
assert host_pattern not in hosts.all
assert host_pattern not in hosts["all"]

Expand Down

0 comments on commit 9345151

Please sign in to comment.