Defect prevention strategies 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 CodeReviewPage: def __init__(self, driver): self.driver = driver self.wait = WebDriverWait(driver, 10) def open_review(self, review_url): self.driver.get(review_url) def is_coding_standards_checked(self): checkbox = self.wait.until(EC.presence_of_element_located((By.ID, 'coding-standards-checkbox'))) return checkbox.is_selected() def are_comments_clear(self): comments = self.wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'code-comment'))) return all(comment.text.strip() != '' for comment in comments) def are_unit_tests_passing(self): status = self.wait.until(EC.presence_of_element_located((By.ID, 'unit-test-status'))) return status.text.lower() == 'passing' def no_defect_patterns(self): defect_warnings = self.driver.find_elements(By.CLASS_NAME, 'defect-warning') return len(defect_warnings) == 0 def is_approve_enabled(self): approve_btn = self.wait.until(EC.presence_of_element_located((By.ID, 'approve-button'))) return approve_btn.is_enabled() def test_defect_prevention_in_code_review(): driver = webdriver.Chrome() review_page = CodeReviewPage(driver) try: review_url = 'https://example.com/code-review/123' review_page.open_review(review_url) assert review_page.is_coding_standards_checked(), 'Coding standards checklist is not completed.' assert review_page.are_comments_clear(), 'Code comments are missing or unclear.' assert review_page.are_unit_tests_passing(), 'Unit tests are not passing.' assert review_page.no_defect_patterns(), 'Defect patterns found in code.' assert review_page.is_approve_enabled(), 'Approve button is not enabled after checks.' finally: driver.quit() if __name__ == '__main__': test_defect_prevention_in_code_review()
This script uses Selenium WebDriver with Python to automate the verification of defect prevention strategies in a code review process.
The CodeReviewPage class models the code review page using the Page Object Model pattern. It encapsulates all interactions and element locators, improving maintainability.
Explicit waits ensure the script waits for elements to load before interacting, avoiding flaky tests.
The test function test_defect_prevention_in_code_review opens the review URL, then asserts that:
- The coding standards checklist is completed.
- Code comments are present and clear.
- Unit tests are passing.
- No defect patterns are detected.
- The approve button is enabled only after all checks pass.
Assertions have clear messages to help identify failures.
The driver quits at the end to close the browser cleanly.
Now add data-driven testing with 3 different code review URLs to verify defect prevention on multiple reviews.
Practice
Solution
Step 1: Understand defect prevention purpose
Defect prevention aims to stop bugs early, ideally before coding begins.Step 2: Compare options with this goal
Only To stop defects before coding starts matches this goal; others focus on later stages or unrelated actions.Final Answer:
To stop defects before coding starts -> Option BQuick Check:
Defect prevention = stop defects early [OK]
- Confusing defect prevention with defect detection
- Thinking testing only happens after coding
- Believing more test cases prevent defects
- Assuming testing should be delayed
Solution
Step 1: Identify defect prevention techniques
Defect prevention includes activities like code reviews done early to catch issues.Step 2: Evaluate each option
Only Conducting code reviews before integration describes a proactive step before integration; others are reactive or harmful.Final Answer:
Conducting code reviews before integration -> Option AQuick Check:
Code reviews before integration = defect prevention [OK]
- Thinking testing after coding is prevention
- Ignoring small bugs helps prevent defects
- Skipping documentation is a prevention method
- Confusing defect detection with prevention
def review_code(code):
defects_found = []
for line in code:
if 'TODO' in line:
defects_found.append('Unfinished task')
return defects_found
code_sample = ['print("Hello")', '# TODO: fix bug', 'return True']
defects = review_code(code_sample)
print(defects)
What will be the output after running this code?Solution
Step 1: Analyze the review_code function
The function checks each line for 'TODO' and adds 'Unfinished task' to defects_found if found.Step 2: Check code_sample lines for 'TODO'
The second line contains '# TODO: fix bug', so 'Unfinished task' is added once.Final Answer:
['Unfinished task'] -> Option DQuick Check:
TODO found = 'Unfinished task' added [OK]
- Assuming empty list if no exact match
- Confusing 'TODO' string with defect name
- Expecting syntax errors due to code formatting
- Ignoring the append operation
Solution
Step 1: Understand pair programming role
Pair programming helps prevent defects by collaboration and communication.Step 2: Identify why defects still appear
If bugs persist, poor communication between pairs is a common cause.Final Answer:
The pairs are not communicating effectively -> Option AQuick Check:
Poor communication reduces pair programming effectiveness [OK]
- Thinking pair programming doesn't prevent defects
- Blaming test case quantity instead of quality
- Ignoring the importance of code reviews
- Assuming pair programming alone fixes all bugs
Solution
Step 1: Identify effective defect prevention methods
Code reviews before coding, pair programming, and static analysis tools are proactive strategies.Step 2: Evaluate each option's effectiveness
Code reviews before coding, pair programming during development, and static analysis tools combines early and continuous prevention; others delay or skip key steps.Final Answer:
Code reviews before coding, pair programming during development, and static analysis tools -> Option CQuick Check:
Combine early reviews + collaboration + tools = best prevention [OK]
- Choosing late testing as prevention
- Ignoring documentation and reviews
- Relying only on manual testing
- Skipping static analysis tools
Ready to prove it?
Pass the test and this pattern moves unknown
