简体   繁体   中英

How to apply parameterization for whole test suite rather than using it for individual single test case in pytest Selenium

Could someone please help me with this Selenium Pytest framework problem? I tried to google it. But nothing helped me. Apologies for the long question. Want to make sure that I give a clear picture of the problem.

I have the employee test data in the below dictionary " my_test_data ". Below is the scenario I want to achieve, for each employee in a single run(py.test -v -s). The reason behind asking for a single run is, I want to grab the transaction ID from each test suite and send a consolidated email. For Example, If the test ran for 5 employees, then 5 unique transaction ID would get generated and I get those transaction ID by inspecting the web element using selenium.

  1. Login to the portal
  2. Enter Personal Informartion
  3. Enter Education Information
  4. Enter Work Experience Information
  5. Submit it
  6. Grab the Session ID and append it with the "session_id" list.
  7. Logout

So, If I have 5 employees, then 5 unique sessions ID will be available in the "session_id" list. Once all the tests run for all 5 employees, then I will grab the session ID and send out the consolidated email along with a few other additional information.

I tried to do this with parameterization in the fixture(params=["employee_1", "employee_1"... "employee_n"]. But the issue is, it runs each test individually for the given number of parameters. But I want the whole test suite to run for each parameter.

Example:

When I do py.test -v -s, then for each employee in the params, I want to do something like this.

Expected Execution Order:

Employee 1:

test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py

Employee 2:

test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py

Employee 3:

test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py

Actual Execution Order:

test_login.py (For Employee 1)
test_login.py (For Employee 2)
test_login.py (For Employee 3)
test_personal_information.py (For Employee 1)
test_personal_information.py (For Employee 2)
test_personal_information.py (For Employee 3)

. . .

my_test_data = {
    "employee_1": {
        "personal_information": "...",
        "education_information": "...",
        "work_experience_information": "...",
    },
    "employee_2": {"..."}
    # Till exmployee n
}

################ conftest.py ################
@pytest.fixture(scope="session", params=["employee_1", "employee_2", "employee_3", "employee_n"]) # Till employee n
def test_setup(request):
    driver = webdriver.Chrome()
    driver.get(url)
    session_id = []
    yield driver, request.param, session_id
    driver.quit()


################ test_login.py ################
@pytest.mark.usefixtures("test_setup")
class TestPersonalInformation(unittest.TestCase):
    # Login action is performed here
    pass


################ test_personal_information.py ################
@pytest.mark.usefixtures("test_setup")
class TestPersonalInformation(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def class_setup(self, set_driver):
        self.personal_information_page = PersonalInformationPage(drier=test_setup[0])
        self.test_Data = test_setup[1]

    def test_personal_information_page(self):
        self.personal_information_page.send_first_name(self.test_Data["personal_information"]["first_name"])
        self.personal_information_page.send_first_name(self.test_Data["personal_information"]["last_name"])
        # Few other personal informations are also sent
        self.personal_information_page.click_on_next_button()


################ test_education_information.py ################
@pytest.mark.usefixtures("test_setup")
class TestEducationInformation(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def class_setup(self, set_driver):
        self.education_information_page = EducationInformationPage(drier=test_setup[0])
        self.test_Data = test_setup[1]

    def test_education_information_page(self):
        self.education_information_page.send_highest_degree(self.test_Data["education_information"]["highest_degree"])
        self.education_information_page.send_university_name(self.test_Data["education_information"]["university_name"])
        # Few other education informations are also sent
        self.education_information_page.click_on_next_button()


################ test_work_experience_information.py ################
@pytest.mark.usefixtures("test_setup")
class TestWorkExperienceInformation(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def class_setup(self, set_driver):
        self.work_experience_page = WorkExperienceInformationPage(drier=test_setup[0])
        self.test_Data = test_setup[1]

    def test_work_experience_page_page(self):
        self.work_experience_page.send_work_experience(self.test_Data["work_experience_information"]["work_experience"])
        self.work_experience_page.send_current_organization_name(self.test_Data["work_experience_information"]["current_organization_name"])
        # Few other work experience informations are also sent
        self.work_experience_page.submit()


################ test_logout.py ################
@pytest.mark.usefixtures("test_setup")
class TestPersonalInformation(unittest.TestCase):
    # Logout action is performed here
    pass

Few things I can think of, for pytest . Do check if they work equally well for unittest .

First, move all the test cases under a single class so that the test functions are executed in order. Something like:

class TestUser:

    def test_login(self, test_setup):
        pass

    def test_personal_information(self, test_setup):
        pass

    def test_education_information(self, test_setup):
        pass

    def test_work_experience_information(self, test_setup):
        pass

    def test_logout(self, test_setup):
        pass

Or, make a separate class and inherit all other classes in it in order :

class TestUser(TestPersonalInformation, TestEducationInformation, TestWorkExperienceInformation):
    pass

Or, (for pytest) use an ordering plugin like pytest-order

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