> ## Documentation Index
> Fetch the complete documentation index at: https://calcs.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing Solvers Without Localhost

> How to test Python solvers locally without setting up the full localhost development environment

Our localhost system is quite powerful for software development, but is quite cumbersome and difficult to set up. For most solver work, you don't actually need to set up the localhost just to test out some code.

<Warning>
  **Keep in mind:** The solvers run on AWS in a lambda environment with *only* the version of Python modules specified in `solver/requirements.txt`. If your Python environment doesn't match this exactly, you run the risk of things not working as expected. Pay particular attention to pytest results and always be careful when dealing with new solvers.
</Warning>

## Test Your Solver

The solvers all get called through a `handle(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 with `reset_warnings()`, a function in the `shared` module.

## Example (USGS API)

Adding the following bit of code to the bottom of `usgs_api.py` and hitting the play button in VS Code:

<Note>
  Note the initial `reset_warnings()` usage
</Note>

```python theme={null}
if __name__ == "__main__":
    # Reset/initialize warnings so that the warning() function works properly
    from shared import reset_warnings
    reset_warnings()

    # run the solver with an example query
    print(
        handle(
            {
                "latitude": 35.21,
                "longitude": -90.05,
                "riskCategory": "II",
                "siteClass": "D-default",
                "standard": "ASCE 7-16",
            }
        )
    )
```

### Expected Output

Returns (in the terminal):

```python theme={null}
{'Ss': 1.1, 'S1': 0.374, 'TL': {'mathjs': 'Unit', 'value': 12, 'unit': 's'}, 'SDC': None}
```

This is *exactly* the value that would be returned from the remote widget in Calcs.com. To test things out, you could paste this in an equation widget and pretend it's a remote widget.

## Make Sure Solver Tests Are Passing

We use `pytest` 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

1. **Navigate to solver folder** - from the dev-environment folder:
   ```bash theme={null}
   cd solver
   ```

2. **Run the tests:**

   To run **ALL** solver tests:

   ```bash theme={null}
   pytest --pspec tests/
   ```

   To run tests for a **specific test file**:

   ```bash theme={null}
   pytest --pspec tests/test_your_file.py
   ```

## Best Practices

1. **Always initialize warnings** with `reset_warnings()` when testing locally
2. **Match your Python environment** to `solver/requirements.txt` as closely as possible
3. **Run pytest regularly** to catch any regressions
4. **Test with realistic inputs** that match what Calcs.com would send
5. **Use the `if __name__ == "__main__":` pattern** to keep test code separate from production code

This approach allows you to develop and test solvers efficiently without the overhead of setting up the full localhost development environment.
