numpy-financial
numpy-financial copied to clipboard
Add fuzz test, property-based test, and table test for PV function
Description
This change request adds testing to the pv
/ present value function.
- Tests
- Adds fuzz tests and property-based tests (PBT) using Hypothesis
- Adds more example-based tests (with pytest.mark.parametrize)
- The hope is that these tests can be extended to other functions as well.
- Add exception handling for
InvalidOperation
,TypeError
,DivisionByZero
,Overflow
. ~I opted to mirrornumpy
and return-0.0
. Arguably, we would want a runtime error and can ignore it in the fuzz tests.~
Context:
- We use fuzz tests for extreme value testing (e.g., empty values, very low, very high, or zero) using a randomized input generator.
- We use property-based tests to verify that certain conditions hold true across a range of inputs.
- I used type hints and Hypothesis Ghostwriter CLI feature to help write the strategy.
- We can generate random
np.ndarray
of small dimensions, but this was a bit slow. I opted to test array-input directly (tests/test_financial.py::TestPV::test_pv_examples[rates_as_array
).
- We can generate random
Testing
If you want to select only the related tests, you can run them like so.
$ pytest tests/test_financial.py::TestPV -vv
tests/test_financial.py::TestPV::test_pv PASSED [ 9%]
tests/test_financial.py::TestPV::test_pv_decimal PASSED [ 18%]
tests/test_financial.py::TestPV::test_pv_examples[default_fv_and_when] PASSED [ 27%]
tests/test_financial.py::TestPV::test_pv_examples[specify_fv_and_when] PASSED [ 36%]
tests/test_financial.py::TestPV::test_pv_examples[when_1] PASSED [ 45%]
tests/test_financial.py::TestPV::test_pv_examples[when_1_and_fv_1000] PASSED [ 54%]
tests/test_financial.py::TestPV::test_pv_examples[fv>0] PASSED [ 63%]
tests/test_financial.py::TestPV::test_pv_examples[negative_rate] PASSED [ 72%]
tests/test_financial.py::TestPV::test_pv_examples[rates_as_array] PASSED [ 81%]
tests/test_financial.py::TestPV::test_pv_fuzz PASSED [ 90%]
tests/test_financial.py::TestPV::test_pv_interest_rate_sensitivity PASSED [100%]
====================================================================================== 11 passed in 0.82s ======================================================================================