Test Your Solver
The solvers all get called through ahandle(request) function. You can directly test this function using the same parameters that would come from Calcs.com.
To make it work only when you’re developing, you can use if __name__ == "__main__": to begin a short script that will only run when you’re directly running the module (e.g., the play button in VS Code).
Special Consideration: Warnings
Our warnings (the yellow bar that shows up on Calcs.com) need to be initialized before running, otherwise they’ll cause an error. You can initialize them withreset_warnings(), a function in the shared module.
Example (USGS API)
Adding the following bit of code to the bottom ofusgs_api.py and hitting the play button in VS Code:
Note the initial
reset_warnings() usageExpected Output
Returns (in the terminal):Make Sure Solver Tests Are Passing
We usepytest to run tests on solver functions. The results are stored in the tests folder with files that begin with the test_ prefix. When creating a PR, Buildkite automatically runs these tests, but you can also run these yourself to quickly check for changes or update values if needed.
Running Tests
-
Navigate to solver folder - from the dev-environment folder:
-
Run the tests:
To run ALL solver tests:
To run tests for a specific test file:
Best Practices
- Always initialize warnings with
reset_warnings()when testing locally - Match your Python environment to
solver/requirements.txtas closely as possible - Run pytest regularly to catch any regressions
- Test with realistic inputs that match what Calcs.com would send
- Use the
if __name__ == "__main__":pattern to keep test code separate from production code