Regression testing in Testing Fundamentals - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = (By.ID, 'username') self.password_input = (By.ID, 'password') self.login_button = (By.ID, 'loginBtn') def enter_username(self, username): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.username_input)).send_keys(username) def enter_password(self, password): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.password_input)).send_keys(password) def click_login(self): WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.login_button)).click() class DashboardPage: def __init__(self, driver): self.driver = driver self.logout_button = (By.ID, 'logoutBtn') def is_logout_visible(self): return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.logout_button)) is not None def test_login_functionality(): driver = webdriver.Chrome() driver.get('https://example.com/login') login_page = LoginPage(driver) login_page.enter_username('testuser') login_page.enter_password('Test@1234') login_page.click_login() # Verify URL contains '/dashboard' WebDriverWait(driver, 10).until(EC.url_contains('/dashboard')) assert '/dashboard' in driver.current_url, f"Expected '/dashboard' in URL but got {driver.current_url}" dashboard_page = DashboardPage(driver) assert dashboard_page.is_logout_visible(), "Logout button should be visible on dashboard" driver.quit() if __name__ == '__main__': test_login_functionality()
This script uses Selenium with Python to automate the regression test for login functionality.
We use the Page Object Model to keep locators and actions organized in LoginPage and DashboardPage classes.
Explicit waits ensure the script waits for elements to appear or be clickable, avoiding flaky tests.
The test opens the login page, enters credentials, clicks login, then verifies the URL contains '/dashboard' and the logout button is visible.
Finally, the browser is closed to clean up.
Now add data-driven testing with 3 different sets of valid username and password combinations
Practice
What is the main purpose of regression testing in data science projects?
Solution
Step 1: Understand regression testing goal
Regression testing is about verifying that recent changes do not cause errors in existing functionality.Step 2: Match purpose with options
Only To check that new changes do not break existing results describes this goal correctly, others describe unrelated tasks.Final Answer:
To check that new changes do not break existing results -> Option AQuick Check:
Regression testing = check no breakage [OK]
- Confusing regression testing with model building
- Thinking it collects new data
- Assuming it is for data visualization
Which of the following is the correct way to describe regression testing?
Regression testing:
def regression_test(old_output, new_output):
return old_output == new_outputSolution
Step 1: Analyze the function code
The function returns True if old_output equals new_output, meaning it compares outputs.Step 2: Match function behavior to options
It compares old and new outputs to detect changes correctly states it compares outputs to detect changes; others describe unrelated actions.Final Answer:
It compares old and new outputs to detect changes -> Option AQuick Check:
Function compares outputs = It compares old and new outputs to detect changes [OK]
- Thinking regression testing retrains models
- Assuming it creates visualizations
- Believing it deletes old results
Given the following Python code for regression testing outputs, what will be the printed result?
old_results = [10, 20, 30]
new_results = [10, 20, 25]
if old_results == new_results:
print("Test Passed")
else:
print("Test Failed")Solution
Step 1: Compare the two lists element-wise
old_results has [10, 20, 30], new_results has [10, 20, 25]. The last elements differ (30 vs 25).Step 2: Evaluate the if condition
Since the lists are not equal, the else block runs, printing "Test Failed".Final Answer:
Test Failed -> Option CQuick Check:
Lists differ, so output = Test Failed [OK]
- Assuming partial match passes test
- Confusing list equality with set equality
- Expecting syntax errors from correct code
Identify the error in this regression testing code snippet:
def regression_test(old, new):
if old = new:
return True
else:
return FalseSolution
Step 1: Check the if condition syntax
The condition uses '=' which is assignment, not comparison. Python requires '==' for comparison.Step 2: Identify correct syntax for comparison
Replacing '=' with '==' fixes the syntax error and allows proper comparison.Final Answer:
Using assignment operator '=' instead of comparison '==' in if condition -> Option BQuick Check:
Use '==' to compare, not '=' [OK]
- Confusing '=' and '==' in conditions
- Thinking return is missing
- Believing function name matters for syntax
You have a data science model that outputs predictions as a list. You want to automate regression testing to check if new code changes break the model's output. Which approach below best applies regression testing?
Solution
Step 1: Understand regression testing in model outputs
Regression testing means re-running tests and comparing new outputs to old outputs to detect unintended changes.Step 2: Evaluate options for best fit
Save previous predictions and compare them with new predictions after changes saves old predictions and compares them to new ones, matching regression testing. Others ignore old outputs or delete them, which is not regression testing.Final Answer:
Save previous predictions and compare them with new predictions after changes -> Option DQuick Check:
Regression testing = compare old and new outputs [OK]
- Ignoring old outputs in testing
- Assuming visualization replaces regression tests
- Deleting old results before testing
Ready to prove it?
Pass the test and this pattern moves unknown
