0
0
Testing Fundamentalstesting~15 mins

Code review as testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Review code for common bugs and style issues
Preconditions (2)
Step 1: Open the 'calculator.py' file
Step 2: Check for syntax errors such as missing colons or parentheses
Step 3: Look for logical errors like incorrect operator usage
Step 4: Verify that variable names are meaningful and consistent
Step 5: Ensure functions have clear and simple logic
Step 6: Check for missing error handling in functions
Step 7: Look for repeated code that can be simplified
Step 8: Verify that comments explain complex parts of the code
Step 9: Check that the code follows PEP 8 style guidelines
✅ Expected Result: All syntax errors, logical bugs, style issues, and missing error handling are identified and documented
Automation Requirements - pylint and unittest
Assertions Needed:
No syntax errors detected by pylint
Functions return expected results for given inputs
Code style conforms to PEP 8 standards
Best Practices:
Use static code analysis tools like pylint for style and syntax checks
Write unit tests to verify function logic
Use meaningful assertion messages
Keep tests small and focused
Run tests automatically after code changes
Automated Solution
Testing Fundamentals
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.

Common Mistakes - 3 Pitfalls
Ignoring pylint warnings and errors
Writing unit tests without assertions
Hardcoding test inputs without covering edge cases
Bonus Challenge

Now add data-driven testing with 3 different input sets for each function

Show Hint