aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Skultety <eskultet@redhat.com>2023-03-22 17:21:46 +0100
committerErik Skultety <eskultet@redhat.com>2023-03-22 17:28:39 +0100
commit808bfca1bdffc93c96482546ebb52ae75e8e2617 (patch)
tree51fcc1c085e7c658716b1b7a8d79ef3741b5920c
parentb1562114b86d13cad9468248dbb27a3138a88871 (diff)
downloadlibvirt-ci-808bfca1bdffc93c96482546ebb52ae75e8e2617.zip
libvirt-ci-808bfca1bdffc93c96482546ebb52ae75e8e2617.tar.gz
libvirt-ci-808bfca1bdffc93c96482546ebb52ae75e8e2617.tar.bz2
tests: Introduce class and module scoped equivalents for monkeypatch
If one tries to combine a class-scoped or a module-scoped fixture that happens to depend on monkeypatch, this is what they'll get: "You tried to access the function scoped fixture monkeypatch with a module scoped request object" Sometimes one wants to monkeypatch to carry over the override to further test functions and that's where fixture scopes come in. To remediate the issue, introduce our own global fixture helpers with conforming to different scopes. At the same time, apply the improvement to test_containers.py. Signed-off-by: Erik Skultety <eskultet@redhat.com>
-rw-r--r--tests/conftest.py15
-rw-r--r--tests/test_containers.py26
2 files changed, 24 insertions, 17 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 61a7b43..d82a303 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -35,6 +35,21 @@ ALL_PROJECTS = sorted(_PROJECTS.names + list(_PROJECTS.internal.keys()))
ALL_TARGETS = sorted(_TARGETS.targets)
+def monkeypatch_context():
+ with pytest.MonkeyPatch.context() as mp:
+ yield mp
+
+
+@pytest.fixture(scope="module")
+def monkeypatch_module_scope():
+ yield from monkeypatch_context()
+
+
+@pytest.fixture(scope="class")
+def monkeypatch_class_scope():
+ yield from monkeypatch_context()
+
+
@pytest.fixture
def config(monkeypatch, request):
if 'config_filename' in request.fixturenames:
diff --git a/tests/test_containers.py b/tests/test_containers.py
index 0321861..a10b21f 100644
--- a/tests/test_containers.py
+++ b/tests/test_containers.py
@@ -3,7 +3,6 @@ import pytest
from pathlib import Path
from io import TextIOBase
-from _pytest.monkeypatch import MonkeyPatch
from test_utils.utils import assert_equal_list
from lcitool.containers import ContainerError, Docker, Podman
@@ -41,12 +40,9 @@ class MockSubUidGidTextIO(TextIOBase):
@pytest.fixture(scope="module")
-def mock_pwd():
- monkeypatch = MonkeyPatch()
- monkeypatch.setattr(pwd, "getpwuid", get_pwuid)
- monkeypatch.setattr(pwd, "getpwnam", get_pwuid)
- yield monkeypatch
- monkeypatch.undo()
+def mock_pwd(monkeypatch_module_scope):
+ monkeypatch_module_scope.setattr(pwd, "getpwuid", get_pwuid)
+ monkeypatch_module_scope.setattr(pwd, "getpwnam", get_pwuid)
@pytest.fixture(scope="module")
@@ -67,11 +63,9 @@ class TestPodmanExtraArgs:
return MockSubUidGidTextIO(file, **kwargs)
@pytest.fixture(scope="class", autouse=True)
- def patch_builtins_open(self):
- monkeypatch = MonkeyPatch()
- monkeypatch.setattr("builtins.open", TestPodmanExtraArgs.mock_open)
- yield monkeypatch
- monkeypatch.undo()
+ def patch_builtins_open(self, monkeypatch_class_scope):
+ monkeypatch_class_scope.setattr("builtins.open",
+ TestPodmanExtraArgs.mock_open)
@pytest.mark.parametrize(
"user, args",
@@ -108,11 +102,9 @@ class TestEngineOptions:
return id_mapping
@pytest.fixture(scope="class", autouse=True)
- def patch_extra_args(self):
- monkeypatch = MonkeyPatch()
- monkeypatch.setattr("lcitool.containers.Podman._extra_args", self.podman_extra_args)
- yield monkeypatch
- monkeypatch.undo()
+ def patch_extra_args(self, monkeypatch_class_scope):
+ monkeypatch_class_scope.setattr("lcitool.containers.Podman._extra_args",
+ self.podman_extra_args)
@pytest.mark.parametrize(
"args, options",