From nobody Sat Dec 31 16:27:59 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Nknc01FVPz1LpWQ; Sat, 31 Dec 2022 16:28:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Nknc00FVRz3G4s; Sat, 31 Dec 2022 16:28:00 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1672504080; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=5BwttD48hDekWARP2YHOMu25y1XeEsAIohzFCBoOfbE=; b=UHolnGDK1lbXYbR8EM7Y8GEWIfJn7hKLSJ2mGZKuxOkqOE8Bqd3AfqvkQjol7WzsxEeA7j HdbguD6jidl9ldS7EKpmr9NUQR/PbdDkAza23xwyuX2ESJMkWaEhYW7E7UBDCxeGgATLZO PqEf9QkGueV9P6u4kkhTZPGjsWCq+do4yNow+ORmltuUYKOsy7qt02npslvwkmiRksLziZ 2rAWGvRdm33JsiP/TXzwm1uk21Vs5SHN9jELnaM+efzYiNc+wju5NOKl61djGGOV8tcb0q 0XMEPatK9Pf5T4olkkhaqVxriIQaVQ+ZnSv0o/FD41oYd1MW0XgIucj84ZIQYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1672504080; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=5BwttD48hDekWARP2YHOMu25y1XeEsAIohzFCBoOfbE=; b=J7G0WTp0xXvhdZhD1nsSPH37IJcoqu7mCUE9QxvCcmbK90699n7b7EmIQW9xXeZhLlJZAf f4WyJT1DQTYZ5FIHV3Y+4Jl2jV2oTig2zO9wz/hU8gfkCCBD/Nr2blmnkX1PCxfgNJDqE0 wRaTig4Pfau0Ushd0hiDnwmW8+wE+S7xRrTjq5UxlsIwpZX04L0BMAoe9UwXw2VEpA6CVn L95Gz/ELU9Ya2Vo7XeCyMLc8fuZe06SpyQ/tbJwPiOulWXHrvLiJqvqPvBYmKMn3HR/y93 etESIGgZCgVa1ursKxowsYyDAQ3EKhQRPX4YMuAjN2xHfVtwNMMfHdfVIotvpA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1672504080; a=rsa-sha256; cv=none; b=q/SC6tpP6d57lQyY6vpxn9BXK19Ymjrq2zGu0QZN6VXAy9K+TRctzPVKw6pjh7BguGpeK/ aTrUxMPGxgdiyjJfwPpQnxrTOP8GOOkLQZvN8zWmknNl08HHCMQrOjuDExw/Z5YQLZlzXF desT6b15Byfvav55di5RP4GvwEAU7pFLGfeU/nXrHo4VPuvEfbUn2/UQmzrIALROF9Ozst lQd13G4A6ujh3nGZiuQJNuLMVRCuN2SFkrBB1tXzU3Y87Z7ocY4rEUR3pxqh0XdzVBHgds AqtE7uLX5VrNrH4zWijGJTNL7mXvmoAsmjoWxF4lG3Lnu9k9oT2IYHjrUWQVdw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Nknbz6P3fzvlZ; Sat, 31 Dec 2022 16:27:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2BVGRxUG013742; Sat, 31 Dec 2022 16:27:59 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2BVGRxlq013741; Sat, 31 Dec 2022 16:27:59 GMT (envelope-from git) Date: Sat, 31 Dec 2022 16:27:59 GMT Message-Id: <202212311627.2BVGRxlq013741@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: 89ffac3b01fb - main - testing: allow custom test cleanup handlers in pytest List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 89ffac3b01fb3f6749799ac67b7d94056a36778e Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=89ffac3b01fb3f6749799ac67b7d94056a36778e commit 89ffac3b01fb3f6749799ac67b7d94056a36778e Author: Alexander V. Chernikov AuthorDate: 2022-12-31 16:22:30 +0000 Commit: Alexander V. Chernikov CommitDate: 2022-12-31 16:27:27 +0000 testing: allow custom test cleanup handlers in pytest In order to provide more flexibility for the test writers, add per-test-method cleanups in addition to the per-class cleanups. Now the test 'test_one' can perform cleanup by either defining per-class 'cleanup' method (typically used in VNET classes) and per-test method 'cleanup_test_one'. The latter has preference. In order to handle paramatrization, testid is passed as a single argument to both of the methods. MFC after: 2 weeks --- tests/atf_python/atf_pytest.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/tests/atf_python/atf_pytest.py b/tests/atf_python/atf_pytest.py index f72122fb740e..d530c7b4515c 100644 --- a/tests/atf_python/atf_pytest.py +++ b/tests/atf_python/atf_pytest.py @@ -9,11 +9,21 @@ import pytest import os +def nodeid_to_method_name(nodeid: str) -> str: + """file_name.py::ClassName::method_name[parametrize] -> method_name""" + return nodeid.split("::")[-1].split("[")[0] + + class ATFCleanupItem(pytest.Item): def runtest(self): - """Runs cleanup procedure for the test instead of the test""" + """Runs cleanup procedure for the test instead of the test itself""" instance = self.parent.cls() - instance.cleanup(self.nodeid) + cleanup_name = "cleanup_{}".format(nodeid_to_method_name(self.nodeid)) + if hasattr(instance, cleanup_name): + cleanup = getattr(instance, cleanup_name) + cleanup(self.nodeid) + elif hasattr(instance, "cleanup"): + instance.cleanup(self.nodeid) def setup_method_noop(self, method): """Overrides runtest setup method""" @@ -91,15 +101,20 @@ class ATFHandler(object): obj.parent.cls.setup_method = ATFCleanupItem.setup_method_noop obj.parent.cls.teardown_method = ATFCleanupItem.teardown_method_noop - def get_object_cleanup_class(self, obj): + @staticmethod + def get_test_class(obj): if hasattr(obj, "parent") and obj.parent is not None: - if hasattr(obj.parent, "cls") and obj.parent.cls is not None: - if hasattr(obj.parent.cls, "cleanup"): - return obj.parent.cls - return None + if hasattr(obj.parent, "cls"): + return obj.parent.cls def has_object_cleanup(self, obj): - return self.get_object_cleanup_class(obj) is not None + cls = self.get_test_class(obj) + if cls is not None: + method_name = nodeid_to_method_name(obj.nodeid) + cleanup_name = "cleanup_{}".format(method_name) + if hasattr(cls, "cleanup") or hasattr(cls, cleanup_name): + return True + return False def list_tests(self, tests: List[str]): print('Content-Type: application/X-atf-tp; version="1"')