From nobody Mon Jan 23 22:12:14 2023 X-Original-To: dev-commits-src-all@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 4P148b1WDPz2t109; Mon, 23 Jan 2023 22:12:15 +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 4P148Z6k6Sz3MNG; Mon, 23 Jan 2023 22:12:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674511934; 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=3MVQ6fOBeSt5GBbbqf4/xXUCN0f6CnrWm7fZsluv5WA=; b=XnXoMthY9sZBPZbae1u7Algm5ayDEsgJeX66FOSQYVBxYyHwXu92mZMcSv4BOWcMb0zD6p i54Id7kkqeZFeuDJ1bzV/R9u9hFOUn9B+c7tq3dYktE0IGE4IvdV5h/BG5zcVTMSrnFY+O gRLpdI6oaTMhc6w2rCh9P/2sCm+w3irEZJNVU3wcTv2FW7d6nSZBUfJ+o8ws4MsJ3NBA7T SoduHu261Aii3p/GIl+U1o7fhGedMVVaH3tRr/N98mhCYmljjj9gzB3orJZ1eerIs7oVR6 hJI1WF/Vkl861FmwqBF+QsMGfFX3KDtp5zJ2WXd4E8h4+rvyoNfcbHeEE+9iLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674511934; 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=3MVQ6fOBeSt5GBbbqf4/xXUCN0f6CnrWm7fZsluv5WA=; b=MdWt85CbhYXVBWBmuTv3FSk3lDPy2J0Tvu9Axd4+UxHqty197Y/r5Oy0hfvUzp3vpH625Q Ni/v+LXHRxDGAtRrrRezmOD/dqukAA9gRslQAQuZSCWTlIXy/Ocnk56NdF1e5G+3qB0G/4 hd9t0HvrRXuxcGQxmOnAtfjyNDCRqpyG1/dVoNuds8zNnGW7uczEp7DX5GMYsl+21kK/tI 0A/P6XAOqFnX4EDzzW1RLyYfz6X1d1LFyy+oakJsHvuNrVld3iuVNsjGGSyMaZtdLhsEzR 2spsZhSThffxh+ZnOqKZzJ6nu5n7B0NYfRWyuMsrUXDC6hZ+lMqQ4L2vhL8YKA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674511934; a=rsa-sha256; cv=none; b=PF6AJLMOFARSNw83XFzSkUFh7Ab/VFX3KPsxj2Qn0lQDzc3AEC8rPptHZ9d0WgzthGNkSz UMrjL26RbhacLe0LvbO7l8LHSrRzRnd1iSDOrRwCSd65HAOxzEx62CkWjXgeXtAz91IEAl GnwltlY9dV6p7z2q9geVh4TcMroI3MRnDYz2QxVpMOXpUTq5dYB3NiPEuFsU8sYly+hahh MmXYc+U4xpfQE7KJiRx/6i/BjquKeHFo9via161LsIY8pXDZIGHdGKRpEPD4b9HjPhIPZc LvZw/JzoTy72rwxn2JZYomF9OdkGYgWlJGXn6dIC/x6LoOtccNVkTcq3WwxO8w== 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 4P148Z5qTmzm2V; Mon, 23 Jan 2023 22:12:14 +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 30NMCEfW017176; Mon, 23 Jan 2023 22:12:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30NMCE0g017175; Mon, 23 Jan 2023 22:12:14 GMT (envelope-from git) Date: Mon, 23 Jan 2023 22:12:14 GMT Message-Id: <202301232212.30NMCE0g017175@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: 8db26a13caf4 - stable/13 - testing: allow custom test cleanup handlers in pytest List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 8db26a13caf4122a4670b952d0e5d2ad4246f377 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=8db26a13caf4122a4670b952d0e5d2ad4246f377 commit 8db26a13caf4122a4670b952d0e5d2ad4246f377 Author: Alexander V. Chernikov AuthorDate: 2022-12-31 16:22:30 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-01-23 22:09:04 +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 (cherry picked from commit 89ffac3b01fb3f6749799ac67b7d94056a36778e) --- 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"')