Bird
Raised Fist0
Testing Fundamentalstesting~15 mins

Defect prevention strategies in Testing Fundamentals - Build an Automation Script

Choose your learning style10 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

Practice

(1/5)
1. Which of the following is a primary goal of defect prevention in software testing?
easy
A. To find defects after software release
B. To stop defects before coding starts
C. To increase the number of test cases
D. To delay testing until the end of development

Solution

  1. Step 1: Understand defect prevention purpose

    Defect prevention aims to stop bugs early, ideally before coding begins.
  2. 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.
  3. Final Answer:

    To stop defects before coding starts -> Option B
  4. Quick Check:

    Defect prevention = stop defects early [OK]
Hint: Defect prevention means stopping bugs before coding [OK]
Common Mistakes:
  • Confusing defect prevention with defect detection
  • Thinking testing only happens after coding
  • Believing more test cases prevent defects
  • Assuming testing should be delayed
2. Which of the following is a correct example of a defect prevention technique?
easy
A. Conducting code reviews before integration
B. Ignoring minor bugs during development
C. Writing test scripts after coding is complete
D. Skipping documentation to save time

Solution

  1. Step 1: Identify defect prevention techniques

    Defect prevention includes activities like code reviews done early to catch issues.
  2. Step 2: Evaluate each option

    Only Conducting code reviews before integration describes a proactive step before integration; others are reactive or harmful.
  3. Final Answer:

    Conducting code reviews before integration -> Option A
  4. Quick Check:

    Code reviews before integration = defect prevention [OK]
Hint: Code reviews before integration prevent defects early [OK]
Common Mistakes:
  • Thinking testing after coding is prevention
  • Ignoring small bugs helps prevent defects
  • Skipping documentation is a prevention method
  • Confusing defect detection with prevention
3. Consider this code snippet for a peer review process:
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?
medium
A. SyntaxError
B. []
C. ['TODO']
D. ['Unfinished task']

Solution

  1. Step 1: Analyze the review_code function

    The function checks each line for 'TODO' and adds 'Unfinished task' to defects_found if found.
  2. Step 2: Check code_sample lines for 'TODO'

    The second line contains '# TODO: fix bug', so 'Unfinished task' is added once.
  3. Final Answer:

    ['Unfinished task'] -> Option D
  4. Quick Check:

    TODO found = 'Unfinished task' added [OK]
Hint: Look for 'TODO' in lines to find defects [OK]
Common Mistakes:
  • Assuming empty list if no exact match
  • Confusing 'TODO' string with defect name
  • Expecting syntax errors due to code formatting
  • Ignoring the append operation
4. A team uses pair programming to prevent defects but notices many bugs still appear. What is the most likely issue?
medium
A. The pairs are not communicating effectively
B. Pair programming is not a defect prevention strategy
C. They are writing too many test cases
D. They are skipping code reviews

Solution

  1. Step 1: Understand pair programming role

    Pair programming helps prevent defects by collaboration and communication.
  2. Step 2: Identify why defects still appear

    If bugs persist, poor communication between pairs is a common cause.
  3. Final Answer:

    The pairs are not communicating effectively -> Option A
  4. Quick Check:

    Poor communication reduces pair programming effectiveness [OK]
Hint: Good communication is key in pair programming [OK]
Common Mistakes:
  • 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
5. A software team wants to reduce defects by combining multiple prevention strategies. Which combination is best to catch defects early and improve quality?
hard
A. Automated testing after coding, skipping reviews, and late integration
B. Skipping documentation, delaying testing, and using only manual code inspection
C. Code reviews before coding, pair programming during development, and static analysis tools
D. Writing test cases after release, ignoring minor bugs, and manual testing only

Solution

  1. Step 1: Identify effective defect prevention methods

    Code reviews before coding, pair programming, and static analysis tools are proactive strategies.
  2. 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.
  3. Final Answer:

    Code reviews before coding, pair programming during development, and static analysis tools -> Option C
  4. Quick Check:

    Combine early reviews + collaboration + tools = best prevention [OK]
Hint: Combine early reviews, collaboration, and tools for best prevention [OK]
Common Mistakes:
  • Choosing late testing as prevention
  • Ignoring documentation and reviews
  • Relying only on manual testing
  • Skipping static analysis tools
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard