简体   繁体   中英

Pytest how do i get the mark parameter inside a teardown method?

In my python module i have the following:

def setup_module(module):
    pass


def teardown_module(module):
    pass # i want myMarker parameter here

class TestClass:

    @pytest.mark.myMarker('how do i get this?')
    def test_simple(self):
        pass

How can i get the marker parameter to the teardown_module?

Put the marker on the class and access it from the module parameter

def setup_module(module):
    pass


def teardown_module(module):
    mark = module.TestClass.pytestmark[0]
    print(mark) # Mark(name='myMarker', args=('how do I get this?',), kwargs={})


@pytest.mark.myMarker('how do I get this?')
class TestClass:

    def test_simple(self):
        pass

If you want it from the test you can add requests to it and assign it to a global variable, or use self.test_simple for more hardcoded solution without requests

mark = None


def setup_module(module):
    pass


def teardown_module(module):
    print(mark) # Mark(name='myMarker', args=('how do I get this?',), kwargs={})


class TestClass:

    @pytest.mark.myMarker('how do I get this?')
    def test_simple(self, request):
        global mark
        test = getattr(self, request.node.name)
        mark = test.pytestmark[0]
        # or
        mark = self.test_simple.pytestmark[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM