# Testing A Cylinder app is easy to test for the same reason it is easy to read: a request goes to one file, and that file is a plain function. Every app comes with a test client, so tests exercise the real routing, hooks, and error handlers without starting a server. ## The test client `app.test_client()` returns a [Werkzeug test client](https://werkzeug.palletsprojects.com/en/stable/test/#werkzeug.test.Client). It sends requests straight to the app, in the same process. Here is an example using [pytest](https://docs.pytest.org/en/stable/) that tests the app from the [Quickstart](quickstart.md). `conftest.py`: ```python import pytest import cylinder @pytest.fixture() def webapp1_app(caplog): def app_map(request): return "my_webapps", "webapp1" app = cylinder.get_app(app_map, log_handler=caplog.handler) # other setup can go here yield app # cleanup or reset logic can go here ``` `webapp1_test.py`: ```python def test_root(webapp1_app, caplog): webapp1_client = webapp1_app.test_client() response = webapp1_client.get("/") assert response.status_code == 200 assert response.data == b"Hello World!" assert "request completed" in caplog.text ``` Run `pytest` from the directory that contains `my_webapps`, because the webapp directory is found relative to the working directory. ## Sending different kinds of request The client has a method for each HTTP verb, and `open()` for anything else: ```python client.get("/users?lname=Smith") client.post("/users", data={"name": "Ada"}) client.post("/API/users", json={"name": "Ada"}) client.get("/admin", headers={"X-Admin-Token": "secret"}) client.head("/assets/logo.png") client.open("/foo/bar", method="MOVE") ``` The response has `status_code`, `headers`, `data`, and, for JSON bodies sent with a JSON content type, `json`. Cookies set by the app are kept by the client and sent with later requests, so a test can walk through a login. ## Testing with different dependencies Tests supply their own `app_map()`, so they can pass different [extra parameters](composition.md#extra-parameters-on-main) from the ones production uses: ```python @pytest.fixture() def client(): fake_users = {"1": {"id": "1", "name": "Ada"}} def app_map(request): return "my_webapps", "webapp1", {"users": fake_users} return cylinder.get_app(app_map).test_client() ``` Nothing has to be patched or mocked. The handler asked for `users` by name and receives whatever the test provides. ## Asserting on log output Passing pytest's `caplog.handler` as the `log_handler` sends Cylinder's log records to pytest, so a test can assert on them, as the first example does. Cylinder writes each log record as it is logged, so the lines of a request are in `caplog` as soon as the request returns. A test can check them on the next line. The handler keeps the formatter that pytest gave it, so `caplog.text` is in pytest's format and not in [Cylinder's](operations.md#where-logs-go). The message of each record is the same in both. ## Cleanup callbacks Functions registered with [`response.call_on_close()`](composition.md#cleaning-up-after-a-request) run when the WSGI server closes the response. With the test client, that happens when the test calls `response.close()`: ```python def test_connection_is_closed(client): response = client.get("/") response.close() # assert that the cleanup happened ``` ## Testing a handler directly A handler is a function, so a unit test can also call it with no client at all: ```python import importlib.util from werkzeug.wrappers import Response def load(path): spec = importlib.util.spec_from_file_location("handler", path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def test_welcome(): handler = load("my_webapps/webapp1/welcome.page.get.py") response = handler.main(Response(), {"site_name": "Test Site"}) assert response.data == b"Welcome to Test Site" ``` Handler filenames contain dots, so they cannot be imported with an `import` statement; the helper above loads one by path. Most projects find the test client covers everything they need, and it has the advantage of testing the routing too.