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.