Bird
Raised Fist0
Testing Fundamentalstesting~15 mins

Static analysis tools in Testing Fundamentals - Build an Automation Script

Choose your learning style10 modes available
Run static analysis on source code and verify reported issues
Preconditions (2)
Step 1: Open the terminal or command line interface
Step 2: Navigate to the project directory containing the source code
Step 3: Run the static analysis tool command to scan the source code
Step 4: Wait for the tool to complete the scan
Step 5: Review the output report generated by the tool
Step 6: Identify any warnings or errors reported by the tool
✅ Expected Result: The static analysis tool completes the scan without crashing and outputs a report listing any code issues such as syntax errors, code smells, or potential bugs.
Automation Requirements - Python unittest
Assertions Needed:
Verify the static analysis tool command runs successfully
Verify the output report file is created
Verify the report contains expected warning or error messages
Best Practices:
Use subprocess module to run external commands
Capture and check command exit codes
Parse output files safely
Use assertions to validate expected results
Automated Solution
Testing Fundamentals
import unittest
import subprocess
import os

class TestStaticAnalysis(unittest.TestCase):
    def test_static_analysis_run(self):
        # Define the command to run the static analysis tool
        # Example: pylint scanning a Python file named example.py
        command = ['pylint', 'example.py', '--output-format=text']

        # Run the command and capture output
        result = subprocess.run(command, capture_output=True, text=True)

        # Assert the command ran successfully (exit code 0 or 1 for pylint warnings)
        self.assertIn(result.returncode, [0, 1], f"Static analysis failed with code {result.returncode}")

        # Assert output contains pylint warning or error messages
        output = result.stdout
        self.assertTrue(len(output) > 0, "Static analysis output is empty")
        self.assertIn('warning', output.lower() + 'error', output.lower(), "No warnings or errors found in output")

        # Optionally, write output to a report file
        report_path = 'static_analysis_report.txt'
        with open(report_path, 'w') as f:
            f.write(output)

        # Assert report file is created and not empty
        self.assertTrue(os.path.exists(report_path), "Report file was not created")
        self.assertTrue(os.path.getsize(report_path) > 0, "Report file is empty")

if __name__ == '__main__':
    unittest.main()

This test script uses Python's unittest framework to automate running a static analysis tool (like pylint) on a source code file named example.py.

We use the subprocess.run function to execute the tool command and capture its output and exit code.

The test asserts that the tool runs successfully by checking the exit code is either 0 (no issues) or 1 (issues found but no crash).

It then checks that the output contains some warnings or errors by looking for keywords in the output text.

The output is saved to a report file, and the test verifies that this file exists and is not empty.

This approach ensures the static analysis tool runs correctly and produces a meaningful report, automating the manual test steps.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not checking the exit code of the static analysis tool', 'why_bad': 'The tool might fail silently or crash, and the test would not detect it.', 'correct_approach': "Always check the command's exit code to confirm it ran successfully."}
Hardcoding file paths without verifying they exist
Parsing output with fragile string matching
{'mistake': "Not capturing or checking the tool's output", 'why_bad': 'You miss verifying if the tool found any issues.', 'correct_approach': 'Capture stdout/stderr and assert expected warnings or errors.'}
Bonus Challenge

Now add data-driven testing to run static analysis on three different source files

Show Hint

Practice

(1/5)
1. What is the main purpose of static analysis tools in software testing?
easy
A. To deploy the software to production
B. To execute the program and check its output
C. To test the user interface manually
D. To find errors in code without running the program

Solution

  1. Step 1: Understand static analysis tools

    Static analysis tools examine code by reading it, not by running it.
  2. Step 2: Compare options

    Only To find errors in code without running the program describes checking code without execution, which is the main purpose.
  3. Final Answer:

    To find errors in code without running the program -> Option D
  4. Quick Check:

    Static analysis = code check without run [OK]
Hint: Static analysis checks code without running it [OK]
Common Mistakes:
  • Confusing static analysis with dynamic testing
  • Thinking static analysis runs the program
  • Mixing up manual UI testing with static analysis
2. Which of the following is a correct example of a static analysis tool?
easy
A. Selenium
B. ESLint
C. JUnit
D. Postman

Solution

  1. Step 1: Identify static analysis tools

    ESLint is a tool that checks JavaScript code for errors and style issues without running it.
  2. Step 2: Eliminate other options

    Selenium is for UI testing, JUnit is for unit testing (dynamic), Postman is for API testing.
  3. Final Answer:

    ESLint -> Option B
  4. Quick Check:

    ESLint = static analysis tool [OK]
Hint: ESLint is a popular static analysis tool for JavaScript [OK]
Common Mistakes:
  • Choosing Selenium which is for dynamic UI testing
  • Confusing JUnit with static analysis
  • Selecting Postman which tests APIs dynamically
3. Consider this Python code snippet checked by a static analysis tool:
def add(a, b):
    return a + b

result = add(5, '3')

What issue will the static analysis tool most likely report?
medium
A. Type mismatch: adding int and str
B. Syntax error due to missing colon
C. Unused variable 'result'
D. No issues found

Solution

  1. Step 1: Analyze the code for type errors

    The function adds two parameters. The call adds an integer 5 and a string '3', which is a type mismatch.
  2. Step 2: Understand static analysis detection

    Static analysis tools detect type mismatches like adding int and str without running the code.
  3. Final Answer:

    Type mismatch: adding int and str -> Option A
  4. Quick Check:

    Static analysis finds type errors like int + str [OK]
Hint: Look for type mismatches in function calls [OK]
Common Mistakes:
  • Thinking missing colon causes error (it is present)
  • Ignoring type mismatch between int and string
  • Assuming variable 'result' is unused error
4. A static analysis tool reports a warning: "Unused variable 'temp'" in this code:
def calculate(x):
    temp = x * 2
    return x + 1

What is the best fix?
medium
A. Add a print statement for 'temp' to use it
B. Rename 'temp' to 'result' to fix the warning
C. Remove the variable 'temp' since it is not used
D. Ignore the warning; it does not affect the code

Solution

  1. Step 1: Understand the warning

    The variable 'temp' is assigned but never used, which wastes memory and may confuse readers.
  2. Step 2: Choose the best fix

    Removing 'temp' cleans the code and resolves the warning. Renaming or printing does not solve the root cause.
  3. Final Answer:

    Remove the variable 'temp' since it is not used -> Option C
  4. Quick Check:

    Unused variables should be removed [OK]
Hint: Remove variables that are assigned but never used [OK]
Common Mistakes:
  • Renaming variable without using it
  • Adding print statements unnecessarily
  • Ignoring warnings thinking they are harmless
5. You want to use a static analysis tool to improve code quality before testing. Which approach is best?
hard
A. Integrate static analysis into the development process early
B. Run static analysis only on production code
C. Use static analysis only for UI code
D. Run static analysis after all tests pass

Solution

  1. Step 1: Understand when to use static analysis

    Static analysis is most effective when used early during development to catch errors before testing.
  2. Step 2: Evaluate options

    Integrate static analysis into the development process early suggests integrating static analysis early, which improves code quality and reduces bugs later.
  3. Final Answer:

    Integrate static analysis into the development process early -> Option A
  4. Quick Check:

    Early static analysis improves code quality [OK]
Hint: Use static analysis early in development [OK]
Common Mistakes:
  • Running static analysis only after tests
  • Limiting static analysis to production code
  • Applying static analysis only to UI code
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