0
0
Testing Fundamentalstesting~15 mins

Defect prevention strategies in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify defect prevention strategy implementation in code review process
Preconditions (2)
Step 1: Open the code review tool for the submitted code change
Step 2: Check if the code follows the coding standards checklist
Step 3: Verify that all code comments are clear and meaningful
Step 4: Ensure that unit tests are included and pass successfully
Step 5: Confirm that no known defect patterns are present in the code
Step 6: Approve the code review if all above conditions are met
✅ Expected Result: Code review process enforces defect prevention by ensuring code quality, clear comments, passing unit tests, and absence of defect patterns before approval
Automation Requirements - Python Selenium
Assertions Needed:
Verify coding standards checklist is marked complete
Verify code comments presence and clarity
Verify unit tests are present and passing
Verify no defect patterns detected in code review
Verify code review approval button is enabled after checks
Best Practices:
Use explicit waits for page elements
Use Page Object Model for code review page
Use clear and descriptive assertion messages
Avoid hardcoded waits
Use meaningful locators (id, name, css selectors)
Automated Solution
Testing Fundamentals
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.

Common Mistakes - 4 Pitfalls
Using hardcoded sleep instead of explicit waits
Using brittle XPath locators that break easily
Not using Page Object Model, mixing locators and test logic
Not verifying all required assertions, only checking one condition
Bonus Challenge

Now add data-driven testing with 3 different code review URLs to verify defect prevention on multiple reviews.

Show Hint