Code review as testing in Testing Fundamentals - Build an Automation Script
import unittest import subprocess # Run pylint to check code style and syntax result = subprocess.run(['pylint', 'calculator.py', '--disable=all', '--enable=syntax-error,style'], capture_output=True, text=True) class TestCalculatorFunctions(unittest.TestCase): def test_add(self): from calculator import add self.assertEqual(add(2, 3), 5, 'Add function should return sum of two numbers') self.assertEqual(add(-1, 1), 0, 'Add function should handle negative numbers') def test_subtract(self): from calculator import subtract self.assertEqual(subtract(5, 3), 2, 'Subtract function should return difference') self.assertEqual(subtract(0, 0), 0, 'Subtract function should handle zero inputs') if __name__ == '__main__': # Check pylint output for errors print('Pylint output:') print(result.stdout) assert 'syntax-error' not in result.stdout, 'Syntax errors found in code' assert 'style' not in result.stdout, 'Style issues found in code' # Run unit tests unittest.main()
This script first runs pylint on the calculator.py file to check for syntax errors and style issues. It captures the output and asserts that no syntax errors or style problems are present.
Then, it defines unit tests for two example functions: add and subtract. Each test checks that the function returns correct results for sample inputs, with clear assertion messages to explain failures.
Using unittest ensures that function logic is tested automatically. Running pylint helps catch style and syntax problems early. This combination mimics a code review by automatically checking common issues.
Now add data-driven testing with 3 different input sets for each function
Practice
code review in software testing?Solution
Step 1: Understand code review purpose
Code review is done to check code manually for errors and improvements before execution.Step 2: Compare options with purpose
Only To find bugs and improve code quality before running the code matches the goal of finding bugs and improving quality before running code.Final Answer:
To find bugs and improve code quality before running the code -> Option AQuick Check:
Code review = find bugs early [OK]
- Thinking code review replaces automated tests
- Believing code review speeds up deployment
- Confusing code review with coding new features
Solution
Step 1: Understand effective feedback
Good feedback is clear, constructive, and helps improve code with examples.Step 2: Evaluate options
Provide clear, constructive comments with examples matches this approach; others are either rude, incomplete, or ignore issues.Final Answer:
Provide clear, constructive comments with examples -> Option AQuick Check:
Constructive feedback = clear + helpful [OK]
- Giving vague or rude comments
- Ignoring minor but important issues
- Only pointing out syntax errors
def add_numbers(a, b):
return a + b
result = add_numbers(2, '3')
print(result)What will happen when this code runs?
Solution
Step 1: Analyze function call with mixed types
The function adds an integer 2 and a string '3', which is invalid in Python.Step 2: Understand Python behavior on adding int and str
Python raises a TypeError when trying to add int and str directly.Final Answer:
It will raise a TypeError -> Option CQuick Check:
int + str = TypeError [OK]
- Assuming Python converts string to int automatically
- Thinking it concatenates as strings
- Expecting numeric addition without error
function multiply(x, y) {
return x * y;
}
console.log(multiply(5));What is the issue and how to fix it?
Solution
Step 1: Check function call with missing argument
Calling multiply(5) passes only one argument; y is undefined.Step 2: Understand JavaScript behavior multiplying number and undefined
Multiplying number by undefined results in NaN (Not a Number).Step 3: Fix by setting default value for y
Use default parameter likefunction multiply(x, y = 1)to avoid NaN.Final Answer:
multiply returns NaN; fix by setting default value for y -> Option BQuick Check:
Missing arg causes NaN; default fixes [OK]
- Assuming missing argument is zero
- Expecting error instead of NaN
- Ignoring default parameter usage
data = [1, 2, 3, 4, 5]
result = {x: x*2 for x in data if x > 2}
result[3] = 10
print(result)What is the final output and what does it teach about code review?
Solution
Step 1: Understand dictionary comprehension with condition
Creates dict with keys 3,4,5 and values doubled: {3:6,4:8,5:10}.Step 2: Note mutation of result[3]
Code changes key 3's value from 6 to 10 explicitly.Step 3: Final output and lesson
Prints {3:10,4:8,5:10}. Code review helps catch such unintended mutations.Final Answer:
{3: 10, 4: 8, 5: 10} - Code review helps catch unintended data changes -> Option DQuick Check:
Mutation changes dict; review catches it [OK]
- Ignoring dictionary mutation after comprehension
- Assuming all data keys included
- Expecting error instead of changed output
Ready to prove it?
Pass the test and this pattern moves unknown
