PyTest: Use results from previous test to parametrize next tests

老子叫甜甜 提交于 2021-01-29 08:50:25

问题


I'm using PyTest and Selenium to test all options listed in a dropdown menu on a website. I want to navigate to that page, grab all the available options in the dropdown, and then use parametrize to create new sub-tests to click each option and test UI elements. It looks like this:

class TestOptions:

    dropdown_options = []

    def test_login(self):
        ...

    def test_navigate_to_page(self):
        ...

    def test_grab_all_options(self):
        self.dropdown_options = get_all_dropdown_option_names()
        # test that all option names follow a criteria
        ...

    @pytest.mark.parametrize("option", dropdown_options)
    def test_each_option(self, option):
        find_option_by_name(option).click()
        # tests
        ...

The problem is that I believe parametrize is set before any tests begin so it sees dropdown_options as an empty list. Is there a work-around so I can dynamically populate that list?

来源:https://stackoverflow.com/questions/62411938/pytest-use-results-from-previous-test-to-parametrize-next-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!