如何精简Python Selenium登录表单测试用例以减少代码重复
Hey there! Glad you've got your login_form_base module sorted—now let's tackle trimming down those 10 test cases to eliminate repetition. Here are some actionable, Python-specific strategies that work great with Selenium:
Use Parameterized Testing (with pytest)
This is hands down the easiest way to turn 10 separate test functions into one. You can define all your login scenarios (empty credentials, invalid password, locked account, etc.) as a list of parameters, and pytest will run the same test logic against each set.Example snippet:
import pytest from login_form_base import LoginForm @pytest.mark.parametrize("username, password, expected_result", [ ("", "", "Please enter username"), ("test_user", "", "Please enter password"), ("test_user", "wrong_pass", "Invalid credentials"), # Add all 10 scenarios here ]) def test_login_scenarios(username, password, expected_result): login_page = LoginForm() login_page.enter_username(username) login_page.enter_password(password) login_page.click_submit() assert login_page.get_error_message() == expected_resultThis way, you only write the core test logic once, and all scenarios are handled via parameters.
Leverage Test Fixtures (pytest) or setUp/tearDown (unittest)
If you're using pytest, create fixtures to handle repetitive setup/teardown tasks (like initializing the Selenium driver, opening the login page, quitting the driver). For unittest, override thesetUp()andtearDown()methods in your test class to avoid repeating these steps in every test case.Example pytest fixture:
import pytest from selenium import webdriver from login_form_base import LoginForm @pytest.fixture def login_page(): driver = webdriver.Chrome() driver.get("https://your-login-page.com") page = LoginForm(driver) yield page driver.quit() # Now your test can just use the fixture def test_empty_username(login_page): login_page.enter_username("") login_page.enter_password("test_pass") login_page.click_submit() assert login_page.get_error_message() == "Please enter username"Abstract Repeated Assertions or Actions
If there are common checks you do across multiple test cases (like verifying successful login redirect, checking error message styles), add these as helper methods in yourlogin_form_basemodule. Then you can call them directly in your tests instead of rewriting the same assertion code.Data-Driven Testing with External Files
For larger sets of test data, you can store your 10 scenarios in a CSV, JSON, or YAML file. Then write a helper function to load the data and loop through each entry in your test. This keeps your test code clean and makes it easy to update scenarios without touching the test logic.Example with JSON:
import json from login_form_base import LoginForm def test_login_from_json(): with open("login_scenarios.json") as f: scenarios = json.load(f) for scenario in scenarios: login_page = LoginForm() login_page.enter_username(scenario["username"]) login_page.enter_password(scenario["password"]) login_page.click_submit() assert login_page.get_error_message() == scenario["expected_result"]
Pick the approach that fits your workflow best—parameterized testing is usually the most straightforward for a small set like 10 scenarios. Let me know if you want to dive deeper into any of these!
内容的提问来源于stack exchange,提问作者HBTHM

