# calculator.py
class Calculator:
def add(self, a, b):
pass
def subtract(self, a, b):
pass
# tests.py
import pytest
from calculator import Calculator
from unittest.mock import patch
class TestCalculator:
@pytest.fixture
def calculator(self):
return Calculator()
def test_add_positive_numbers(self, calculator):
result = calculator.add(2, 3)
assert result == 5
@patch('calculator.Calculator.add')
def test_mocked_add_method(self, mock_add, calculator):
# Mocking the add method
mock_add.return_value = 10
# Now when we call calculator.add, it will return the mocked value (10)
result = calculator.add(5, 5)
assert result == 10
# Verify that the add method was called with the correct arguments
mock_add.assert_called_once_with(5, 5)
def test_divide_by_zero(self):
with pytest.raises(ZeroDivisionError):
divide(10, 0)
import unittest
from calculator import Calculator
from unittest.mock import patch
class TestCalculator(unittest.TestCase):
def setUp(self):
self.calculator = Calculator()
def test_add_positive_numbers(self):
result = self.calculator.add(2, 3)
self.assertEqual(result, 5)
@patch('calculator.Calculator.get_greeting')
def test_mocked_get_greeting_method(self, mock_get_greeting):
# Mocking the get_greeting method
mock_get_greeting.return_value = "Mocked greeting"
# Now when we call calculator.get_greeting, it will return the mocked value ("Mocked greeting")
result = self.calculator.get_greeting()
self.assertEqual(result, "Mocked greeting")
# Verify that the get_greeting method was called
mock_get_greeting.assert_called_once()
def test_divide_by_zero(self):
with self.assertRaises(ZeroDivisionError):
divide(10, 0)
if __name__ == '__main__':
unittest.main()
• “banana”: 3 (An actual word.)
• “A” and “a”: 1 (A simple legal case with a positive result.)
• “”: 0 (A simple legal case with a zero result.)
• null: 0 (Simple error case.)
• “AA” and “aa”: 2 (A case where the count is > 1 and all As.)
• Consider counting other As in other languages (such as angstrom or umlaut).
• “b”: 0 (A simple, nonblank legal case with a negative result.)
• “aba”: 2 (Target character at the beginning and end to look for an off by-one loops bug.)
• “bab”: 1 (Target character in the middle of the string.)
• space/tabs/etc.: N (Whitespace characters mixed with N As.)
• long string without As: N, where N > 0
• long string with As: N, where N is equal to the number of As
• X\nX in the string: N, where N is equal to the number of As (Format characters.)
• {java/C/HTML/JavaScript}: N, where N is equal to the number of As (Executable characters, or errors or accidental interpretation of code.) Missing several of the previous tests is a bad indicator. Better candidates discuss more advanced testing issues and rise above the specifics of input selection.
• Question the look and feel, color palette, and contrast. Is it consistent with related applications? Is it accessible for the visually impaired, and so on?
• Worry that the text box is too small/suggest that it be long enough to accommodate the longer strings that can be entered.
• Wonder about multiple instances of this application on the same server. Is there a chance for crosstalk between users?
• Ask the question, “Is the data recorded?” It might contain addresses or other personally identifiable information.
• Suggest automation with some real-world data, such as drawing from a dictionary of words or text selections from books.
• Ask the questions, “Is it fast enough? Will it be fast enough under load?”
• Ask the questions, “Is it discoverable? How do users find this page?”
• Enter HTML and JavaScript. Does it break the page rendering?
• Ask whether it should count capital or lowercase As, or both.
• Try copying and pasting strings. Some concepts are even more advanced and indicate an experienced and valuable testing mind willing to look past only the problem presented.
• Realize that if the count is passed to the server via a URL-encoded HTTP GET request, the string can be clipped as it bounces across the Net. Therefore, there is no guarantee how long the supported URL can be.
• Suggest the application be parameterized. Why count only As?
• Consider whether this application can be internationalized.
• Think about writing scripts or manually sampling string lengths, say by powers of 2, to find the limits and ensure that string lengths in between would work.
• Consider the implementation and code behind this. There might be a counter to walk the string and another to keep track of how many As have been encountered (accumulator). So, it’s interesting to vary both the total number of As and the length of a string of As around interesting boundary values.
• Ask the questions, “Can the HTTP POST method and parameters be hacked? Perhaps there is security vulnerability?”
• Generate test input and validation with script to create interesting permutations and combinations of string properties such as length, number of As, and so on.
• Server side via API, in browser, what kind of app?