-
Notifications
You must be signed in to change notification settings - Fork 694
Description
Problem to solve
Currently, Hurl does not support mathematical operations in assertions. Users need to manually calculate expected values or use multiple variables, which makes tests less maintainable and harder to read, especially for API responses involving calculations like loan repayments, pricing, taxes, or any computed totals.
For example, if testing a loan API that returns totalRepayment, tenure, and installment, there's no way to directly assert that totalRepayment == tenure * installment within Hurl.
Proposal
Add support for mathematical expressions in Hurl assertions, allowing arithmetic operations (+, -, *, /, %, ^) between:
- JSON response values (using JSONPath)
- Variables (using
{{variable}}syntax) - Literal numbers
Example syntax:
POST https://api.example.com/loan/calculate
{
"amount": 10000,
"tenure": 12,
"rate": 5
}
HTTP 200
[Asserts]
$.totalRepayment == {{tenure}} * {{installment}}
$.totalInterest == {{tenure}} * {{installment}} - {{principal}}
$.monthlyPayment == ({{amount}} * {{rate}}) / 100
Alternative syntax (if above is too complex):
[Asserts]
$.totalRepayment == eval "{{tenure}} * {{installment}}"
The feature should support:
- Basic arithmetic operators:
+,-,*,/,%(modulo),^(power) - Parentheses for operation precedence
- Mixed operands (JSONPath values, variables, literals)
- Standard order of operations (PEMDAS/BODMAS)
Additional context and resources
Use cases:
- Financial APIs: Validating loan calculations, tax computations, pricing totals
- E-commerce: Asserting
total == subtotal + tax + shipping - Analytics APIs: Verifying percentage calculations or aggregated metrics
- Any computed values: Reducing test brittleness by expressing relationships rather than hard-coded values
Current workaround:
Users must either:
- Pre-calculate values and store in variables (requires external scripting)
- Skip validation of mathematical relationships entirely