Static analysis tools in Testing Fundamentals - Build an Automation Script
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.
Now add data-driven testing to run static analysis on three different source files
Practice
static analysis tools in software testing?Solution
Step 1: Understand static analysis tools
Static analysis tools examine code by reading it, not by running it.Step 2: Compare options
Only To find errors in code without running the program describes checking code without execution, which is the main purpose.Final Answer:
To find errors in code without running the program -> Option DQuick Check:
Static analysis = code check without run [OK]
- Confusing static analysis with dynamic testing
- Thinking static analysis runs the program
- Mixing up manual UI testing with static analysis
Solution
Step 1: Identify static analysis tools
ESLint is a tool that checks JavaScript code for errors and style issues without running it.Step 2: Eliminate other options
Selenium is for UI testing, JUnit is for unit testing (dynamic), Postman is for API testing.Final Answer:
ESLint -> Option BQuick Check:
ESLint = static analysis tool [OK]
- Choosing Selenium which is for dynamic UI testing
- Confusing JUnit with static analysis
- Selecting Postman which tests APIs dynamically
def add(a, b):
return a + b
result = add(5, '3')What issue will the static analysis tool most likely report?
Solution
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.Step 2: Understand static analysis detection
Static analysis tools detect type mismatches like adding int and str without running the code.Final Answer:
Type mismatch: adding int and str -> Option AQuick Check:
Static analysis finds type errors like int + str [OK]
- Thinking missing colon causes error (it is present)
- Ignoring type mismatch between int and string
- Assuming variable 'result' is unused error
def calculate(x):
temp = x * 2
return x + 1What is the best fix?
Solution
Step 1: Understand the warning
The variable 'temp' is assigned but never used, which wastes memory and may confuse readers.Step 2: Choose the best fix
Removing 'temp' cleans the code and resolves the warning. Renaming or printing does not solve the root cause.Final Answer:
Remove the variable 'temp' since it is not used -> Option CQuick Check:
Unused variables should be removed [OK]
- Renaming variable without using it
- Adding print statements unnecessarily
- Ignoring warnings thinking they are harmless
Solution
Step 1: Understand when to use static analysis
Static analysis is most effective when used early during development to catch errors before testing.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.Final Answer:
Integrate static analysis into the development process early -> Option AQuick Check:
Early static analysis improves code quality [OK]
- Running static analysis only after tests
- Limiting static analysis to production code
- Applying static analysis only to UI code
Ready to prove it?
Pass the test and this pattern moves unknown
