问题
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