CREDIT: https://github.com/plain-vanilla-games/flask-test-kata
This is a python testing kata for a simple Flask web "service".
This project has the following skeleton:
app
├── calculator
│ ├── __init__.py
│ ├── app.py
│ ├── logic.py
│ └── templates
│ └── index.html
├── README.md
├── requirements.txt
└── tests
├── __init__.py
├── base.py
├── integration
| ├── __init__.py
| └── test_views.py
└── unit
├── __init__.py
└── test_logic.py
The tasks at hand:
-
Implement
test_mul
unit test for the multiply method inlogic.Calculator
, then flesh outlogic.Calculator.mul
until test passes. -
test_mul
as a test is rather coarse. A better test would be in the formtest_function_satisfies_condition_after_pre-condition
. Implement the teststest_mul_with_two_positive_numbers
,test_mul_with_two_negative_numbers
,test_mul_with_one_negative_one_positive_number
. -
The constructor of
logic.Calculator
takes two argumentsmin_value
andmax_value
. Change the class so that it's methods throw the exceptions inlogic.py
when confronted with numbers that are too high or too low. -
Make sure to add tests to check the boundary of
min_value
andmax_value
. -
logic.Calculator
contains another method stub:div
. This method should dividea
byb
, but it is just a stub now. Write corresponding tests for the method, as you've written forlogic.Calculator.mul
, then implement the method to pass your tests. -
Division has more caveats than multiplication. Python will raise an exception of type ZeroDivisionError when you attempt to divide by zero, write a test asserts that your Calculator class does not suppress them.
The preferred way of testing whether exceptions are raised is to use
with pytest.raises(MyException):
code...
-
If by now the multiplication logic has been correctly implemented, the
test_multiply
integration test should be passing. Implement integration tests which verify the responses for valid and invalid inputs (think about the boundaries of max and min values). -
Write corresponding integration tests for the
/div
view -
Make sure that for requests that attempt to divide by zero that the calculator app returns an appropriate 403 response.